Function calls are not supported in decorators but 'LoggerModule' was called.
See original GitHub issueType of Issue
[x] Bug Report
[ ] Feature Request
Description
I wrote an angular logger library with ng-packagr to use this in different angular applications. The library doesn’t do much more than printing something to the console in the constructor of the service. Everything works fine without --prod, so without AOT compile, in the angular application.
Besides I tried different ng-versions (5.x.x, 6.x.x & 7.x.x). But in all cases everytime (with AOT) the same error when I call LoggerModule.forRoot() in the app.module of the application:
ERROR in Error during template compile of 'AppModule'
Function calls are not supported in decorators but 'LoggerModule' was called.
I read many articles about this topic, tried different angularCompilerOptions in tsconfig. Any further ideas out there? The module works fine without AOT (but this is no option for us)…
How To Reproduce
Write a library like the usage example of: https://www.npmjs.com/package/ng-packagr
Use this library in a new angular application which is generated by the angular cli by calling LoggerModule.forRoot() in the app.module.ts.
NgModule of the library:
@NgModule({
declarations: [],
imports: [],
providers: []
})
export class LoggerModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: LoggerModule,
providers: [LoggerService]
}
}
}
NgModule of the application:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
LoggerModule.forRoot()
],
providers: [],
bootstrap: [AppComponent],
entryComponents: [AppComponent]
})
export class AppModule {
}
Try ng build --prod…
Expected Behaviour
The build of the ng-app works with and without --prod flag / aot compiler.
Version Information
$ node_modules/.bin/ng-packagr --version
ng-packagr: ^4.3.1
@angular/*: ^6.1.0
typescript: ~2.9.2
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:9 (2 by maintainers)
Top GitHub Comments
export loggerModule = LoggerModule.forRoot();
add loggerModule to imports, should work
I finally found the root cause of the issue: calling
throw
(or any function) inside theforRoot()
breaks themetadata.json
generated byngc
.So, previously I had this:
So, refactoring the method as follows solved the issue: