From 21fea2ce619cd2702b0ceaee9f160a5f34ccbaf3 Mon Sep 17 00:00:00 2001 From: Alex Castle Date: Tue, 4 Feb 2020 11:22:26 -0800 Subject: [PATCH] Modify splitChunksPlugin to give shared CSS chunks different names (#10408) * Modify splitChunksPlugin to give shared CSS chunks different names * fix lint Co-authored-by: Joe Haddad --- packages/next/build/webpack-config.ts | 42 +++++++++++++++------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 1f251032242dd..12aedaf210d43 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -311,6 +311,17 @@ export default async function getBaseWebpackConfig( const devtool = dev ? 'cheap-module-source-map' : false + const isModuleCSS = (module: { type: string }): boolean => { + return ( + // mini-css-extract-plugin + module.type === `css/mini-extract` || + // extract-css-chunks-webpack-plugin (old) + module.type === `css/extract-chunks` || + // extract-css-chunks-webpack-plugin (new) + module.type === `css/extract-css-chunks` + ) + } + // Contains various versions of the Webpack SplitChunksPlugin used in different build types const splitChunksConfigs: { [propName: string]: webpack.Options.SplitChunksOptions @@ -368,14 +379,7 @@ export default async function getBaseWebpackConfig( updateHash: (hash: crypto.Hash) => void }): string { const hash = crypto.createHash('sha1') - if ( - // mini-css-extract-plugin - module.type === `css/mini-extract` || - // extract-css-chunks-webpack-plugin (old) - module.type === `css/extract-chunks` || - // extract-css-chunks-webpack-plugin (new) - module.type === `css/extract-css-chunks` - ) { + if (isModuleCSS(module)) { module.updateHash(hash) } else { if (!module.libIdent) { @@ -400,17 +404,19 @@ export default async function getBaseWebpackConfig( }, shared: { name(module, chunks) { - return crypto - .createHash('sha1') - .update( - chunks.reduce( - (acc: string, chunk: webpack.compilation.Chunk) => { - return acc + chunk.name - }, - '' + return ( + crypto + .createHash('sha1') + .update( + chunks.reduce( + (acc: string, chunk: webpack.compilation.Chunk) => { + return acc + chunk.name + }, + '' + ) ) - ) - .digest('hex') + .digest('hex') + (isModuleCSS(module) ? '_CSS' : '') + ) }, priority: 10, minChunks: 2,