You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Injection Tokens are not tree-shaken because the constructor is considered impure by esbuild #31270
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
If an injection token is defined as const TOKEN = new InjectionToken('desc'), then it will not be tree-shaken from the production bundle, because esbuild considers the constructor to have side-effects.
This means that if it uses a factory function, the items that factory function references are also included.
This can cause large services to be included from a library even if they are unused because they are the default implementation of a token.
This can be mitigated by marking the token definition with /* @__PURE__ */ like so:
I believe this is something that should be fixed in either @angular/core or @angular/build, by marking the InjectionToken constructor as pure: https://esbuild.github.io/api/#pure.
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run ng version)
@angular/build includes logic to make these tree-shakable by adding the /* @__PURE__ */ comment. Could you provide a reproduction case where they are not being tree-shaken?
The application code is not the part that is considered for side-effects by esbuild, it's the InjectionToken constructor. Esbuild doesn't know whether new InjectionToken() has side effects, so it retains that code. Only the implementation of the constructor knows that for sure.
Exporting injection tokens is very common for libraries, so retaining them has an impact on the bundle size, especially when the tokens use a factory function which references large classes.
Activity
alan-agius4 commentedon Sep 22, 2025
@angular/buildincludes logic to make these tree-shakable by adding the/* @__PURE__ */comment. Could you provide a reproduction case where they are not being tree-shaken?eblocha commentedon Sep 22, 2025
I was able to reproduce it by setting up a new application with
ng new, then changing main.ts to the following:After running
ng build, the main chunk contains the token (I formatted the file):If I add
/* @__PURE__ */, then this token is not present.I can try to create a repo after I finish work today.
alan-agius4 commentedon Sep 22, 2025
In this case this is working as intended. As we cannot assume that code written by developers is pure. Same thing if you had
const foo = new Array();Only code from
@angular/packages is annotated automatically.eblocha commentedon Sep 22, 2025
The application code is not the part that is considered for side-effects by esbuild, it's the InjectionToken constructor. Esbuild doesn't know whether
new InjectionToken()has side effects, so it retains that code. Only the implementation of the constructor knows that for sure.Exporting injection tokens is very common for libraries, so retaining them has an impact on the bundle size, especially when the tokens use a factory function which references large classes.
fix(@angular/build): mark `InjectionToken` as pure for improved tree-…
fix(@angular-devkit/build-angular): mark `InjectionToken` as pure for…
fix(@angular/build): mark `InjectionToken` as pure for improved tree-…
fix(@angular-devkit/build-angular): mark `InjectionToken` as pure for…
fix(@angular/build): mark `InjectionToken` as pure for improved tree-…
fix(@angular-devkit/build-angular): mark `InjectionToken` as pure for…
angular-automatic-lock-bot commentedon Oct 26, 2025
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.