时间:2021-07-01 10:21:17 帮助过:17人阅读
优化对比 :
未优化前:index.html引入一个main.js文件,体积2M以上。
优化后入:index.html引入main.js、commons.js、charts.js、other.js。以达到将main.js平分目的。每个文件控制300k以内.(如果高兴100k也没问题)
用到的一堆库及工具:
vue、webpack、babel、highcharts、echarts、jquery、html2canvas******此去省略若干m代码
问题:
开发环境用webpack后发现单个js文件5m。
生产环境借助vue-cli的webpack配置,减少到2m。
解决方案:
搜索各种解决方案:require.ensure、require依赖、多entry、commonsChunkPlugin****此去省力若干方案
网络类似下边这种上解决方案太多了,但是都达不到预期效果
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
}相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
vue的select内置组件使用步骤详解
React使用时避免重渲染
以上就是如何处理webpack打包体积大文件的详细内容,更多请关注Gxl网其它相关文章!