我正在尝试使用 Webpack 4 Split Chunks Plugin 创建多个供应商捆绑包。在这种情况下,我想为 react/react-dom 创建一个块,为 react-router/react-router-dom 创建一个块。
当cacheGroups
仅包含react
andvendor
时,构建按预期工作。捆绑输出是:
- index
- react
- runtime
- vendors
同样,如果我只有 cacheGroupsrouter
并且vendor
它按预期工作。输出是:
- index
- router
- runtime
- vendors
在任何一种情况下,当创建块时,检查都会显示正确的代码react
或router
在它们各自的情况下。
但是......当我同时包含两者时它不起作用 - 在这种情况下,只有router
块被创建,并且react
代码被推送到索引(src)包中。
我怀疑正则表达式模式中有问题导致先前的缓存组失效?任何帮助表示赞赏。
这是我的 splitChunks 的 webpack 配置:
splitChunks: {
cacheGroups: {
react: {
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
name: 'react',
chunks: 'all'
},
router: {
test: /[\\/]node_modules[\\/](react-router|react-router-dom)[\\/]/,
name: 'router',
chunks: 'all'
},
vendor: {
test(mod) {
// exclude anything outside node modules
if (!mod.context.includes('node_modules')) {
return false;
}
// exclude react and react-dom
if (/[\\/]node_modules[\\/](react|react-dom)[\\/]/.test(mod.context)) {
return false;
}
// exclude react-router and react-router-dom
if (/[\\/]node_modules[\\/](react-router|react-router-dom)[\\/]/.test(mod.context)) {
return false;
}
// return all other node modules
return true;
},
name: 'vendors',
chunks: 'all'
}
}
}