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.

[Question] Tree shaking services

See original GitHub issue

I recently created a PR for an addon which removed the app re-export for a service that’s considered private API and replaced it with an injection string that uses the namespaced name (@service('ember-breadcrumb-trail@breadcrumbs') breadcrumbs).

This almost worked perfectly, but I noticed the CI job for embroider optimized failed. Looking more closely at it, it seems the service is not part of the final bundle so somewhere during the build process it gets marked as unneeded and dropped. I found this surprising since I explicitly inject it in multiple helpers.

I looked around at the documentation and specification but couldn’t really find an answer to how the code analysis works. Does Embroider look at container lookups when analyzing the code?

While experimenting I noticed that explicitly importing and registering the service in the container does fix the problem (with the class import being the trigger I guess). Is this what’s required to make services work that aren’t re-exported for the app folder?

I don’t think it’s a major issue since not re-exporting a service seems like a niche use-case and most addons use a - prefixed service name for private services instead. I just wanted to verify that this is intended behavior.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ef4commented, Jun 1, 2021

Does Embroider look at container lookups when analyzing the code?

No, it doesn’t. In general they aren’t statically analyzable.

A lot of the common cases are analyzable, but that leaves behind some painful and surprising edge cases that we would either need to make into annoying build errors or just let apps break.

I’m open to adding a staticServices option so people can experiment with getting fully static analysis of services, but nobody has prioritized working on that at the moment.

While experimenting I noticed that explicitly importing and registering the service in the container does fix the problem

Yes, you can manage it directly like that and it will work.

Going forward, I hope people will help design improvements to Ember’s API in this area to help making it easier for tooling (including both Embroider and TypeScript) to statically understand the dependency injection system).

0reactions
Windviscommented, Jun 1, 2021

@ef4 Thank you, this has been very educational!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree-Shaking: A Reference Guide - Smashing Magazine
“Tree-shaking” is a must-have performance optimization when bundling JavaScript. In this article, we dive deeper on how exactly it works and ...
Read more >
Tree Shakeable Providers and Services in Angular - Cory Rylan
First, let's define tree shaking before we dig too deep. Tree shaking is a step in a build process that removes unused code...
Read more >
Why "providedIn" Services are Tree-Shakable (2021, Advanced)
In this tutorial we going to talk about Tree - Shaking and what makes services which decorated with @Injectable({ providedIn: .
Read more >
Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >
What are Services and Tree Shakable providers in Angular ...
Tree Shaking is a Process wherein the unused code is removed during the build phase. Tree Shaking Ensures that our application includes the...
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