question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build tree shaking do not remove every unused code

See original GitHub issue

Which @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):

image

Extract from generated main.js:

image

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
clydincommented, Sep 8, 2021

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 the Map constructor has the possibility of throwing an exception it has possible side effects. Determining whether the Map 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(....)

0reactions
angular-automatic-lock-bot[bot]commented, Oct 22, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found