pjlamb12
pjlamb12

Reputation: 2422

Angular APP_INITIALIZER prevents feature modules from building

I'm using the APP_INITIALIZER in my Angular app to load configuration when my app bootstraps. It technically works; the configuration is loaded and the app.module seems to build properly, but none of the feature modules are built and thus are unusable. Here's the output of ng build with the APP_INITIALIZER:

enter image description here

And here is the output without APP_INITIALIZER:

enter image description here

Here's the code that is called in the APP_INITIALIZER factory:

// in app.module.ts
function initConfig(configService: AppConfigService) {
    return () => configService.loadConfig();
}

// in app-config.service.ts
public loadConfig() {
    return this._http.get('./assets/app-config/config.json')
        .toPromise()
        .then((config: any) => {
            this.config = config;
            this.configSubject$.next(this.config);
        })
}

So as you can see, the factory returns a promise, it loads the configuration, etc., but something happens that prevents it from building the feature modules. Am I missing something in the factory that is preventing this from loading the feature module?

Edit

As a test, I just returned true immediately in the initLoad factory function, and the same thing happened where the feature modules are not built.

Edit 2

After a little more digging and looking at this, I believe the more accurate statement is that lazy loaded modules are the ones that are not being built. If a feature module is not lazy loaded, it seems to continue to be built and work as expected.

Here's a GitHub repo with a demo app where a module is set up to lazy load. If you comment out the providers array in the AppModule, and run ng build, you'll see a line about the lazy loaded module being built, right under the main chunk. Then, re-add the providers array and run ng build, and only the main chunk is there.

Upvotes: 1

Views: 1252

Answers (1)

yurzui
yurzui

Reputation: 214125

Seems don't exporting initConfig function breaks dependencies resolution algorithm.

So simply export it:

export function initLoad() {...}

Upvotes: 4

Related Questions