When configuring Webpack-based React single-page scaffolding, the public dependent library code is packaged into Vender.js, the page logic code is packaged in App.js, and the Webpack-bundle-analyzer analysis finds that two JS contains duplicate code. The optimization method is as follows:
Webpack.config.js modification
(1) Modify the order of the entry file, vender.js as the dependent file should be referenced first
Webpackconfig.entry = { vender: [ ' Babel-polyfill ', ' classnames ', ' react ', ' react-dom ', ' Axios ', ' react-router ', ' react-router-dom ', app: './src/app.jsx ' }
(2) Uglifyjs-webpack-plugin, Commonschunkplugin
Use plug-ins for optimized compression:
Webpackconfig.plugins = (Webpackconfig.plugins | | []). Concat ([ new Webpack. Defineplugin ({ ' process.env ': { NODE_ENV:JSON.stringify (' production ') } }), new Webpack. Loaderoptionsplugin ({ minimize:true }), //remove information such as comments in the packaged code new Uglifyjsplugin ({ Uglifyoptions: { output: { comments:false }, compress:true } }), // Timestamp new Webpack When adding a packaged file . Bannerplugin (bannertxt), //Public Code extraction new Webpack.optimize.CommonsChunkPlugin ({ name: "Vender", Minchunks:infinity }) ]);
Webpack-bundle-analyzer Configuration method
Webpack code Optimization Compression method