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.

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155

1 Answers1

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.

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
  • 1
    you can check this link for a little understanding of what this does. https://github.com/angular/angular/issues/19698#issuecomment-338340211 – Sachith Rukshan Feb 07 '21 at 17:59
  • 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) => function() { return appConfig.loadConfig(configUrl) }`. I had `() => appConfig.loadConfig(configUrl)` and it still didn't build with the @dynamic comment – user1738539 Jun 22 '21 at 15:11