webpack: Tree shaking not working with webpack 4.5

Do you want to request a feature or report a bug?

I’d like to report a bug

What is the current behavior?

Tree shaking does not appear to be working. Dead code should be identified in the output bundle with a comment such as

/* unused harmony export _functionname_ */

If the current behavior is a bug, please provide the steps to reproduce.

Refer to the basic configuration in https://github.com/cdhgee/webpack-tree-shaking.

  1. Run npm install
  2. Run npm run build
  3. Examine the output in dist/bundle.js. Around line 83, the exports from another-module.js should be shown.

What is the expected behavior?

Functions c and d are not used anywhere in the project and should be identified with a comment such as

/* unused harmony export c */

Because these comments are missing, I conclude that either (a) tree shaking is not running, or (b) tree shaking is running but is not identifying c and d as dead code.

If this is a feature request, what is motivation or use case for changing the behavior?

n/a

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

NodeJS version: 9.11.1 (64-bit) Webpack version: 4.5.0 OS: Windows 10 1709 (64-bit)

These versions are all the most recent available as of today (2018-04-08).

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 21 (2 by maintainers)

Most upvoted comments

@zhongds I follow the Tree Shaking guide document and do exactly what it inform. My code work like a charm without UglifyJsPlugin.

@sokra It is only enable in production mode via a plugin or something internal? Want to document it in Tree shaking guide https://webpack.js.org/guides/tree-shaking/

For mine I do this:

{ "presets": ["env", { "modules": false }] }

(Note that the env preset is in a tuple with the options object.)

@GuillaumeCisco tree shaking does not work in development mode per the docs here

@webpack/documentation-team the updated Tree Shaking instructions worked perfectly for me, big thanks to whoever did that update!

In my case, I had a linked npm package containing some big modules that should’ve been shook as they weren’t used. Adding "sideEffects": false to the package’s package.json file cut the production build size for the main project in half (shaved hundreds of kbs). Double checked the build stats on webpack visualizer and tested the output bundles.

🎉 🎉 🎉

Also, check if you’re using babel-loader. That was my problem, it was transpiling ES6 modules into CommonJS, thus breaking tree shaking. It could be fixed by passing in { modules: false } into your babelrc, but depending on how you bundle your code that may break older browsers that don’t support ES6 modules…

@hanyulo like a charm ? So you solved your problem?

`“@babel/core”: “^7.0.0-rc.1”,

"@babel/preset-env": "^7.0.0-rc.1",

"@babel/preset-react": "^7.0.0-rc.1",

"babel-core": "^6.26.3",

"babel-loader": "^8.0.0-beta.4",

"babel-plugin-import": "^1.8.0",

"babel-preset-env": "^1.7.0",`

with these devDependencies, how should i set my .babelrc { "presets": [ [ "@babel/preset-env", { "targets": { "node": "current" } } ], "@babel/preset-react" ] }

For mine I do this: { “presets”: [“env”, { “modules”: false }] } – @AndrewSouthpaw

Thanks for this hint! I was facing the same problem and setting modules to false in my .babelrc did fix it!

webpack: 4.5.0. I also have the same problem. webpack --mode production take effects, but when I use mode in webpack.config.js, tree-shaking do not work (I had setted sideEffects on package.json).

I followed the steps in Tree Shaking Documentation:

  • Use ES2015 module syntax (i.e. import and export).
  • Add a “sideEffects” entry to your project’s package.json file.
  • Include a minifier that supports dead code removal (e.g. the UglifyJSPlugin).

what’s the problem? @Legends

Tried your repo @cdhgee and it works fine. Note that tree shaking is only enabled in production mode:

$ npm run build -- --mode production --display-used-exports
Hash: c665586af0bb888f4551
Version: webpack 4.5.0
Time: 529ms
Built at: 2018-4-9 09:46:10
    Asset       Size  Chunks             Chunk Names
bundle.js  599 bytes       0  [emitted]  main
Entrypoint main = bundle.js
   [0] ./index.js + 1 modules 264 bytes {0} [built]
       | ./index.js 48 bytes [built]
       | ./another-module.js 216 bytes [built]
       |    [only some exports used: a]