Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular APP_INITIALIZER in library module generates metadata compile time error: Lambda not supported

This is my ng library module code

   @NgModule({
      imports: [NgIdleModule.forRoot()],
      providers: [ {
        provide: APP_INITIALIZER,
        useFactory: (idleStateChangeHandlerService: IdleStateChangeHandlerService) =>  () => console.log(IdleStateChangeHandlerService),
        multi: true,
        deps: [IdleStateChangeHandlerService]
      }]
    })
    export class IdleActivityModule {
      static forRoot(config: IdleActivityConfig): ModuleWithProviders {
        return {
          ngModule: IdleActivityModule,
          providers: [
            {
              provide: IdleActivityConfigInjectionToken,
              useValue: config
            }
          ]
        };
      }
    }

On build:

> Compiling TypeScript sources through ngc ERROR:
> C:/_dev/seemis-workspace/projects/shared-modules/src/lib/idle-activity/idle-activity.module.ts:9:1:
> Error encountered in metadata generated for exported symbol
> 'IdleActivityModule':  
> C:/_dev/seemis-workspace/projects/shared-modules/src/lib/idle-activity/idle-activity.module.ts:13:17:
> Metadata collected contains an error that will be reported at runtime:
> Lambda not supported.   {"__symbolic":"error","message":"Lambda not
> supported","line":12,"character":16}

If I move APP_INITIALIZER provider into the app module instead it is fine, but I don't want the app to have to have this knowledge.

like image 779
jenson-button-event Avatar asked Nov 14 '19 09:11

jenson-button-event


1 Answers

This is fixed with some Kung-fu black magic:

Add a code comment with @dynamic

// @dynamic
@NgModule({ ...

See Angular Compiler Options for no reason other than to be even more confused.

like image 163
jenson-button-event Avatar answered Nov 16 '22 15:11

jenson-button-event