Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4 - create vendor chunk

In a webpack 3 configuration I would use the code below to create separate vendor.js chunk:

entry: {     client: ['./client.js'],     vendor: ['babel-polyfill', 'react', 'react-dom', 'redux'], },  output: {   filename: '[name].[chunkhash].bundle.js',   path: '../dist',   chunkFilename: '[name].[chunkhash].bundle.js',   publicPath: '/', },  plugins: [     new webpack.HashedModuleIdsPlugin(),     new webpack.optimize.CommonsChunkPlugin({       name: 'vendor',     }),     new webpack.optimize.CommonsChunkPlugin({       name: 'runtime',     }), ], 

With all the changes I'm not sure how to do it with Webpack 4. I know that CommonChunksPlugin was removed, so there is a different way to achieve that. I've also read this tutorial but I'm still not sure about extracting runtime chunk and properly defining output property.

EDIT: Unfortunately, I was experiencing issues with the most popular answer here. Check out my answer.

like image 999
Tomasz Mularczyk Avatar asked Feb 26 '18 10:02

Tomasz Mularczyk


People also ask

What is vendor main chunk?

The chunk-vendors. js , as its name says, is a bundle for all the modules that are not your own, but from other parties. They are called third-party modules, or vendor modules. Oftentimes, it means (only and) all the modules coming from the /node_modules directory of your project.

What is splitChunks in webpack?

splitChunks.If the current chunk contains modules already split out from the main bundle, it will be reused instead of a new one being generated. This can affect the resulting file name of the chunk. webpack.config.js module.

What is better than webpack?

There are some alternatives to Webpack that provide the same functionality as webpack. These alternatives are gulp, babel, parcel, browserify, grunt, npm, and requireJS.

What is code splitting webpack?

Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which can then be loaded on demand or in parallel.


1 Answers

In order to reduce the vendor JS bundle size. We can split the node module packages into different bundle files. I referred this blog for splitting the bulky vendor file generated by Webpack. Gist of that link which I used initially:

optimization: {   runtimeChunk: 'single',   splitChunks: {     chunks: 'all',     maxInitialRequests: Infinity,     minSize: 0,     cacheGroups: {       vendor: {         test: /[\\/]node_modules[\\/]/,         name(module) {           // get the name. E.g. node_modules/packageName/not/this/part.js           // or node_modules/packageName           const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];            // npm package names are URL-safe, but some servers don't like @ symbols           return `npm.${packageName.replace('@', '')}`;         },       },     },   }, } 

If one wants to group multiple packages and chunk then into different bundles then refer following gist.

optimization: {   runtimeChunk: 'single',   splitChunks: {     chunks: 'all',     maxInitialRequests: Infinity,     minSize: 0,     cacheGroups: {       reactVendor: {         test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,         name: "reactvendor"       },       utilityVendor: {         test: /[\\/]node_modules[\\/](lodash|moment|moment-timezone)[\\/]/,         name: "utilityVendor"       },       bootstrapVendor: {         test: /[\\/]node_modules[\\/](react-bootstrap)[\\/]/,         name: "bootstrapVendor"       },       vendor: {         test: /[\\/]node_modules[\\/](!react-bootstrap)(!lodash)(!moment)(!moment-timezone)[\\/]/,         name: "vendor"       },     },   }, } 
like image 112
swapnil2993 Avatar answered Sep 30 '22 22:09

swapnil2993