Created
October 28, 2022 17:39
-
-
Save n8jadams/b62738069e8d9089ab6058ad9cb0579f to your computer and use it in GitHub Desktop.
Split up vendors into smaller chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The best way to split up your npm deps into smaller chunks | |
module.exports = (env) => { | |
const config = { | |
// ... rest of config options | |
optimization: { | |
runtimeChunk: "single", | |
splitChunks: { | |
cacheGroups: { | |
vendors: { | |
test: /[\\/]node_modules[\\/]/, | |
name(module) { | |
const [_, afterNodeModules] = module.context.split("/node_modules/") | |
if (!afterNodeModules) { | |
return false | |
} | |
const packageName = afterNodeModules.split("/")[0] | |
const moduleFileName = module | |
.identifier() | |
.split("/") | |
.reduceRight((item) => item) | |
// npm package names are URL-safe, but some servers don't like @ symbols | |
return `vendors.${packageName.replace("@", "")}.${moduleFileName}` | |
}, | |
chunks: "all" | |
} | |
} | |
} | |
} | |
} | |
return config | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment