Conditional Module imports (e.g. for development only)
See original GitHub issueπ Conditional Module imports (e.g. for development-only modules)
Relevant Package
This feature request is for @angular/core / ivyDescription
View Engine allowed us to pass a module conditionally, which was useful as some modules should only be imported in development. Example:
@NgModule({
imports: [
process.env.NODE_ENV === 'development' ? NgxsReduxDevtoolsPluginModule.forRoot({}) : undefined
]
})
Using Ivy, the above solution will fail when building with production target. Running ng serve xxx --prod leads to these errors: CLI will show lots of misguiding errors (telling me component xxx is not a known element). Browser console will show βASSERTION ERROR: Type passed in is not NgModuleType, it does not have βΙ΅modβ property.β
I thought Ivy might not like the undefined value, so I tried this as a workaround:
@NgModule()
export class NoopModule {}
@NgModule({
imports: [
process.env.NODE_ENV === 'development' ? NgxsReduxDevtoolsPluginModule.forRoot({}) : NoopModule
]
})
β¦ which lead to the same errors.
Describe the solution youβd like
Does Ivy support this by now, in way I donβt know? if not, I suggest to allow to pass undefined to module imports.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Conditional module imports in Angular | by Luuk Gruijs - Medium
Importing ngrx/store-devtools only in development can be done like this: ... I hope this little trick helps you to do conditional module imports....
Read more >Conditional module imports in Angular - Agile Actors #learning
You want to use ngrx/store-devtools only in development and prevent it from being imported in production.
Read more >How can I conditionally import an ES6 module? - Stack Overflow
You can now call the import keyword as a function (i.e. import() ) to load a module at runtime. It returns a Promise...
Read more >Conditional module imports in Angular - LinkedIn
Importing ngrx/store-devtools only in development can be done like ... I hope this little trick helps you to do conditional module imports.
Read more >Conditional module imports in Angular - HackerNoon
The best examples are: You're using ngrx/store and the ngrx/store-devtools module. You want to use ngrx/store-devtools only in development ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
ok there is one important thing!!
One MUST NOT pass undefined into imports list under any circumstances.
This will fail during production build with Error
Unexpected value 'undefined' imported by the module ...
But if you pass in an empty array as @alxhub does in his example above, it will work.
So examples and issues mentioned above might be due to dynamic environment. But the environment may also actually be analyzed correctly and the βundefinedβ value might be source of the problem.
@LanderBeeuwsaert Angular 12 supports that particular use-case using the new
BrowserAnimationsModule.withConfig
API.