Repository: vercel/next.js
Pull request: Turbopack: Skip loading webpack plugin

## What?

Don't load the webpack build-manifest-plugin because it imports some webpack pieces.

Closes PACK-5545

Changed files:
filename: packages/next/src/build/webpack/plugins/build-manifest-plugin-utils.ts
patch: @@ -0,0 +1,81 @@
+import type { CustomRoutes, Rewrite } from '../../../lib/load-custom-routes'
+import type { BuildManifest } from '../../../server/get-page-files'
+
+export type ClientBuildManifest = {
+  [key: string]: string[]
+}
+
+// Add the runtime ssg manifest file as a lazy-loaded file dependency.
+// We also stub this file out for development mode (when it is not
+// generated).
+export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
+
+function normalizeRewrite(item: {
+  source: string
+  destination: string
+  has?: any
+}): CustomRoutes['rewrites']['beforeFiles'][0] {
+  return {
+    has: item.has,
+    source: item.source,
+    destination: item.destination,
+  }
+}
+
+export const processRoute = (r: Rewrite) => {
+  const rewrite = { ...r }
+
+  // omit external rewrite destinations since these aren't
+  // handled client-side
+  if (!rewrite?.destination?.startsWith('/')) {
+    delete (rewrite as any).destination
+  }
+  return rewrite
+}
+
+export function normalizeRewritesForBuildManifest(
+  rewrites: CustomRoutes['rewrites']
+): CustomRoutes['rewrites'] {
+  return {
+    afterFiles: rewrites.afterFiles
+      ?.map(processRoute)
+      ?.map((item) => normalizeRewrite(item)),
+    beforeFiles: rewrites.beforeFiles
+      ?.map(processRoute)
+      ?.map((item) => normalizeRewrite(item)),
+    fallback: rewrites.fallback
+      ?.map(processRoute)
+      ?.map((item) => normalizeRewrite(item)),
+  }
+}
+
+export function createEdgeRuntimeManifest(
+  originAssetMap: Partial<BuildManifest>
+): string {
+  const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js']
+
+  const assetMap: Partial<BuildManifest> = {
+    ...originAssetMap,
+    lowPriorityFiles: [],
+  }
+
+  // we use globalThis here because middleware can be node
+  // which doesn't have "self"
+  const manifestDefCode = `globalThis.__BUILD_MANIFEST = ${JSON.stringify(
+    assetMap,
+    null,
+    2
+  )};\n`
+  // edge lowPriorityFiles item: '"/static/" + process.env.__NEXT_BUILD_ID + "/low-priority.js"'.
+  // Since lowPriorityFiles is not fixed and relying on `process.env.__NEXT_BUILD_ID`, we'll produce code creating it dynamically.
+  const lowPriorityFilesCode =
+    `globalThis.__BUILD_MANIFEST.lowPriorityFiles = [\n` +
+    manifestFilenames
+      .map((filename) => {
+        return `"/static/" + process.env.__NEXT_BUILD_ID + "/${filename}",\n`
+      })
+      .join(',') +
+    `\n];`
+
+  return manifestDefCode + lowPriorityFilesCode
+}

filename: packages/next/src/build/webpack/plugins/build-manifest-plugin.ts
patch: @@ -1,5 +1,5 @@
 import type { BloomFilter } from '../../../shared/lib/bloom-filter'
