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 support pnpm (#32087 and #38023), so you need to use either yarn or npm.

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 my-lib --buildable

The generated buildable library uses the @nrwl/angular:ng-packagr-lite executor which is optimized for the incremental builds scenario:

1{
2    "projectType": "library",
3    ...
4    "targets": {
5        "build": {
6            "executor": "@nrwl/angular:ng-packagr-lite",
7            "outputs": ["dist/libs/my-lib"],
8            "options": {...},
9            "configurations": {...},
10            "defaultConfiguration": "production"
11        },
12        ...
13    },
14   ...
15},

Please note that it is important to keep the outputs property in sync with the dest property in the file ng-package.json located inside the library root. When a library is generated, this is configured correctly, but if the path is later changed in ng-package.json, it needs to be updated as well in the project configuration.

The @nrwl/angular:package executor also supports incremental builds. It is used to build and package an Angular library to be distributed as an NPM package following the Angular Package Format (APF) specification. It will be automatically configured when generating a publishable library (nx g @nrwl/angular:lib my-lib --publishable --importPath my-lib).

Adjust the application executor

Change your Angular application’s "build" target executor to @nrwl/angular:webpack-browser and the "serve" target executor to @nrwl/web:file-server as shown below:

1{
2    "projectType": "application",
3    ...
4    "targets": {
5        "build": {
6            "executor": "@nrwl/angular:webpack-browser",
7            "outputs": ["{options.outputPath}"],
8            "options": {
9                "buildLibsFromSource": false
10                ...
11            },
12            "configurations": { ... },
13            "defaultConfiguration": "production"
14        },
15        "serve": {
16            "executor": "@nrwl/web:file-server",
17            "options": {
18                "buildTarget": "my-app:build"
19            },
20            "configurations": {
21                "production": {
22                    "buildTarget": "my-app:build:production"
23                }
24            }
25        },
26        ...
27    }
28},

Running and serving incremental builds

To build an application incrementally use the following command:

nx build my-app --parallel

To serve an application incrementally use this command:

nx serve my-app --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                "buildLibsFromSource": false
10                ...
11            },
12            "configurations": { ... }
13        },
14        "serve": {
15            "executor": "@nrwl/web:file-server",
16            "options": {
17                "buildTarget": "my-app:build",
18                "parallel": true
19            },
20            "configurations": {
21                "production": {
22                    "buildTarget": "my-app:build:production"
23                }
24            }
25        },
26        ...
27    }
28},

Build target name

It is required to use the same target name for the build target (target using one of the executors that support incremental builds: @nrwl/angular:webpack-browser, @nrwl/angular:package and @nrwl/angular:ng-packagr-lite) in the project being built and the buildable libraries it depends on. The executors that support incremental builds rely on the build target name of the project to identify which of the libraries it depends on are buildable.

If you need to have a different build target name for an application (or library) build (e.g. when composing different targets), you need to make sure the build target name of all the relevant projects is the same.

Say you have the same application above with a configuration as follows:

1{
2    "projectType": "application",
3    ...
4    "targets": {
5        "build-base": {
6            "executor": "@nrwl/angular:webpack-browser",
7            "outputs": ["{options.outputPath}"],
8            "options": {
9                "buildLibsFromSource": false
10                ...
11            },
12            "configurations": { ... }
13        },
14        "build": {
15            "executor": "@nrwl/workspace:run-commands",
16            "outputs": ["{options.outputPath}"],
17            "options": {
18                "commands": [
19                    "node ./tools/scripts/important-script.js",
20                    "node ./tools/scripts/another-important-script.js"
21                ],
22                ...
23            },
24            "configurations": { ... }
25        },
26        "serve": {
27            "executor": "@nrwl/web:file-server",
28            "options": {
29                "buildTarget": "my-app:build-base",
30                "parallel": true
31            },
32            "configurations": {
33                "production": {
34                    "buildTarget": "my-app:build-base:production"
35                }
36            }
37        },
38        ...
39    }
40},

And the targetDependencies configured in the nx.json as:

1{
2  "targetDependencies": {
3    "build": [{ "target": "build-base", "projects": "self" }],
4    "build-base": [{ "target": "build-base", "projects": "dependencies" }]
5  }
6}

The build target name of the application is build-base. Therefore, the build target name of the buildable libraries it depends on must also be build-base:

1{
2    "projectType": "library",
3    ...
4    "targets": {
5        "build-base": {
6            "executor": "@nrwl/angular:ng-packagr-lite",
7            "outputs": ["dist/libs/my-lib"],
8            "options": {...},
9            "configurations": {...},
10            "defaultConfiguration": "production"
11        },
12        ...
13    },
14   ...
15},

Example repository

Check out the nx-incremental-large-repo for a live example.