Lower you expectation for Webpack’s Tree Shaking

Recently I upgrade my project from webpack1 to webpack3, one of the main highlight feature for me is Tree Shaking. Depnding on the static structure of ES6 module system, webpack is now able to get rid of unused modules from your build.

That sounds very cool, but after I finished my upgrade and rebuilt my project, the decrease of the file sizes did not reflect my expectation.

Why? Did I not use it right, or is it actually not working?

So then I created this test project which is used to test what impact will have on your code with webpack’s new tree shaking feature and what configuration needs to be made to enable the feature.

Test Example Code

I created two independent directories for test, one is for webpack1 the other is for webpack3.

In both directory, I have test code using lodash as well as my own code.

The examples are:

  • partial import my own module: import { a } from './lib
  • import whole lodash: import lodash from 'lodash
  • import named lodash module: import { uniq } from 'lodash
  • import single lodash module: import uniq from 'lodash/uniq

Notice for all lodash example, we only use the uniq function.

Results

Check out the results for webpack1 and webpack2:

Webpack1

The dist file for partialImportTest.js (the unused module b is added in the dist file as well):

Webpack2

The dist file for partialImportTest.js (the unused module b is not added in the dist file):

Result Conclusion

  • Tree shaking does not work with lodash (lodash-es as well, I have tried, and the file size is twice bigger)
  • Tree shaking works for my own code, webpack only imports what is used
  • Webpack1 seems to generate smaller file.

Configuration Needed For Tree Shaking

The documentation for Tree Shaking is actually not complete, and I won’t blame you if you can’t make tree shaking work by following its documentation.

So for the tree shaking to work, there’s actually two dependencies:

  • disable babel-loader’s es6 module transformation, let webpack take over it (the new ability for webpack is that it now understands es6 module)
  • enable uglifyJS compression. Actually UglifyJS is the one which gets rid of unused code.

To disable your babel-loader’s es6 module transformation, update your options for babel-loader like below:

And add uglifyJS:

You will be ready to shake trees!

Final Conclusion

As soon as Tree Shaking does not work effectively with libraries like lodash, the major benefits we can get may actually from downsizing our own code by getting rid of unused dead code.

However, for most project caring very much about its performance, they might already have been using tools like ESLint – Pluggable JavaScript linter to help them avoid bad practices during their development.

So I doubt how much impact will Tree Shaking have on serious projects, which is why I think you should lower your expectation, it is not a cure for your file size related performance issues, though it is good to have this extra feature.

Instead of relying on tree shaking, following best practices like alway import modules you actually use is a better way to keep your dist file small.

Do:

Don’t do:

Leave a Comment

Your email address will not be published. Required fields are marked *