question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Configurable module creation with IVY and AOT on

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/core

Is this a regression?

No

Description

I want to create configurable Angular 9 module with IVY and AOT on. In the simplest scenario it must provide a single stateful service configurable by name:

@Injectable()
export class CounterService {
  private counter = 0;
  shot() {
    return this.counter++;
  }
}

@NgModule()
export class CounterModule {
  static withConfig(name: string): ModuleWithProviders {
    return {
      ngModule: CounterModule,
      providers: [{
        provide: name,
        useClass: CounterService
      }]
    };
  }
}

Everything works fine at this point. But if I want to add any logic into CounterModule.withConfig:

@NgModule()
export class CounterModule {
  static withConfig(name?: string): ModuleWithProviders {
    const counterProviderToken = name ? name : CounterService;
    return {
      ngModule: CounterModule,
      providers: [{
        provide: counterProviderToken,
        useClass: CounterService
      }]
    };
  }
}

I’m getting a compilation error NG1010.

🔬 Minimal Reproduction

https://github.com/vdshb/ng-modules-problem

Please, check out the last commit. It adds code to get compilation exception.

🔥 Exception or Error


error NG1010: Value at position 1 in the NgModule.imports of AppModule is not a reference: [object Object]

🌍 Your Environment

Angular Version:



Angular CLI: 9.1.0
Node: 13.12.0
OS: linux x64

Angular: 9.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.0
@angular-devkit/build-angular     0.901.0
@angular-devkit/build-optimizer   0.901.0
@angular-devkit/build-webpack     0.901.0
@angular-devkit/core              9.1.0
@angular-devkit/schematics        9.1.0
@ngtools/webpack                  9.1.0
@schematics/angular               9.1.0
@schematics/update                0.901.0
rxjs                              6.5.4
typescript                        3.7.5
webpack                           4.42.0

Anything else relevant? I’m not sure if it’s a bug or we have any other ways to make a configurable module.

It might be related to this ticket: https://github.com/angular/angular/issues/36243 but the cases are different.

Everithyng works fine with option projects.ng-modules-shots.architect.build.options.aot=false set in angular.json

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
alxhubcommented, May 26, 2020

I have some good news, @vdshb! #37126 will relax the single-statement restriction and allow you to write your forRoot or withConfig functions however your heart desires 😃

4reactions
JoostKcommented, Apr 3, 2020

Actually I will reopen this as I think this is rather arbitrary. At least the error message should be improved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Ivy
Ivy is the code name for Angular's next-generation compilation and rendering pipeline. With the version 9 release of Angular, the new compiler and...
Read more >
Automatically upgrade lazy-loaded Angular modules for Ivy!
From NgModule to NgModuleFactory with the AOT Compiler: ... The Angular compiler's job is to statically analyse all of the code in our...
Read more >
All About Angular Engine Ivy in 5 mins
The build and rebuild time enhancement in Ivy, empowers AOT to offer an improved developer experience. In Angular 9, AOT compilation is enabled ......
Read more >
Lazy loading Angular modules with Ivy - InDepth.Dev
To solve this issue we need to create an NgModule that declares our component and imports CommonModule , just like we normally would ......
Read more >
Configurable angular 9 module - typescript - Stack Overflow
I want to create configurable Angular 9 module with IVY and AOT on. IVY and AOT on by default in current version of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found