1

I would like to have an angular5 feature module in a separate library.

I've created a project and put the module declaration inside:

@NgModule({
    imports: [],
    declarations: [],
    exports: [],
    providers: []
})
export class ApiModule {
    public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
        return {
            ngModule: ApiModule,
            providers: [ { provide: Configuration, useFactory: configurationFactory } ]
        }
    }

    constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
        if (parentModule) {
            throw new Error('ApiModule is already loaded. Import your base AppModule only.');
        }
    }
}

After compiling it with tsc, it turns out that compiling the main project with ng build --prod gives:

ERROR in Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'ApiModule' was called.

1 Answers1

0

The problem was that tsc do not emit the metadata which contains all of the data from decorators required by angular.

I solved this by using the ng-packagr https://github.com/dherges/ng-packagr (v2.0.0-rc.11)