I just finished updating my react app from webpack 1 to webpack 2. However, My bundle size increased by ~30Kb. I was hoping the bundle size would decrease , How can i confirm that tree shaking worked. And why the increase?
2 Answers
TL;DR: As of version 2.3, webpack doesn't have any tree shaking. It merely uses UglifyJS to remove unused code.
First we must define what is tree shaking.
Stackoverflow defines it as "dead code elimination algorithm for modern javascript".
Webpack clarifies that it relies on ES2015 module import/export for the static structure of its module system.
Rollup (which originally popularized the term) also has a similar explanation.
So we can deduce a specific definition: static exclusion of unused ES module exports.
Now, let's see which stages of transformation each module usually has:
- babel-loader is fed an entry point which is a javascript file in some module format. Babel can either transform it to another module format of leave it as is (
module: false
) - webpack will statically parse the file and find imported modules (using some sort of regexp)
- webpack will either transform the module format (if babel doesn't tranform it already) or add some wrappers (for commonjs modules)
- imported module becomes an entry point and goes to babel-loader
- after all modules are loaded and transformed, uglify will process the result bunle and remove unused code (
unused: true
)
Now, we can see that while uglify can remove unused exports it doesn't actually rely on ES module syntax. It's just a general purpose dead code elimination so it can't be defined as "Tree shaking".
So how can we confirm whether webpack has tree shaking?
- First of all, all the code must be in ES module format.
- As it was already mentioned in another answer we must disable Uglify.
- We also must disable babel's module transformation as we can't know whether a module is used at that phase.
And now if webpack actually implements a tree shaking algorithm we can confirm it by looking at the bundle size of this entry point:
import { keyBy } from 'lodash-es'; // lodash is in ES module format
console.log(keyBy([{ key: 'value' }], 'key'));
If webpack does have tree shaking the result should be tens of kilobytes. If it doesn't it would be half a megabyte or more.

- 31,770
- 10
- 98
- 102
There's a few steps you can take:
- turn off minification/uglify
- insert comments into some of the functions you know are unused
- check the output to see if those comments are there or not
Webpack can also show you the size of each import module/package. For example, at work we have a bundle that includes both lodash and underscore because one of the libraries we're using requires each of them; webpack clearly shows how many kilobytes each package adds:
You should be able to see from this where the increase in size is coming from.
-
2i followed the webpack migration guide to update. So for step 1 I should just set **minimize: false; in theLoaderOptionsPlugin** or just remove uglify from production env plugin and for step 2, Do i search the bundled js file to see if the comments were included? – jasan Feb 13 '17 at 19:09