时间:2021-07-01 10:21:17 帮助过:26人阅读
entry:{
main:'xxx.js',
chunks:['c1', 'c2'],
commons:['jquery', 'highcharts', 'echarts','d3', 'xxxxx.js']
}
plugins:{
new commonsChunkPlugin({
name:'commons',
minChunks:2
})
}最优解决方案:
entry:{
main:'xxx.js'
}
plugins:{
new commonsChunkPlugin({
name:'commons',
minChunks:function(module){
// 下边return参考的vue-cli配置
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
}) ,
// 以下才是关键
new commonsChunkPlugin({
name:'charts',
chunks:['commons']
minChunks:function(module){
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0 && ['jquery.js', 'highcharts.js','echarts'].indexOf( module.resource.substr(module.resource.lastIndexOf('/')+1).toLowerCase() ) != -1
)
}
})
// 如果愿意,可以再new 一个commonsChunkPlugin
}以上代码打包出来的 结果 : main.js 、commons.js、charts.js