欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解webpack 打包文件體積過大解決方案(code splitting)

 更新時間:2018年04月10日 08:36:56   作者:懦酷  
這篇文章主要介紹了webpack 打包文件體積過大解決方案(code splitting),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

優(yōu)化對比 :

  未優(yōu)化前:index.html引入一個main.js文件,體積2M以上。

  優(yōu)化后入:index.html引入main.js、commons.js、charts.js、other.js。以達到將main.js平分目的。每個文件控制300k以內.(如果高興100k也沒問題)

用到的一堆庫及工具:

vue、webpack、babel、highcharts、echarts、jquery、html2canvas******此去省略若干m代碼

問題:

  開發(fā)環(huán)境用webpack后發(fā)現單個js文件5m。

  生產環(huán)境借助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
})  
}

最優(yōu)解決方案:

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 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論