javascript - Webpack Split Chunks Plugin 覆盖 cacheGroups,正则表达式匹配错误?
问题描述
我正在尝试使用 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'
}
}
}
解决方案
尝试对您的配置进行此操作:
optimization: {
splitChunks: {
maxInitialRequests: 4, // This one!
默认值为maxInitialRequests
4,因此如果您有超过 4 个捆绑包,它会以某种方式合并其中的一些。
推荐阅读
- twilio - TwilioError:参与者因身份重复而断开连接
- matlab - 如何在 Matlab 中找到 N 倍卷积的参数?
- git - 检查两个 git 存储库是否相关
- python - 如何从四分之一字符串推断日期时间?
- php - 从另一个表(Laravel)更新3个或更多表的数据?
- typescript - 数组中的打字稿泛型类型丢失
- python - 使用带参数的存储过程将 pandas 数据框插入 sql 数据库
- java - AutoValue 示例:错误:找不到符号类 AutoValue_Animal
- laravel - Laravel 清除文件缓存
- python - 用子列表中的整数替换字符串(所有出现) - python