Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an Ahead-of-Time (AOT) compiled library to be consumed by Angular applications

I have an Angular 5 library that I expose as a package for other apps to consume from their node_modules.

Currently, the app is Just-in-Time(JIT) compiled using rollup and gulp and exported as a package. So developer applications use my package in their JIT compiled form.

Researching about AOT has led me to believe that any Angular app which when AOT compiled is much more performant than its JIT counterpart on the browser. However, as a library developer, I would like to know if app developers will get any performance benefit if I were to expose my library AOT compiled?

I use ng-bootstrap and a lot of other open-source libraries to create components in my module and add custom styling or functionalities on top of them. Do all the libraries I consume in my module also need to be in their AOT forms or I could use their JIT counterparts?

Also, I think it would be a good idea to have separate packages for my library - packageName and packageName-aot so that the users have an option to choose whichever library they want to use.

Apart from the entire code refactoring(changing the private variables used in the template to public, removing arrow functions, lambda expressions etc.), is there anything else I need to keep in mind before exposing my library modules in AOT form?

I cannot use the Angular CLI due to certain constraints, so will have to depend on @ngtools/webpack to get AOT compilation if any.

Currently, my tsconfig.json has the following options:

"angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMedtadataEmit": true,
    "fullTemplateTypeCheck": true
}

I have searched a lot on the internet, but the Angular AOT docs are pretty vague and are not very clear for what I have been trying to do here. Any sort of direction would be really helpful.
Thanks!

like image 796
nashcheez Avatar asked May 18 '18 22:05

nashcheez


People also ask

What is AOT ahead-of-time compilation Angular?

The Angular ahead-of-time (AOT) compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase before the browser downloads and runs that code. Compiling your application during the build process provides a faster rendering in the browser.

What are the ways to control AOT compilation?

When you use the Angular AOT compiler, you can control your app compilation in two ways: By providing template compiler options in the tsconfig. json file. By specifying Angular metadata.

Which file is used to set the default AOT is true in Angular 9?

json file, you can add a custom script which compile with AoT. Then you can run the command npm aot .

Which of the following are true about AOT compilation in Angular?

AOT provides better security. It compiles HTML components and templates into JavaScript files long before they are served into the client display. So, there are no templates to read and no risky client-side HTML or JavaScript evaluation. This will reduce the chances of injections attacks.


1 Answers

The library you write, and the libraries your library depends on, need to be AOT capable. That’s really all there is to it.

You do not write, and deliver, a library that is AOT compiled (AOT compiled form, as you put it); the consuming applications will do the AOT compiling at build time.

All you must do is make sure that when a consuming application does an AOT build (ng build --prod if using the Angular CLI), that your library plays nice. By plays nice I mean, your library can AOT compile with the consuming app without blowing errors.

Library tools like generator-angular2-library, or ng-packagr (which is a tool the @angular/cli 6.0.0 uses to support building libraries) just build packages to JavaScript, nothing more.

It is up to the consuming application's build tool to do an AOT build. This is what allows a library to be used by any application (or application framework/library) that needs it. Try not to overthink what it takes to build/deploy a library. The heavy lifting of AOT is up to the application that uses it.

like image 101
R. Richards Avatar answered Sep 18 '22 17:09

R. Richards