0

Is it possible to make the following code AoT-compatible?

interface Config {
    someField?: string;
    anotherField?: number;
}

@NgModule({})
export class SomeModule {
    protected static _config: Config;
    
    public static withConfig(config: Config = {}): ModuleWithProviders {
        // the line below fails AoT compilation
        SomeModule._config = Object.assign({}, { someField: 'defaultValue' }, config);
        return {
            ngModule: SomeModule,
            providers: []
        };
    }
}

SomeModule.withConfig() has to be imported in main module and config has to be set synchronously before the app starts (for decorating function), but with AoT compilation it throws:

Angular compilation done, starting webpack bundling.

Error: Error encountered resolving symbol values statically. Calling function 'SomeModule', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppModule in /.../app/app.module.ts, resolving symbol AppModule in /../app/app.module.ts

Any ideas?

EDIT: because of the comments, here is how I import the module in app.module:

@NgModule({
    imports: [
        SomeModule.withConfig(), // yeah, just like that or with { someField: '' }
    ]
})
export class AppModule {}
Community
  • 1
  • 1
Daniel Kucal
  • 8,684
  • 6
  • 39
  • 64
  • The question presumes that it's not compatible with AoT, but it's not true. This is how `forRoot` method is supposed to work. And of course, it works with AoT. It's not clear how you use it. See http://stackoverflow.com/help/mcve – Estus Flask May 22 '17 at 13:56
  • I don't understand what you mean... The question is about synchronous module config injection. It works with JIT compiler, but doesn't with AoT. What's not clear? I've provided code example and wrote what I want to achieve... – Daniel Kucal May 22 '17 at 20:09
  • The code you've posted should work with AoT. The same recipe (known as forRoot) works with AoT in Angular router and numerous third-party modules. It depends on how you use it in your own application. You cite the error that mentions AppModule. But there's no AppModule in the question. Where's that app.module.ts? The questions without http://stackoverflow.com/help/mcve are considered off-topic. – Estus Flask May 22 '17 at 20:38
  • So why it doesn't work? I thought it's pretty clear how to import a module. I added the app.module part to the question. I've investigated how they do it in other libraries and there always was injection into providers (then accessible asynchronously in constructors by DI, so not my case). Could you now reply to the question? Thanks in advance. – Daniel Kucal May 22 '17 at 21:01
  • It's still not clear what the code looks like in whole. Why does `class SomeModule` have no `export`? Is it defined in app.module.ts too? It's not possible to answer the question in its current state. You're not providing enough information to replicate the issue or give an idea where the problem may be. – Estus Flask May 22 '17 at 21:21

0 Answers0