Skip to content
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

perf: injection tokens' factories don't get tree-shaken #891

Closed
davidlj95 opened this issue Oct 7, 2024 · 1 comment
Closed

perf: injection tokens' factories don't get tree-shaken #891

davidlj95 opened this issue Oct 7, 2024 · 1 comment

Comments

@davidlj95
Copy link
Owner

After implementing some new APIs that weren't used in any place (except for unit tests of those) in #883, expected that bundle size of production apps wouldn't change. Because those APIs weren't used. So they'd be tree-shaken... right? Well turns out they aren't:

📦 Bundle size (Angular v18)

Git ref: 6709d12f8a4f41b5fa323f1c63c67c3947750af1

Module file Size Base size Difference
ngx-meta-core.mjs 3134 bytes (3.1KiB) 2816 bytes (2.8KiB) 11.29%: 318 bytes (318B)

(Not only v18, but also rest of versions)

So first thought it would have to do with the recent re-organization of files and directories and the index.ts files around exporting module's features. See #862.

Found this evanw/esbuild#1420 But doesn't seem to be the issue as we don't use export * as x. Also tried reverting that change back and the issue still appears again.

However, if we export something like const FOO = 'foo'. It doesn't appear in the production build if unused.

Tried then exporting partially until seeing which is the part that can't get tree shaken. Used the v18 app as reference. Exporting the helpers withNameAttribute, withContentAttribute ... but not using them just adds 4 bytes to the bundle* I was expecting 0, but definitely 4 is something acceptable and probably related to bundler internals.

After checking, seems it's due to var added. Not sure why. Bundler mysteries. Maybe var can't declare at once more than x things at once? Given the output is var x=[...],y=[...],... Anywaysssss

What if the new InjectionToken is the issue? Tried exporting const FOO = new InjectionToken('foo'). It gets in the production bundle despite unused! Why? Probably because new X may come with side effects so it's not 100% to tree shake it.

Tried then removing usages of _HEAD_ELEMENT_UPSERT_OR_REMOVE (standard & json ld modules) from the example app. Built again and... yup, the token and it's factory are still in the production bundle. How can then an injection token be defined in a manner that if it's unused doesn't end up in the production bundle?

@davidlj95
Copy link
Owner Author

davidlj95 commented Oct 10, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant