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

improve splitChunks config for webpack 5 #21208

Merged
merged 2 commits into from Jan 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 30 additions & 27 deletions packages/next/build/webpack-config.ts
Expand Up @@ -434,17 +434,11 @@ export default async function getBaseWebpackConfig(
cacheGroups: {
default: false,
vendors: false,
// In webpack 5 vendors was renamed to defaultVendors
defaultVendors: false,
},
},
prodGranular: {
chunks: 'all',
cacheGroups: {
default: false,
vendors: false,
// In webpack 5 vendors was renamed to defaultVendors
defaultVendors: false,
framework: {
chunks: 'all',
name: 'framework',
Expand All @@ -458,10 +452,13 @@ export default async function getBaseWebpackConfig(
enforce: true,
},
lib: {
test(module: { size: Function; identifier: Function }): boolean {
test(module: {
size: Function
nameForCondition: Function
}): boolean {
return (
module.size() > 160000 &&
/node_modules[/\\]/.test(module.identifier())
/node_modules[/\\]/.test(module.nameForCondition() || '')
)
},
name(module: {
Expand Down Expand Up @@ -493,26 +490,32 @@ export default async function getBaseWebpackConfig(
minChunks: totalPages,
priority: 20,
},
shared: {
name(module, chunks) {
return (
crypto
.createHash('sha1')
.update(
chunks.reduce(
(acc: string, chunk: webpack.compilation.Chunk) => {
return acc + chunk.name
},
''
...(isWebpack5
? undefined
: {
default: false,
vendors: false,
shared: {
name(module, chunks) {
return (
crypto
.createHash('sha1')
.update(
chunks.reduce(
(acc: string, chunk: webpack.compilation.Chunk) => {
return acc + chunk.name
},
''
)
)
.digest('hex') + (isModuleCSS(module) ? '_CSS' : '')
)
)
.digest('hex') + (isModuleCSS(module) ? '_CSS' : '')
)
},
priority: 10,
minChunks: 2,
reuseExistingChunk: true,
},
},
priority: 10,
minChunks: 2,
reuseExistingChunk: true,
},
}),
},
maxInitialRequests: 25,
minSize: 20000,
Expand Down
34 changes: 34 additions & 0 deletions test/.stats-app/stats-config.js
Expand Up @@ -135,5 +135,39 @@ module.exports = {
},
],
},
{
title: 'Webpack 5 Mode',
diff: 'onOutputChange',
renames,
configFiles: [
{
path: 'next.config.js',
content: `
module.exports = {
generateBuildId: () => 'BUILD_ID',
future: {
webpack5: true
}
}
`,
},
],
filesToTrack: clientGlobs,
// will be output to fetched-pages/${pathname}.html
pagesToFetch: [
'http://localhost:$PORT/',
'http://localhost:$PORT/link',
'http://localhost:$PORT/withRouter',
],
pagesToBench: [
'http://localhost:$PORT/',
'http://localhost:$PORT/error-in-render',
],
benchOptions: {
reqTimeout: 60,
concurrency: 50,
numRequests: 2500,
},
},
],
}