When defining an APP_INITIALIZER
in a library module, the build fails with the Lambda not supported
error. The build error is thrown when the function is exported as per docs:
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { MylibComponent } from './mylib.component';
export function myLibInit() {
return () => console.log('Hi from exported function');
}
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useFactory: myLibInit
}
]
})
export class MylibModule { }
The build error is thrown in both dev
and prod
.
I have alternatively also tried defining the factory function using ES6 object method shorthand notation:
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { MylibComponent } from './mylib.component';
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useFactory() {
return () => console.log('Hi from ES6 object method shorthand')
}
}
]
})
export class MylibModule { }
This passes both the dev
and prod
build, but the app throws the ERROR TypeError: this.appInits[r] is not a function
error at runtime when the library and app is built with the prod
flag.
How does one correctly use the APP_INITIALIZER
in a library and not get build or runtime errors?
A reproduction can be found here:
git clone https://github.com/samherrmann/angular-sandbox.git
cd angular-sandbox
git checkout lambda-not-supported
npm install
npm run build
GitHub issue regarding this problem can be found here.