{"version":3,"sources":["../../../../src/build/webpack/plugins/build-manifest-plugin.ts"],"sourcesContent":["import type { BloomFilter } from '../../../shared/lib/bloom-filter'\nimport type { CustomRoutes } from '../../../lib/load-custom-routes'\nimport devalue from 'next/dist/compiled/devalue'\nimport { webpack, sources } from 'next/dist/compiled/webpack/webpack'\nimport {\n  BUILD_MANIFEST,\n  MIDDLEWARE_BUILD_MANIFEST,\n  CLIENT_STATIC_FILES_PATH,\n  CLIENT_STATIC_FILES_RUNTIME_MAIN,\n  CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,\n  CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL,\n  CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,\n  SYSTEM_ENTRYPOINTS,\n} from '../../../shared/lib/constants'\nimport type { BuildManifest } from '../../../server/get-page-files'\nimport getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint'\nimport { getSortedRoutes } from '../../../shared/lib/router/utils'\nimport { Span } from '../../../trace'\nimport { getCompilationSpan } from '../utils'\nimport {\n  createEdgeRuntimeManifest,\n  normalizeRewritesForBuildManifest,\n  processRoute,\n  srcEmptySsgManifest,\n  type ClientBuildManifest,\n} from './build-manifest-plugin-utils'\n\ntype DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }\n\nfunction buildNodejsLowPriorityPath(filename: string, buildId: string) {\n  return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`\n}\n\n// This function takes the asset map generated in BuildManifestPlugin and creates a\n// reduced version to send to the client.\nexport function generateClientManifest(\n  assetMap: BuildManifest,\n  rewrites: CustomRoutes['rewrites'],\n  clientRouterFilters?: {\n    staticFilter: ReturnType<BloomFilter['export']>\n    dynamicFilter: ReturnType<BloomFilter['export']>\n  },\n  compiler?: any,\n  compilation?: any\n): string | undefined {\n  const compilationSpan = compilation\n    ? getCompilationSpan(compilation)\n    : compiler\n      ? getCompilationSpan(compiler)\n      : new Span({ name: 'client-manifest' })\n\n  const genClientManifestSpan = compilationSpan?.traceChild(\n    'NextJsBuildManifest-generateClientManifest'\n  )\n\n  return genClientManifestSpan?.traceFn(() => {\n    const clientManifest: ClientBuildManifest = {\n      __rewrites: normalizeRewritesForBuildManifest(rewrites) as any,\n      __routerFilterStatic: clientRouterFilters?.staticFilter as any,\n      __routerFilterDynamic: clientRouterFilters?.dynamicFilter 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\nexport function 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\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  private appDirEnabled: boolean\n  private clientRouterFilters?: Parameters<typeof generateClientManifest>[2]\n\n  constructor(options: {\n    buildId: string\n    rewrites: CustomRoutes['rewrites']\n    isDevFallback?: boolean\n    appDirEnabled: boolean\n    clientRouterFilters?: Parameters<typeof generateClientManifest>[2]\n  }) {\n    this.buildId = options.buildId\n    this.isDevFallback = !!options.isDevFallback\n    this.rewrites = {\n      beforeFiles: [],\n      afterFiles: [],\n      fallback: [],\n    }\n    this.appDirEnabled = options.appDirEnabled\n    this.clientRouterFilters = options.clientRouterFilters\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) {\n    const compilationSpan =\n      getCompilationSpan(compilation) ?? getCompilationSpan(compiler)\n    if (!compilationSpan) {\n      throw new Error('No span found for compilation')\n    }\n\n    const createAssetsSpan = compilationSpan.traceChild(\n      'NextJsBuildManifest-createassets'\n    )\n\n    return createAssetsSpan.traceFn(() => {\n      const entrypoints: Map<string, any> = compilation.entrypoints\n      const assetMap: DeepMutable<BuildManifest> = {\n        polyfillFiles: [],\n        devFiles: [],\n        lowPriorityFiles: [],\n        rootMainFiles: [],\n        rootMainFilesTree: {},\n        pages: { '/_app': [] },\n      }\n\n      const mainFiles = new Set(\n        getEntrypointFiles(entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN))\n      )\n\n      if (this.appDirEnabled) {\n        assetMap.rootMainFiles = [\n          ...new Set(\n            getEntrypointFiles(\n              entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_APP)\n            )\n          ),\n        ]\n      }\n\n      const compilationAssets: {\n        name: string\n        source: typeof sources.RawSource\n        info: object\n      }[] = compilation.getAssets()\n\n      assetMap.polyfillFiles = compilationAssets\n        .filter((p) => {\n          // Ensure only .js files are passed through\n          if (!p.name.endsWith('.js')) {\n            return false\n          }\n\n          return (\n            p.info && CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL in p.info\n          )\n        })\n        .map((v) => v.name)\n\n      assetMap.devFiles = getEntrypointFiles(\n        entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH)\n      ).filter((file) => !mainFiles.has(file))\n\n      for (const entrypoint of compilation.entrypoints.values()) {\n        if (SYSTEM_ENTRYPOINTS.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        const buildManifestPath = buildNodejsLowPriorityPath(\n          '_buildManifest.js',\n          this.buildId\n        )\n        const ssgManifestPath = buildNodejsLowPriorityPath(\n          '_ssgManifest.js',\n          this.buildId\n        )\n        assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath)\n        compilation.emitAsset(\n          ssgManifestPath,\n          new sources.RawSource(srcEmptySsgManifest)\n        )\n      }\n\n      assetMap.pages = Object.keys(assetMap.pages)\n        .sort()\n        .reduce(\n          // eslint-disable-next-line\n          (a, c) => ((a[c] = assetMap.pages[c]), a),\n          {} as typeof assetMap.pages\n        )\n\n      let buildManifestName = BUILD_MANIFEST\n\n      if (this.isDevFallback) {\n        buildManifestName = `fallback-${BUILD_MANIFEST}`\n      }\n\n      compilation.emitAsset(\n        buildManifestName,\n        new sources.RawSource(JSON.stringify(assetMap, null, 2))\n      )\n\n      compilation.emitAsset(\n        `server/${MIDDLEWARE_BUILD_MANIFEST}.js`,\n        new sources.RawSource(`${createEdgeRuntimeManifest(assetMap)}`)\n      )\n\n      if (!this.isDevFallback) {\n        const buildManifestPath = buildNodejsLowPriorityPath(\n          '_buildManifest.js',\n          this.buildId\n        )\n        compilation.emitAsset(\n          buildManifestPath,\n          new sources.RawSource(\n            `self.__BUILD_MANIFEST = ${generateClientManifest(\n              assetMap,\n              this.rewrites,\n              this.clientRouterFilters,\n              compiler,\n              compilation\n            )};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`\n          )\n        )\n      }\n    })\n  }\n\n  apply(compiler: webpack.Compiler) {\n    compiler.hooks.make.tap('NextJsBuildManifest', (compilation: any) => {\n      compilation.hooks.processAssets.tap(\n        {\n          name: 'NextJsBuildManifest',\n          stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,\n        },\n        () => {\n          this.createAssets(compiler, compilation)\n        }\n      )\n    })\n    return\n  }\n}\n"],"names":["BuildManifestPlugin","generateClientManifest","getEntrypointFiles","buildNodejsLowPriorityPath","filename","buildId","CLIENT_STATIC_FILES_PATH","assetMap","rewrites","clientRouterFilters","compiler","compilation","compilationSpan","getCompilationSpan","Span","name","genClientManifestSpan","traceChild","traceFn","clientManifest","__rewrites","normalizeRewritesForBuildManifest","__routerFilterStatic","staticFilter","__routerFilterDynamic","dynamicFilter","appDependencies","Set","pages","sortedPageKeys","getSortedRoutes","Object","keys","forEach","page","dependencies","filteredDeps","filter","dep","has","length","sortedPages","devalue","entrypoint","getFiles","file","test","map","replace","constructor","options","isDevFallback","beforeFiles","afterFiles","fallback","appDirEnabled","processRoute","createAssets","Error","createAssetsSpan","entrypoints","polyfillFiles","devFiles","lowPriorityFiles","rootMainFiles","rootMainFilesTree","mainFiles","get","CLIENT_STATIC_FILES_RUNTIME_MAIN","CLIENT_STATIC_FILES_RUNTIME_MAIN_APP","compilationAssets","getAssets","p","endsWith","info","CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL","v","CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH","values","SYSTEM_ENTRYPOINTS","pagePath","getRouteFromEntrypoint","filesForPage","buildManifestPath","ssgManifestPath","push","emitAsset","sources","RawSource","srcEmptySsgManifest","sort","reduce","a","c","buildManifestName","BUILD_MANIFEST","JSON","stringify","MIDDLEWARE_BUILD_MANIFEST","createEdgeRuntimeManifest","apply","hooks","make","tap","processAssets","stage","webpack","Compilation","PROCESS_ASSETS_STAGE_ADDITIONS"],"mappings":";;;;;;;;;;;;;;;;IAmGA,iFAAiF;IACjF,+G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