Webpack Package File size solution (code splitting)

Source: Internet
Author: User

Optimization comparison:

Before optimization: index.html introduces a main.js file that is more than 2M in volume.

Optimized after: Index.html introduced Main.js, Commons.js, Charts.js, Other.js. To achieve the purpose of dividing the main.js equally. Within 300k per file control. (If happy 100k is no problem)

A bunch of libraries and tools to use:

Vue, Webpack, Babel, Highcharts, Echarts, jquery, html2canvas****** this to omit some m code

Problem:

Development environment with Webpack after the discovery of a single JS file 5m.

The production environment is reduced to 2m thanks to the webpack configuration of the vue-cli.

Solution:

Search Various solutions: require.ensure, require dependent, multi-entry, commonschunkplugin**** this effort to save some programs

The network is similar to the bottom of the solution is too many, but not to achieve the desired effect

entry:{      main: ' Xxx.js ',      chunks:[' C1 ', ' C2 '],     commons:[' jquery ', ' highcharts ', ' echarts ', ' D3 ', ' Xxxxx.js ']      }
Plugins:{new Commonschunkplugin ({name: ' Commons ', Minchunks:2})        }

Optimal solution:

entry:{main: ' Xxx.js '}plugins:{new Commonschunkplugin ({name: ' Commons ', Minchunks:function (module) {// Below return reference VUE-CLI Configuration//any required modules inside Node_modules is extracted to vendor return (Modu Le.resource &&/\.js$/.test (module.resource) && module.resource.indexOf (path . Join (__dirname, '.. /node_modules ') (= = 0)}),//The following is the key 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)   }})
If you want, you can have a new commonschunkplugin}

The result of the above code package:main.js, Commons.js, Charts.js

Webpack Package File size solution (code splitting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.