0

By following this article I tried to dynamically import modules to my AppModule according to the environment I'm running (e.g. I only want StoreDevtoolsModule in dev, not in production).

My approach was

let devImports = [
  StoreDevtoolsModule.instrument({maxAge: 25}),
];

if (environment.production) {
  devImports = [];
}

@NgModule({
  ...
  imports: [
    // other imports
    ...devImports,
  ],
  ...
})
export class AppModule {
}

And it worked when running ng s, but then I tried running with AOT to check if it works as well with node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng s --aot and it didn't, StoreDevtoolsModule was imported and I was able to use the extension to see my app state.

I can understand why AOT fails to dynamically import modules, but is there another way? I have this article with different approach to try yet, but I don't know if it works for that compilation approach.

(I know I can logOnly: environment.production for StoreDevtoolsModule, it was just an example)

Thanks.

João Ghignatti
  • 2,281
  • 1
  • 13
  • 25
  • while there are other problems, `instrument` isn't going to work because angular can't turn it into json metadata. The reason it doesn't work with AOT is because in AOT decorators aren't decorators at all. – Aluan Haddad May 21 '19 at 21:39
  • Thanks for your comment. I get why it won't work with AOT and it was just an example to use StoreDevtoolsModule, but, does it implies that other modules that you call a method inside, like `StoreModule.forRoot(reducer)` won't work with dynamic import for AOT? Is there a way to dynamic import them, as asked? Thanks again. – João Ghignatti May 21 '19 at 22:17

0 Answers0