New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webpack Tree-shaking fails with static properties but not static getters #8308
Comments
|
The minimizer doesn't detect this as dead code. You can try the terser-webpack-plugin with the Code like this |
|
Thanks, I will give the plugin a look. This is from isolating tree-shaking issues in a larger library as you might expect. Good point on the side-effects, but the "if" seems deterministic enough. Does this mean webpack has no intention of changing this behavior in the future? |
webpack is not doing the Dead-Code-Elimination. It's done by uglify-js2 (webpack 4) or terser (webpack 5). If you want this behavior changed, that's where to look. |
|
Rollup can detect and drop such unused static properties on classes and functions. Terser and Uglify, on the other hand, will never get rid of classes and functions with static properties set in the event there's a side effect. In order to achieve the result you're looking for you'd have to wrap such classes and functions within a pure IIFE as follows to instruct terser/uglify there are no side effects if not referenced: Babel 7 emits its ES5 generated class code within pure IIFEs for this reason. Related Babel discussion to generate ES static class properties within a pure IIFE: |
Bug report
What is the current behavior?
If an ES6 class contains a static getter property but the class itself is un-used, it will correctly be removed from the bundle. So this is tree-shake compatible:
However, if an ES6 class contains a (attach style) static property, and the class is un-used, it will not be removed from the bundle. Tree shaking fails on this case, in other words:
If the current behavior is a bug, please provide the steps to reproduce.
Make a file, app.js with two classes:
and an
init.js:note how we only import
hello, and NOTgoodbye. So we expectgoodbyeto be absent from the resulting bundle.Then build with webpack defaults. If this is uncommented:
The bundle.js will not contain the goodbye class.
However if that one is commented out, and this one is uncommented:
The bundle will include the
goodbyeclass, even though it is never used.What is the expected behavior?
In both cases, different ways of putting static stuff on a class should not prevent the class from being removed from the bundle. Both times the
goodbyeclass should be removed from the bundle, since it is not used or referenced anywhere.Other relevant information:
webpack version: 4.23.1
Node.js version: v9.10.1
Operating System: Windows 10
Additional tools:
The text was updated successfully, but these errors were encountered: