-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom decorators not working again #39758
Comments
Heya, you're likely seeing your decorated components to be tree-shaken away, because there may not be any reference to them. The decorator executes a global side-effect and this setup cannot work with certain optimizations enabled. My suggestion would be to turn off I'll go ahead and close this as non-actionable from a framework perspective, but feel free to continue the discussion/ask questions below. |
@JoostK How can i provide an injection token from the components but resolved on a parent component? I ask because the type managers and the main component reside in the same module, but i want to construct the references from the components and then inject them into the main component. If i declare in any type manager component
then inject it on the main component i get an empty array, of course that is because each component has it's own injector visibility "from here to childs" I looked up the docs and found the [EDIT] |
So what you're trying to do is conceptually incompatible with how tree-shaking works.
This requires that the component is "seen" somehow for the registration to be taken into account. If the component class is not imported from anywhere, then a tree-shaker considers it dead code and allows the class to be omitted from the build output. What I meant to suggest was to provide the token from an NgModule: @NgModule({
providers: [
{ provide: TYPE_MANAGER_TOKEN, useValue: {'someType': SomeComponent}, multi: true },
]
}) This can be cleaned up with a function: export function provideComponent(name: string, type: unknown) {
return { provide: TYPE_MANAGER_TOKEN, useValue: {[name]: type}, multi: true };
}
@NgModule({
providers: [
provideComponent('someType', SomeComponent),
]
}) As an alternative, it is possible to stick with your original design using the decorator if you introduce a hard reference to the classes that have this decorator. This can be done using a static field on an NgModule class: @NgModule({...})
export class SomeModule {
static decoratedComponents: [SomeComponent],
} Although that static field may not be read by anyone, its existence will prevent the tree-shaker from removing the |
Thank you so much for your time replying this issue, i went ahead with a static map in the module that the components reside in
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
馃悶 bug report
Affected Package
The issue is caused by package @angular/compiler
Is this a regression?
Yes, the previous version in which this bug was not present was: 11.0.0
Description
When i updated the angular packages to 11.0.1 version the custom decorators on my project stopped working, same behaviour as before 11.0.0 i think this issue is linked with this one #39574
the code:
type-manager.decorator.ts
then the component register to that map like this
manager-for-type-a.component.ts
manager-for-type-b.component.ts
manager
main.component.html
main.component.ts
馃敟 Exception or Error
Decorators are not executing
馃實 Your Environment
Angular Version:
Working:
Not Working:
Anything else relevant?
Meaby the reason are the latest changes to the compiler
The text was updated successfully, but these errors were encountered: