KEMBAR78
Turbopack: Skip loading webpack plugin by timneutkens · Pull Request #84125 · vercel/next.js · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
88 changes: 8 additions & 80 deletions packages/next/src/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions packages/next/src/shared/lib/turbopack/manifest-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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[]
Expand Down
Loading