Tree shaking with webpack settings

Webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset. Its such a great tool for web development. One of the great feature it offers is the ability to tree shaking the javascript and CSS bundles with the right plugins.

For js tree shaking. I have used UglifyJsPlugin. Below is how I got the plugin works in my webpack.config.js file.

For css tree shaking. I have used PurifyCSSPlugin. Below is how I got the plugin works in my webpack.config.js file.

In order for PurifyCSSPlugin, make sure the above code goes after ExtractTextPlugin. See below

Another nice feature that I love to use in my webpack config is the ability to control how chunks should be sorted before they are included to the html via chunksSortMode option of html-webpack-plugin

To control the order of the chunks, I name them with the number at the front of that name. For example

Any feedback, please leave your comments and I’ll get back to you ASAP.