-
Notifications
You must be signed in to change notification settings - Fork 25.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(compiler-cli): ignore the module.id anti-pattern for NgModule ids (
#45024) In early versions of Angular, it was sometimes necessary to provide a `moduleId` to `@Component` metadata, and the common pattern for doing this was to set `moduleId: module.id`. This relied on the bundler to fill in a value for `module.id`. However, due to the superficial similarity between `Component.moduleId` and `NgModule.id`, many users ended up setting `id: module.id` in their NgModules. This is an anti-pattern that has a few negative effects, including preventing the NgModule from tree-shaking properly. This commit changes the compiler to ignore `id: module.id` in NgModules, and instead provide a warning which suggests removing the line entirely. PR Close #45024
- Loading branch information
Showing
6 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@name NgModule.id Set to module.id anti-pattern | ||
@category compiler | ||
@shortDescription Setting NgModule.id to module.id is a common anti-pattern | ||
|
||
@description | ||
Using `module.id` as an NgModule `id` is a common anti-pattern and is likely not serving a useful purpose in your code. | ||
|
||
NgModules can be declared with an `id`: | ||
|
||
```typescript | ||
@NgModule({ | ||
id: 'my_module' | ||
}) | ||
export class MyModule {} | ||
``` | ||
|
||
Declaring an `id` makes the NgModule available for lookup via the `getNgModuleById()` operation. This functionality is rarely used, mainly in very specific bundling scenarios when lazily loading NgModules without obtaining direct references to them. In most Angular code, ES dynamic `import()` (`import('./path/to/module')`) should be used instead, as this provides a direct reference to the NgModule being loaded without the need for a global registration side-effect. | ||
|
||
If you are not using `getNgModuleById`, you do not need to provide `id`s for your NgModules. Providing one has a significant drawback: it makes the NgModule non-tree-shakable, which can have an impact on your bundle size. | ||
|
||
In particular, the pattern of specifying `id: module.id` results from a misunderstanding of `@NgModule.id`. In earlier versions of Angular, it was sometimes necessary to include the property `moduleId: module.id` in `@Component` metadata. | ||
|
||
Using `module.id` for `@NgModule.id` likely results from confusion between `@Component.moduleId` and `@NgModule.id`. `module.id` would not typically be useful for `getNgModuleById()` operations as the `id` needs to be a well-known string, and `module.id` is usually opaque to consumers. | ||
|
||
@debugging | ||
You can remove the `id: module.id` declaration from your NgModules. The compiler ignores this declaration and issues this warning instead. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters