Skip to content

Dynamic component broken when using Standalone + aot: false  #47996

Closed as not planned

Description

@JeanMeche

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

Suppose this single standalone component with a dynamic component inside :

@Component({
  selector: "app-root",
  template: "<div #container>OK</div>",
  standalone: true,
})
export class AppComponent implements OnInit {
  @ViewChild("container", { read: ViewContainerRef, static: true })
  container!: ViewContainerRef;

  ngOnInit() {
    const component = Component({
      template: "test no",
      styles: [":host {color: red; padding-left: 8px; display:block; }"],
    })(class {});
    this.container.createComponent(component);
  }
}

With aot: false, we get an error : The component '' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available. at runtime.

BUT:
The error does not happen IF platformBrowserDynamic().bootstrapModule({} as any) is present in main.ts (but not called), everything works fine.

Please provide a link to a minimal reproduction of the bug

https://github.com/JeanMeche/angular-change-detection/tree/broken-standalone

Please provide the environment you discovered this bug in (run ng version)

Angular 14.2.9

Activity

  1. JoostK commented on Nov 8, 2022

    @JoostK
    Member

    I'm confused, this seems expected to me. The local, anonymous component declaration isn't supported for AOT compilation so it remains present for the JIT compiler to pick up, and the error is reported to indicate that the JIT compiler is not available.

    Just the import from @angular/platform-browser-dynamic contains a transitive import for @angular/compiler, which contains a side-effectful module that makes the compiler available globally. So it's not because platformBrowserDynamic().bootstrapModule({} as any) is included in the bundle, but rather that an import for @angular/platform-browser-dynamic is present (the "dynamic" part in the platform-browser name refers to its behavior of bringing in the JIT compiler)

  2. added
    needs: clarification
    on Nov 8, 2022
  3. JeanMeche commented on Nov 8, 2022

    @JeanMeche
    MemberAuthor

    Yeah, I had the feeling that a transitive import would be the underlying reason. Thx for pointing it out.

    So this means, currently JIT compiling is not expected to work out of the box when using standalone components only.

    How would one, only import the compiler into a project ?

  4. JoostK commented on Nov 8, 2022

    @JoostK
    Member

    So this means, currently JIT compiling is not expected to work out of the box when using standalone components only.

    I'm not sure what you mean by "standalone components", as it's not what Angular describes as its standalone components using the standalone: true flag.

    How would one, only import the compiler into a project ?

    Using a direct import of @angular/compiler. Please be aware that including the JIT compiler has bundle size and performance overhead, as well as requiring the use of eval which introduces a security risk.

  5. added
    area: core
    on Nov 9, 2022
  6. added this to the needsTriage milestone on Nov 9, 2022
  7. alxhub commented on Nov 15, 2022

    @alxhub
    Member

    This is working as intended - to disable AOT and use JIT compilation, the Angular compiler must be imported dynamically:

    import '@angular/compiler';
  8. angular-automatic-lock-bot commented on Dec 16, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreneeds: clarification

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions