Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buildmanifest for webpack 5 splitchunks #21325

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 32 additions & 30 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Expand Up @@ -70,17 +70,6 @@ function isJsFile(file: string): boolean {
return !file.endsWith('.hot-update.js') && file.endsWith('.js')
}

function getFilesArray(files: any) {
if (!files) {
return []
}
if (isWebpack5) {
return Array.from(files)
}

return files
}

// 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 All @@ -106,8 +95,6 @@ export default class BuildManifestPlugin {
return tracer.withSpan(spans.get(compiler), () => {
const span = tracer.startSpan('NextJsBuildManifest-createassets')
return traceFn(span, () => {
const namedChunks: Map<string, webpack.compilation.Chunk> =
compilation.namedChunks
const assetMap: DeepMutable<BuildManifest> = {
polyfillFiles: [],
devFiles: [],
Expand All @@ -129,27 +116,42 @@ export default class BuildManifestPlugin {
}
}

const mainJsChunk = namedChunks.get(CLIENT_STATIC_FILES_RUNTIME_MAIN)

const mainJsFiles: string[] = getFilesArray(mainJsChunk?.files).filter(
isJsFile
)

const polyfillChunk = namedChunks.get(
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS
)

const mainJsFiles = compilation.entrypoints
.get(CLIENT_STATIC_FILES_RUNTIME_MAIN)
.getFiles()
.filter(isJsFile)
// Create a separate entry for polyfills
assetMap.polyfillFiles = getFilesArray(polyfillChunk?.files).filter(
isJsFile
)
assetMap.polyfillFiles = compilation.entrypoints
.get(CLIENT_STATIC_FILES_RUNTIME_POLYFILLS)
.getFiles()
.filter((file: string) => {
if (!isJsFile(file)) {
return false
}

// webpack runtime is already available for polyfills on the page
if (file.includes('/webpack')) {
return false
}
return true
})

const reactRefreshChunk = namedChunks.get(
const reactRefreshChunk = compilation.entrypoints.get(
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH
)
assetMap.devFiles = getFilesArray(reactRefreshChunk?.files).filter(
isJsFile
)
assetMap.devFiles = reactRefreshChunk
? reactRefreshChunk.getFiles().filter((file: string) => {
if (!isJsFile(file)) {
return false
}

// webpack runtime is already available for polyfills on the page
if (file.includes('/webpack')) {
return false
}
return true
})
: []

for (const entrypoint of compilation.entrypoints.values()) {
const isAmpRuntime =
Expand Down