Webpack 2 Tree Shaking Configuration
Tree Shaking, a modern dead code elimination algorithm for ECMAScript 2015+ is one of the most anticipated features in the upcoming Webpack 2. By eliminating unused ES6 exports, Webpack can further help the minification process to get your bundle much smaller than ever before.
In the sample code below, I will show how my new Webpack 2 configuration helped cut 28% out of my already optimized Webpack 1 project.
CommonJS Out, Native Imports In
Webpack 2 now knows how to work out the imports natively, without having to convert them to CommonJS modules. This is very important because CommonJS exports leaves references alive, meaning everything you declared in exports will have to be included in your final build.
To cut a long story short, you need to slightly adjust your .babelrc configuration. Instead of es2015 preset, you’ll need a new one named ‘babel-preset-es2015-native-modules’. This preset tells Babel to skip CommonJS module conversion during the transpilation process.
You would install this preset with npm.
npm i babel-preset-es2015-native-modules --save-dev
Your new .babelrc will look like this:
Or, if you use React
“presets”: [“es2015-native-modules”, “react”]
Your Babel configuration is now ready. Essentially, this was all it takes for tree shaking to work in Webpack 2.
Now, let’s shift focus on some of the important updates in Webpack 2 configuration for those who want to convert from v1.x.
Migrating from Webpack 1 to Webpack 2
Tree shaking works out of the box in Webpack 2. If you have an existing project, chances are you will want to upgrade your old configuration. Alternatively, some of you might use seed projects that were meant to work with Webpack 1.x. Let’s review just a few of the breaking changes that will likely require you to update some of the settings.
- Loaders configuration
There have been minor improvements in how you define loader configuration. The previous GET-like syntax has been replaced with JSON-style notation.
Let’s observe this Webpack 1.x example:
The Webpack 2.x version of the same would look like this:
Notice how query parameters are now neatly stacked as key-value pairs.
In Webpack 2, resolvers from `root`, `modulesDirectories`, and `fallback` settings will merge into a single property — `modules`. Here is how we can resolve file and module locations in Webpack 2:
You can specify a number of directories in `modules`, but make sure not to forget `node_modules` or npm package dependencies will fail to load.
3. Uglify Plugin Changes
The UglifyJsPlugin will no longer put loaders into minimize mode, and the debug option has been deprecated. These options are simply moved into a new plugin, LoaderOptionsPlugin, for separation of concerns reasons. Use it as such:
4. Other changes
Tobias Koppers aka Sokra published a very useful What’s new in Webpack 2 gist, which I highly recommend looking at.
Take a look at this sample React + Redux + Webpack 2 sample app that leverages all of the important configuration settings that will make your production build smaller than ever. Comparing to the original Webpack 1.x version of the same app, the size went down by a whopping 28%.
Take a look yourself and share this with your network. Someone will surely want to optimize their webpack build using the new tree shaking technology.
If you liked this article, please hit the recommend (♥️) button below. Want more like this? Follow Modus Create: Front End Development.
With more than 10 years of experience in Scrum and Scrum-like teams, I finally decided to get certified as a…medium.com