Build tree shaking do not remove every unused code
See original GitHub issueWhich @angular/* package(s) are the source of the bug?
compiler-cli
Is this a regression?
Yes
Description
I have generated a new project - ng create test-ts
- and added just a few unused variables and only one that is used.
(in grey, unused variables):
Extract from generated main.js:
The kpiResourceMap
is in the final bundle (and unassigned) but never used in the app, so it should be removed.
Please provide a link to a minimal reproduction of the bug
https://github.com/MYRE-CORP/test-ts
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in
Angular CLI: 12.2.4
Node: 14.15.3
Package Manager: npm 7.21.0
OS: Ubuntu
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Tree shaking and code splitting in webpack - LogRocket Blog
Tree shaking, also known as dead code elimination, is the practice of removing unused code in your production build. It's important to ship...
Read more >Tree-shaking not eliminating some dead code #2672 - GitHub
Some of the builds contain apparenty unused functions. For example I don't understand why I'm finding var union = unionBy(identity); in build/ ...
Read more >Tree Shaking - How to Clean up Your JavaScript - KeyCDN
Tree shaking is a strategy web developers use to create leaner JavaScript bundles by getting rid of unused code.
Read more >Tree Shaking - webpack
All the code noted above does not contain side effects, so we can mark the property as false to inform webpack that it...
Read more >Tree-shaking versus dead code elimination | by Rich Harris
It doesn't currently remove things like unused methods from objects that are used, and there are times when it is forced to assume...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This case is related to dead code elimination safety. In the above code,
new Map
is at the module level which will cause the expression to be executed at module evaluation time. Such code that can possibility have side effects cannot be safely removed since removal may alter the behavior of the application at runtime. Since theMap
constructor has the possibility of throwing an exception it has possible side effects. Determining whether theMap
construction is safe at build time is a non-trivial analysis effort for JavaScript and one which current optimizers do not perform. However, if the expression has been manually reviewed to be side effect free, the expression can be annotated to provide a hint to the optimizer that it is safe to be removed. This should be possible by adding a pure annotation as a leading comment similar to the following:const kpiResourceMap = /*#__PURE__*/ new Map(....)
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.