build-manifest-plugin.js.map 15.5 KB
Newer Older
1
{"version":3,"sources":["../../../../build/webpack/plugins/build-manifest-plugin.ts"],"names":["generateClientManifest","compiler","assetMap","rewrites","compilerSpan","spans","get","genClientManifestSpan","traceChild","traceFn","clientManifest","__rewrites","appDependencies","Set","pages","sortedPageKeys","Object","keys","forEach","page","dependencies","filteredDeps","filter","dep","has","length","sortedPages","getEntrypointFiles","entrypoint","getFiles","file","test","map","replace","processRoute","r","rewrite","destination","startsWith","BuildManifestPlugin","constructor","options","buildId","isDevFallback","beforeFiles","afterFiles","fallback","createAssets","compilation","assets","createAssetsSpan","entrypoints","polyfillFiles","devFiles","ampDevFiles","lowPriorityFiles","ampFirstPages","ampFirstEntryNames","ampFirstEntryNamesMap","entryName","pagePath","push","mainFiles","CLIENT_STATIC_FILES_RUNTIME_MAIN","CLIENT_STATIC_FILES_RUNTIME_POLYFILLS","CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH","CLIENT_STATIC_FILES_RUNTIME_AMP","systemEntrypoints","values","name","filesForPage","CLIENT_STATIC_FILES_PATH","srcEmptySsgManifest","ssgManifestPath","sources","RawSource","sort","reduce","a","c","buildManifestName","BUILD_MANIFEST","JSON","stringify","clientManifestPath","apply","isWebpack5","hooks","make","tap","processAssets","stage","webpack","Compilation","PROCESS_ASSETS_STAGE_ADDITIONS","emit"],"mappings":"4DAAA,2EACA,2DAKA,6DASA,qHACA,wEAEA,4DACA,mD,mFAOA;AACA;AACA,QAASA,CAAAA,sBAAT,CACEC,QADF,CAEEC,QAFF,CAGEC,QAHF,CAIU,CACR,KAAMC,CAAAA,YAAY,CAAGC,uBAAMC,GAAN,CAAUL,QAAV,CAArB,CACA,KAAMM,CAAAA,qBAAqB,CAAGH,YAAH,cAAGA,YAAY,CAAEI,UAAd,CAC5B,4CAD4B,CAA9B,CAIA,MAAOD,CAAAA,qBAAP,cAAOA,qBAAqB,CAAEE,OAAvB,CAA+B,IAAM,CAC1C,KAAMC,CAAAA,cAAmC,CA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AArD,EACD,CAFD,EAGD,CA3JsC,C","sourcesContent":["import devalue from 'next/dist/compiled/devalue'\nimport {\n  webpack,\n  isWebpack5,\n  sources,\n} from 'next/dist/compiled/webpack/webpack'\nimport {\n  BUILD_MANIFEST,\n  CLIENT_STATIC_FILES_PATH,\n  CLIENT_STATIC_FILES_RUNTIME_MAIN,\n  CLIENT_STATIC_FILES_RUNTIME_POLYFILLS,\n  CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,\n  CLIENT_STATIC_FILES_RUNTIME_AMP,\n} from '../../../next-server/lib/constants'\nimport { BuildManifest } from '../../../next-server/server/get-page-files'\nimport getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'\nimport { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'\nimport { Rewrite } from '../../../lib/load-custom-routes'\nimport { getSortedRoutes } from '../../../next-server/lib/router/utils'\nimport { spans } from './profiling-plugin'\nimport { CustomRoutes } from '../../../lib/load-custom-routes'\n\ntype DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }\n\nexport type ClientBuildManifest = Record<string, string[]>\n\n// This function takes the asset map generated in BuildManifestPlugin and creates a\n// reduced version to send to the client.\nfunction generateClientManifest(\n  compiler: any,\n  assetMap: BuildManifest,\n  rewrites: CustomRoutes['rewrites']\n): string {\n  const compilerSpan = spans.get(compiler)\n  const genClientManifestSpan = compilerSpan?.traceChild(\n    'NextJsBuildManifest-generateClientManifest'\n  )\n\n  return genClientManifestSpan?.traceFn(() => {\n    const clientManifest: ClientBuildManifest = {\n      // TODO: update manifest type to include rewrites\n      __rewrites: rewrites as any,\n    }\n    const appDependencies = new Set(assetMap.pages['/_app'])\n    const sortedPageKeys = getSortedRoutes(Object.keys(assetMap.pages))\n\n    sortedPageKeys.forEach((page) => {\n      const dependencies = assetMap.pages[page]\n\n      if (page === '/_app') return\n      // Filter out dependencies in the _app entry, because those will have already\n      // been loaded by the client prior to a navigation event\n      const filteredDeps = dependencies.filter(\n        (dep) => !appDependencies.has(dep)\n      )\n\n      // The manifest can omit the page if it has no requirements\n      if (filteredDeps.length) {\n        clientManifest[page] = filteredDeps\n      }\n    })\n    // provide the sorted pages as an array so we don't rely on the object's keys\n    // being in order and we don't slow down look-up time for page assets\n    clientManifest.sortedPages = sortedPageKeys\n\n    return devalue(clientManifest)\n  })\n}\n\nfunction getEntrypointFiles(entrypoint: any): string[] {\n  return (\n    entrypoint\n      ?.getFiles()\n      .filter((file: string) => {\n        // We don't want to include `.hot-update.js` files into the initial page\n        return /(?<!\\.hot-update)\\.(js|css)($|\\?)/.test(file)\n      })\n      .map((file: string) => file.replace(/\\\\/g, '/')) ?? []\n  )\n}\n\nconst processRoute = (r: Rewrite) => {\n  const rewrite = { ...r }\n\n  // omit external rewrite destinations since these aren't\n  // handled client-side\n  if (!rewrite.destination.startsWith('/')) {\n    delete (rewrite as any).destination\n  }\n  return rewrite\n}\n\n// This plugin creates a build-manifest.json for all assets that are being output\n// It has a mapping of \"entry\" filename to real filename. Because the real filename can be hashed in production\nexport default class BuildManifestPlugin {\n  private buildId: string\n  private rewrites: CustomRoutes['rewrites']\n  private isDevFallback: boolean\n\n  constructor(options: {\n    buildId: string\n    rewrites: CustomRoutes['rewrites']\n    isDevFallback?: boolean\n  }) {\n    this.buildId = options.buildId\n    this.isDevFallback = !!options.isDevFallback\n    this.rewrites = {\n      beforeFiles: [],\n      afterFiles: [],\n      fallback: [],\n    }\n    this.rewrites.beforeFiles = options.rewrites.beforeFiles.map(processRoute)\n    this.rewrites.afterFiles = options.rewrites.afterFiles.map(processRoute)\n    this.rewrites.fallback = options.rewrites.fallback.map(processRoute)\n  }\n\n  createAssets(compiler: any, compilation: any, assets: any) {\n    const compilerSpan = spans.get(compiler)\n    const createAssetsSpan = compilerSpan?.traceChild(\n      'NextJsBuildManifest-createassets'\n    )\n    return createAssetsSpan?.traceFn(() => {\n      const entrypoints: Map<string, any> = compilation.entrypoints\n      const assetMap: DeepMutable<BuildManifest> = {\n        polyfillFiles: [],\n        devFiles: [],\n        ampDevFiles: [],\n        lowPriorityFiles: [],\n        pages: { '/_app': [] },\n        ampFirstPages: [],\n      }\n\n      const ampFirstEntryNames = ampFirstEntryNamesMap.get(compilation)\n      if (ampFirstEntryNames) {\n        for (const entryName of ampFirstEntryNames) {\n          const pagePath = getRouteFromEntrypoint(entryName)\n          if (!pagePath) {\n            continue\n          }\n\n          assetMap.ampFirstPages.push(pagePath)\n        }\n      }\n\n      const mainFiles = new Set(\n        getEntrypointFiles(entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN))\n      )\n\n      assetMap.polyfillFiles = getEntrypointFiles(\n        entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_POLYFILLS)\n      ).filter((file) => !mainFiles.has(file))\n\n      assetMap.devFiles = getEntrypointFiles(\n        entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH)\n      ).filter((file) => !mainFiles.has(file))\n\n      assetMap.ampDevFiles = getEntrypointFiles(\n        entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_AMP)\n      )\n\n      const systemEntrypoints = new Set([\n        CLIENT_STATIC_FILES_RUNTIME_MAIN,\n        CLIENT_STATIC_FILES_RUNTIME_POLYFILLS,\n        CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,\n        CLIENT_STATIC_FILES_RUNTIME_AMP,\n      ])\n\n      for (const entrypoint of compilation.entrypoints.values()) {\n        if (systemEntrypoints.has(entrypoint.name)) continue\n        const pagePath = getRouteFromEntrypoint(entrypoint.name)\n\n        if (!pagePath) {\n          continue\n        }\n\n        const filesForPage = getEntrypointFiles(entrypoint)\n\n        assetMap.pages[pagePath] = [...new Set([...mainFiles, ...filesForPage])]\n      }\n\n      if (!this.isDevFallback) {\n        // Add the runtime build manifest file (generated later in this file)\n        // as a dependency for the app. If the flag is false, the file won't be\n        // downloaded by the client.\n        assetMap.lowPriorityFiles.push(\n          `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`\n        )\n        // Add the runtime ssg manifest file as a lazy-loaded file dependency.\n        // We also stub this file out for development mode (when it is not\n        // generated).\n        const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`\n\n        const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js`\n        assetMap.lowPriorityFiles.push(ssgManifestPath)\n        assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest)\n      }\n\n      assetMap.pages = Object.keys(assetMap.pages)\n        .sort()\n        // eslint-disable-next-line\n        .reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)\n\n      let buildManifestName = BUILD_MANIFEST\n\n      if (this.isDevFallback) {\n        buildManifestName = `fallback-${BUILD_MANIFEST}`\n      }\n\n      assets[buildManifestName] = new sources.RawSource(\n        JSON.stringify(assetMap, null, 2)\n      )\n\n      if (!this.isDevFallback) {\n        const clientManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`\n\n        assets[clientManifestPath] = new sources.RawSource(\n          `self.__BUILD_MANIFEST = ${generateClientManifest(\n            compiler,\n            assetMap,\n            this.rewrites\n          )};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`\n        )\n      }\n\n      return assets\n    })\n  }\n\n  apply(compiler: webpack.Compiler) {\n    if (isWebpack5) {\n      compiler.hooks.make.tap('NextJsBuildManifest', (compilation) => {\n        // @ts-ignore TODO: Remove ignore when webpack 5 is stable\n        compilation.hooks.processAssets.tap(\n          {\n            name: 'NextJsBuildManifest',\n            // @ts-ignore TODO: Remove ignore when webpack 5 is stable\n            stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,\n          },\n          (assets: any) => {\n            this.createAssets(compiler, compilation, assets)\n          }\n        )\n      })\n      return\n    }\n\n    compiler.hooks.emit.tap('NextJsBuildManifest', (compilation: any) => {\n      this.createAssets(compiler, compilation, compilation.assets)\n    })\n  }\n}\n"]}