Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why entryComponents is not necessary anymore in Angular 9/ivy compiler?

Can anyone give a clear explanation of why in the IVY compiler, entry components API Is not necessary anymore?. In other words what was change internally so that Angular suddenly doesn't need a heads up that you are going to create component dynamically

like image 999
Ariel Henryson Avatar asked May 01 '20 11:05

Ariel Henryson


People also ask

Is entryComponents deprecated?

Entry components are deprecated, for more information, see entryComponents deprecation in the Deprecated APIs and features. An entry component is any component that Angular loads imperatively, (which means you're not referencing it in the template), by type.

Is Ivy enabled by default in Angular 9?

In version 9, Ivy is the default. For compatibility with current workflows during the update process, you can choose to opt-out of Ivy and continue using the previous compiler, View Engine.

What is the difference between declarations and entryComponents?

Difference between entry Component and Declaration: entryComponents are used to register components for offline computation in a module. These components are referenced here as they not referenced anywhere else in HTML template. Declarations are used to make Directives(components, pipes etc.) in a specific module.

How do I enable Ivy in Angular 9?

Using Ivy in an existing project : AOT compilation with Ivy is faster and should be used by default. In the angular. json workspace configuration file, set the default build options for your project to always use AOT compilation. To stop using the Ivy compiler, set enableIvy to false in tsconfig.


Video Answer


1 Answers

ViewEngine

Prior to Ivy, ViewEngine compiler performed the whole program analysis based on NgModule configurations and html template and then produced module and component factories based on this global transitive information.

This means that if you have a component which you’re not referencing in the template and you haven't added it to entryComponents array of NgModule then this component won't be compiled and you can't render it dynamically because Angular doesn't know where to get factory for this component.

Once you added it, the compiler will produce dedicated factory and also add this factory to internal HashMap so that it can be resolved through ComponentFactoryResolver.

Ivy

Ivy introduced a completely new ngtsc compiler which mental model is that the decorator is the compiler.

In other words, the overall architecture of ngtsc it is a set of TypeScript transformers: for component, pipe, ngModule etc.

These transformers emit static functions like AppComponent.ɵfac, AppComponent.ɵcmp in place meaning that transpiled code resides in the same file where the original component/pipe/ngModule is located. So we have factories(all code required for instantiating Angular components/pipes/modules) in the same place and they can be easily accessed by those static properties.

In simple words, if there is a file included in TypeScript compilation that has class with a @Componentdecorator then ngtsc compiler will emit factory for this class in the same file.

As you can guess if you import that component in any file and Angular can easily discover its factory through static property.

See also:

  • DESIGN DOC(Ivy): Compiler Architecture

  • DESIGN DOC (Ivy): Separate Compilation

like image 150
yurzui Avatar answered Oct 05 '22 17:10

yurzui