Using Angular Library

Overview - Angular Library

Using Angular Library
Environment: angular v8.2; node.js v12

Create

You can create an Angular library within an existing application. e.g. lib name is my-lib. Commands:

1
ng generate library my-lib`

Build

1
ng build my-lib

Tips

Angular lvy compatibility

Ivy applications can be built with libraries that were created with the View Engine compiler. This compatibility is provided by a tool known as the Angular compatibility compiler (ngcc). CLI commands run ngcc as needed when performing an Angular build.

In version 9, the server builder which is used for App shell and Angular Universal has the bundleDependencies option enabled by default. If you opt-out of bundling dependencies you will need to run the standalone Angular compatibility compiler (ngcc). This is needed because otherwise Node will be unable to resolve the Ivy version of the packages.

You can run ngcc after each installation of node_modules by adding a postinstall npm script

1
2
3
4
5
6
// package.json
{
"scripts": {
"postinstall": "ngcc"
}
}

The postinstall script will run on every installation of node_modules, including those performed by ng update and ng add.
Don’t use –create-ivy-entry-points as this will cause Node not to resolve the Ivy version of the packages correctly.