Setup incremental builds for Angular applications
In this guide we’ll specifically look into which changes need to be made to enable incremental builds for Angular applications.
Incremental builds requires Nx version 10.4.0 or later.
Requirements
It’s required that you run the Angular compatibility compiler (ngcc
) after every package installation if you have Ivy enabled. This comes configured by default in every Nx workspace. The incremental build relies on the fact that ngcc
must have already been run. You can check your package.json
and make sure you have the following:
1{
2 ...
3 "scripts": {
4 ...
5 "postinstall": "ngcc --properties es2015 browser module main",
6 ...
7 }
8 ...
9}
Please note that
ngcc
doesn’t supportpnpm
(#32087 and #38023), so you need to use eitheryarn
ornpm
.
Use buildable libraries
To enable incremental builds you need to use buildable libraries.
You can generate a new buildable library with:
nx g @nrwl/angular:lib mylib --buildable
The generated buildable library uses the @nrwl/angular:ng-packagr-lite
executor which is optimized for the incremental builds scenario:
1"mylib": {
2 "projectType": "library",
3 ...
4 "targets": {
5 "build": {
6 "executor": "@nrwl/angular:ng-packagr-lite",
7 "outputs": ["dist/libs/mylib"],
8 "options": {...},
9 "configurations": {...},
10 "defaultConfiguration": "production"
11 },
12 "lint": {...},
13 "test": {...}
14 },
15 ...
16},
Please note that it is important to keep the
outputs
property in sync with thedest
property in the fileng-package.json
located inside the library root. When a library is generated, this is configured correctly, but if the path is later changed inng-package.json
, it needs to be updated as well in the project configuration.
Adjust the app executor
Change your Angular app’s “build” target executor to @nrwl/angular:webpack-browser
and the “serve” target executor to @nrwl/web:file-server
as shown below:
1"app0": {
2 "projectType": "application",
3 ...
4 "targets": {
5 "build": {
6 "executor": "@nrwl/angular:webpack-browser",
7 "outputs": ["{options.outputPath}"],
8 "options": { ... },
9 "configurations": { ... },
10 "defaultConfiguration": "production"
11 },
12 "serve": {
13 "executor": "@nrwl/web:file-server",
14 "options": {
15 "buildTarget": "app0:build"
16 },
17 "configurations": {
18 "production": {
19 "buildTarget": "app0:build:production"
20 }
21 }
22 },
23 ...
24 }
25},
Running and serving incremental builds
To build an app incrementally use the following command:
nx build myapp --parallel
To serve an app incrementally use this command:
nx serve myapp --parallel
Note: you can specify the --parallel
flags as part of the options property on the file-server executor in your project.json
file. The file-server executor will pass those to the nx build
command it invokes.
1{
2 "projectType": "application",
3 ...
4 "targets": {
5 "build": {
6 "executor": "@nrwl/angular:webpack-browser",
7 "outputs": ["{options.outputPath}"],
8 "options": { ... },
9 "configurations": { ... }
10 },
11 "serve": {
12 "executor": "@nrwl/web:file-server",
13 "options": {
14 "buildTarget": "app0:build",
15 "parallel": true
16 },
17 "configurations": {
18 "production": {
19 "buildTarget": "app0:build:production"
20 }
21 }
22 },
23 ...
24 }
25},
Example repository
Check out the nx-incremental-large-repo for a live example.