通过在 Laravel Mix 中的 Extract.js 组件中插入一个 console.log,我发现在我当前的设置中,插件只是将这些行插入到 Webpack 配置的优化字段中。
optimization: {
runtimeChunk: { name: '/js/manifest' },
splitChunks: {
cacheGroups: {},
chunks: 'all',
name: '/js/vendor',
},
},
为了过滤供应商文件中的内容,我必须像这样编辑 splitChunks:
splitChunks: {
cacheGroups: {
vendor: {
// moved name and chunks here
name: 'js/vendor',
chunks: 'all',
// you can pass a RegExp to test, but also a function
test(module/* , chunk */) {
if (module.context) {
// node_modules are needed
const isNodeModule = module.context.includes('node_modules');
// but only specific node_modules
const nodesToBundle = [
'vue',
'lodash',
'axios',
].some(str => module.context.includes(str));
if (isNodeModule && nodesToBundle) {
return true;
}
}
return false;
},
}
},
// removed name and chunks from here
// chunks: 'all',
// name: '/js/vendor',
},
在这些更改之后,我仍然有您遇到的奇怪命名,但是感谢这个GitHub 存储库,我发现我可以通过添加以下内容来删除此行为:
default: false, // disable default groups
vendors: false, // disable vendors group
到 CacheGroups 字段。
所以我完整的 webpackConfig 函数参数是:
.webpackConfig({
output: {
// with other settings
},
optimization: {
runtimeChunk: { name: '/js/manifest' },
splitChunks: {
cacheGroups: {
default: false,
vendors: false,
vendor: {
name: 'js/vendor',
chunks: 'all',
// you can pass a RegExp to test, but also a function
test(module/* , chunk */) {
if (module.context) {
// node_modules are needed
const isNodeModule = module.context.includes('node_modules');
// but only specific node_modules
const nodesToBundle = [
'vue',
'lodash',
'axios',
].some(str => module.context.includes(str));
if (isNodeModule && nodesToBundle) {
return true;
}
}
return false;
},
}
},
},
}
})
请务必同时删除混音文件中的 extract() 调用。