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

1

5

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!

Delayedaction answered 18/5, 2018 at 22:32 Comment(2)
I think you'd spend less time making your project work with angular-cli than rolling your own build.Haddix
@Haddix I would if I could! :)Delayedaction
O
9

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.

Osculation answered 19/5, 2018 at 0:21 Comment(5)
Thank you for the answer. This clears up things a bit. So this means that I won't be able to make my library AOT capable even if one of my dependencies is not?Delayedaction
That is true. If one of the libraries you need is not AOT capable, then your library won't be either. Most libraries that are maintained are capable of AOT compilation, so unless you are using some library that is older, or stale, you should be able to use AOT in the end.Osculation
@R.Richards Do you know of any way to build a library AOT, in the scenario where this is built as a UMD and the consumed in another application using SystemJs module loader? It works fine when the main application runs with JIT but as the compiler gets removed on prod builds, i would like to have the library AOT compiled as well in order to be able to use it in production and not give up on AOT in main app.Holbrooke
@Holbrooke Did you find a solution for this issue? as I have the same issue.Dennis
What if the library is in a private monorepo and won't be published?Musculature

© 2022 - 2024 — McMap. All rights reserved.