-import type { Rewrite, CustomRoutes } from '../../../lib/load-custom-routes'
+import type { CustomRoutes } from '../../../lib/load-custom-routes'
 import devalue from 'next/dist/compiled/devalue'
 import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
 import {
@@ -19,82 +19,21 @@ import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
 import { getSortedRoutes } from '../../../shared/lib/router/utils'
 import { Span } from '../../../trace'
 import { getCompilationSpan } from '../utils'
+import {
+  createEdgeRuntimeManifest,
+  normalizeRewritesForBuildManifest,
+  processRoute,
+  srcEmptySsgManifest,
+  type ClientBuildManifest,
+} from './build-manifest-plugin-utils'
 
 type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
 
-export type ClientBuildManifest = {
-  [key: string]: string[]
-}
-
-// Add the runtime ssg manifest file as a lazy-loaded file dependency.
-// We also stub this file out for development mode (when it is not
-// generated).
-export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
-
 // nodejs: '/static/<build id>/low-priority.js'
 function buildNodejsLowPriorityPath(filename: string, buildId: string) {
   return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`
 }
 
-export function createEdgeRuntimeManifest(
-  originAssetMap: Partial<BuildManifest>
-): string {
-  const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js']
-
-  const assetMap: Partial<BuildManifest> = {
-    ...originAssetMap,
-    lowPriorityFiles: [],
-  }
-
-  // we use globalThis here because middleware can be node
-  // which doesn't have "self"
-  const manifestDefCode = `globalThis.__BUILD_MANIFEST = ${JSON.stringify(
-    assetMap,
-    null,
-    2
-  )};\n`
-  // edge lowPriorityFiles item: '"/static/" + process.env.__NEXT_BUILD_ID + "/low-priority.js"'.
-  // Since lowPriorityFiles is not fixed and relying on `process.env.__NEXT_BUILD_ID`, we'll produce code creating it dynamically.
-  const lowPriorityFilesCode =
-    `globalThis.__BUILD_MANIFEST.lowPriorityFiles = [\n` +
-    manifestFilenames
-      .map((filename) => {
-        return `"/static/" + process.env.__NEXT_BUILD_ID + "/${filename}",\n`
-      })
-      .join(',') +
-    `\n];`
-
-  return manifestDefCode + lowPriorityFilesCode
-}
-
-function normalizeRewrite(item: {
-  source: string
-  destination: string
-  has?: any
-}): CustomRoutes['rewrites']['beforeFiles'][0] {
-  return {
-    has: item.has,
-    source: item.source,
-    destination: item.destination,
-  }
-}
-
-export function normalizeRewritesForBuildManifest(
-  rewrites: CustomRoutes['rewrites']
-): CustomRoutes['rewrites'] {
-  return {
-    afterFiles: rewrites.afterFiles
-      ?.map(processRoute)
-      ?.map((item) => normalizeRewrite(item)),
-    beforeFiles: rewrites.beforeFiles
-      ?.map(processRoute)
-      ?.map((item) => normalizeRewrite(item)),
-    fallback: rewrites.fallback
-      ?.map(processRoute)
-      ?.map((item) => normalizeRewrite(item)),
-  }
-}
-
 // This function takes the asset map generated in BuildManifestPlugin and creates a
 // reduced version to send to the client.
 export function generateClientManifest(
@@ -161,17 +100,6 @@ export function getEntrypointFiles(entrypoint: any): string[] {
   )
 }
 
-export const processRoute = (r: Rewrite) => {
-  const rewrite = { ...r }
-
-  // omit external rewrite destinations since these aren't
-  // handled client-side
-  if (!rewrite?.destination?.startsWith('/')) {
-    delete (rewrite as any).destination
-  }
-  return rewrite
-}
-
 // This plugin creates a build-manifest.json for all assets that are being output
 // It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
 export default class BuildManifestPlugin {

filename: packages/next/src/shared/lib/turbopack/manifest-loader.ts
patch: @@ -33,13 +33,6 @@ import type { SetupOpts } from '../../../server/lib/router-utils/setup-dev-bundl
 import { deleteCache } from '../../../server/dev/require-cache'
 import { writeFileAtomic } from '../../../lib/fs/write-atomic'
 import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites'
-import {
-  type ClientBuildManifest,
-  normalizeRewritesForBuildManifest,
-  srcEmptySsgManifest,
-  processRoute,
-  createEdgeRuntimeManifest,
-} from '../../../build/webpack/plugins/build-manifest-plugin'
 import getAssetPathFromRoute from '../router/utils/get-asset-path-from-route'
 import { getEntryKey, type EntryKey } from './entry-key'
 import type { CustomRoutes } from '../../../lib/load-custom-routes'
@@ -53,6 +46,13 @@ import {
 import { tryToParsePath } from '../../../lib/try-to-parse-path'
 import { safePathToRegexp } from '../router/utils/route-match-utils'
 import type { Entrypoints } from '../../../build/swc/types'
+import {
+  normalizeRewritesForBuildManifest,
+  type ClientBuildManifest,
+  srcEmptySsgManifest,
+  processRoute,
+  createEdgeRuntimeManifest,
+} from '../../../build/webpack/plugins/build-manifest-plugin-utils'
 
 interface InstrumentationDefinition {
   files: string[]