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

1

6

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.

Thermopile answered 14/11, 2019 at 9:0 Comment(0)
T
11

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.

Thermopile answered 4/1, 2020 at 6:46 Comment(2)
you can check this link for a little understanding of what this does. github.com/angular/angular/issues/19698#issuecomment-338340211Up
In my case I added // @dynamic comment, but I also needed to move away from Lambda for the return of my factory. useFactory: (appConfig: AppConfigService<T>) => function() { return appConfig.loadConfig(configUrl) }. I had () => appConfig.loadConfig(configUrl) and it still didn't build with the @dynamic commentMetapsychology

© 2022 - 2024 — McMap. All rights reserved.