// 需结合webpack-bundle-analyzer来拆解自己项目中的chunk-vendors
// optimization.splitChunks拆分出来的chunk,需要在htmlWebpackPlulgin中的chunks里手动写入
// 才会被引入到index.html中,否则只做了拆分,没引入,页面就会空白
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
function changeHtmlWebpackPluginChunks(config) {
const plugins = config.plugins;
// 这里要和下面的optimization.splitChunks分离出来的对应好,否则无法引入
// 你的项目可能和我这不一样,请按着自己项目进行拆分
const chunkArr = ["runtime","chunk-@dcloudio", "chunk-core-js", "chunk-elliptic", "chunk-bn", "uview-ui", "chunk-pages"];
plugins.forEach((item, index) => {
// 找到HtmlWebpackPlugin
if( item.options
&& Object.prototype.toString.call(item.options) === "[object Object]"
&& Object.hasOwnProperty.call(item.options, 'filename')
&& Object.hasOwnProperty.call(item.options, 'chunks')
&& item.options.filename === "index.html"
) {
const oldChunks = item.options.chunks || [];
// 修改chunks引入
item.options.chunks = [...chunkArr, ...oldChunks];
}
});
}
module.exports = {
configureWebpack: config => {
config.plugins.push(new BundleAnalyzerPlugin());
if (process.env.NODE_ENV === 'production') {
changeHtmlWebpackPluginChunks(config);
}
},
chainWebpack: config => {
config.when(process.env.NODE_ENV === "production", config => {
config.optimization.splitChunks({
chunks: "all", // 必须三选一: "initial"(同步包) | "all"(推荐,同步或异步包) | "async" (默认就是async,异步包)
automaticNameDelimiter: '~', // 打包分隔符
name: function(module, chunks, cacheGroupKey) {
const moduleFileName = module
.identifier()
.split('/')
.reduceRight((item) => item);
const allChunksNames = chunks.map((item) => item.name).join('~');
return `${moduleFileName}`;
},
cacheGroups: {
"uview-ui": { // 把uview-ui拆分出来
name: "uview-ui",
test: /uview-ui/,
chunks: "all",
reuseExistingChunk: true,
enforce: true,
priority: 100,
minChunks: 1,
minSize: 30000,
},
"pages": { // 把pages.json拆分出来
name: "chunk-pages",
test: /pages\.json$/,
chunks: "all",
reuseExistingChunk: true,
enforce: true, // 强行分离
priority: 90, // 打包优先级权重值
minChunks: 1, // 引用超过一次直接打包到chunk-pages中
minSize: 10000,
},
"core-js": {
name: "chunk-core-js",
test: /core-js/,
chunks: "initial",
reuseExistingChunk: true,
enforce: true
},
"@dcloudio": { // vue也在这里面
name: "chunk-@dcloudio",
test: /@dcloudio/,
chunks: "initial",
reuseExistingChunk: true,
enforce: true
},
"elliptic": {
name: "chunk-elliptic",
test: /elliptic/,
chunks: "initial",
reuseExistingChunk: true,
enforce: true
},
"bn": {
name: "chunk-bn",
test: /bn/,
chunks: "initial",
reuseExistingChunk: true,
enforce: true
}
}
});
// 提取公共runtime
config.optimization.runtimeChunk("single");
})
}
}
}
4 个回复
aiplat点com - aiplat.com
不要chunks: "all",
splitChunks.chunks要改成 chunks: 'async',
splitChunks: {
chunks: 'async',
minSize: 20000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: Infinity,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendor: {
test: /[\/]node_modules[\/]/,
name(module) {
const packageName = module.context.match(/[\/]node_modules\/([\/]|$)/)[1];
return
npm.${packageName.replace('@', '')}
;},
priority: 9,
minChunks: 1,
reuseExistingChunk: true,
enforce: true,
// chunks: 'all',
},
commons: {
name: 'commons',
priority: 8,
minChunks: 2,
reuseExistingChunk: true,
enforce: true,
chunks: 'all',
},
default: {
minChunks: 2,
priority: 7,
}
}
}
4***@qq.com
今天回复我了吗 - uniapp 粉丝条
同样没有效果
aiplat点com - aiplat.com
解决了?