Skip to content

Conversation

arturovt
Copy link
Contributor

@arturovt arturovt commented Aug 24, 2025

Previously, prependTokenToDependencyPath was a top-level function used only in dev-mode code. ESBuild conservatively kept it in the production bundle even when ngDevMode was false, leaving an unnecessary stub.

This commit inlines the function directly inside the ngDevMode guard, ensuring that ESBuild can fully remove it during tree-shaking. This eliminates dead code and reduces bundle size in production.

Inlining also allows dropping assertDefined, which was previously referenced only inside prependTokenToDependencyPath. With the function now fully inlined and dev-only, assertDefined is also eliminated from production builds, further reducing bundle size.

This was built with production configuration and NG_BUILD_MANGLE=0:

image

…shaking

Previously, `prependTokenToDependencyPath` was a top-level function
used only in dev-mode code. ESBuild conservatively kept it in the
production bundle even when `ngDevMode` was false, leaving an
unnecessary stub.

This commit inlines the function directly inside the `ngDevMode`
guard, ensuring that ESBuild can fully remove it during tree-shaking.
This eliminates dead code and reduces bundle size in production.

Inlining also allows dropping `assertDefined`, which was previously
referenced only inside `prependTokenToDependencyPath`. With the
function now fully inlined and dev-only, `assertDefined` is also
eliminated from production builds, further reducing bundle size.
@pullapprove pullapprove bot requested a review from thePunderWoman August 24, 2025 21:18
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Aug 24, 2025
@ngbot ngbot bot added this to the Backlog milestone Aug 24, 2025
@JeanMeche
Copy link
Member

JeanMeche commented Aug 24, 2025

Do you have an esbuild issue reference for why this isn't removed by tree shaking ?

@arturovt
Copy link
Contributor Author

evanw/esbuild#4095

@JeanMeche
Copy link
Member

What if we moved prependTokenToDependencyPath into r3_injector (so it's not exported anymore). Does it get tree shaken away ?

@arturovt
Copy link
Contributor Author

Nope, I tried it :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants