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.

Use buildable libraries

To enable incremental builds you need to use buildable libraries.

Directory Flag Behavior Changes

The command below uses the as-provided directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived option, omit the --directory flag. See the as-provided vs. derived documentation for more details.

You can generate a new buildable library with:

โฏ

nx g @nx/angular:lib libs/my-lib --buildable

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

1{ 2 "projectType": "library", 3 ... 4 "targets": { 5 "build": { 6 "executor": "@nx/angular:ng-packagr-lite", 7 "outputs": [ 8 "{workspaceRoot}/dist/libs/my-lib" 9 ], 10 "options": { 11 ... 12 }, 13 "configurations": { 14 ... 15 }, 16 "defaultConfiguration": "production" 17 }, 18 ... 19 }, 20 ... 21}, 22
More details

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 @nx/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 @nx/angular:lib libs/my-lib --publishable --importPath my-lib).

Adjust the application executor

Angular Esbuild Performance

From internal testing done at Nx, the build time saved from using incremental builds when using Esbuild with Angular is not as effective as the time saved when using Webpack with Angular. Angular's build time with Esbuild already provides a great performance boost and therefore overall time saved may not warrant using incremental builds with Esbuild for Angular

Change your Angular applicationโ€™s "build" target executor to Nx's version of builder you're currently using and the " serve" target executor to @nx/angular:dev-server as shown below.

  • @angular-devkit/build-angular:application -> @nx/angular:application
  • @angular-devkit/build-angular:browser-esbuild -> @nx/angular:browser-esbuild
  • @angular/build:browser -> @nx/angular:webpack-browser
project.json
1{ 2 "projectType": "application", 3 ... 4 "targets": { 5 "build": { 6 "dependsOn": ["^build"], 7 "executor": "@nx/angular:application", 8 "outputs": [ 9 "{options.outputPath}" 10 ], 11 "options": { 12 "buildLibsFromSource": false 13 ... 14 }, 15 "configurations": { 16 ... 17 }, 18 "defaultConfiguration": "production" 19 }, 20 "serve": { 21 "executor": "@nx/angular:dev-server", 22 "options": { 23 "buildTarget": "my-app:build", 24 "buildLibsFromSource": false 25 }, 26 "configurations": { 27 "production": { 28 "buildTarget": "my-app:build:production" 29 } 30 } 31 }, 32 ... 33 } 34}, 35

Add Executor to Target Defaults

If you'd like to avoid adding "dependsOn": ["^build"] to every application in your workspace that uses one of the required executors you can add it to the "targetDefaults" section of the nx.json:

nx.json
1{ 2 "targetDefaults": { 3 "@nx/angular:application": { 4 "dependsOn": ["^build"] 5 } 6 } 7} 8

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

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: @nx/angular:application, @nx/angular:browser-esbuild, @nx/angular:webpack-browser, @nx/angular:package and @nx/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:

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

And the targetDefaults configured in the nx.json as:

1{ 2 "targetDefaults": { 3 "build": { 4 "dependsOn": ["build-base"] 5 }, 6 "build-base": { 7 "dependsOn": ["^build-base"] 8 } 9 } 10} 11

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": "@nx/angular:ng-packagr-lite", 7 "outputs": [ 8 "{workspaceRoot}/dist/libs/my-lib" 9 ], 10 "options": { 11 ... 12 }, 13 "configurations": { 14 ... 15 }, 16 "defaultConfiguration": "production" 17 }, 18 ... 19 }, 20 ... 21}, 22

Example repository

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