In this guide we'll specifically look into which changes need to be made to enable incremental builds for Angular applications.
Use buildable libraries
Section titled “Use buildable libraries”To enable incremental builds you need to use buildable libraries.
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:
{ "projectType": "library", ... "targets": { "build": { "executor": "@nx/angular:ng-packagr-lite", "outputs": [ "{workspaceRoot}/dist/libs/my-lib" ], "options": { ... }, "configurations": { ... }, "defaultConfiguration": "production" }, ... }, ...},
Adjust the application executor
Section titled “Adjust the application executor”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
{ "projectType": "application", ... "targets": { "build": { "dependsOn": ["^build"], "executor": "@nx/angular:application", "outputs": [ "{options.outputPath}" ], "options": { "buildLibsFromSource": false ... }, "configurations": { ... }, "defaultConfiguration": "production" }, "serve": { "executor": "@nx/angular:dev-server", "options": { "buildTarget": "my-app:build", "buildLibsFromSource": false }, "configurations": { "production": { "buildTarget": "my-app:build:production" } } }, ... }},
{ "projectType": "application", ... "targets": { "build": { "dependsOn": ["^build"], "executor": "@nx/angular:browser-esbuild", "outputs": [ "{options.outputPath}" ], "options": { "buildLibsFromSource": false ... }, "configurations": { ... }, "defaultConfiguration": "production" }, "serve": { "executor": "@nx/angular:dev-server", "options": { "buildTarget": "my-app:build", "buildLibsFromSource": false }, "configurations": { "production": { "buildTarget": "my-app:build:production" } } }, ... }},
{ "projectType": "application", ... "targets": { "build": { "dependsOn": ["^build"], "executor": "@nx/angular:webpack-browser", "outputs": [ "{options.outputPath}" ], "options": { "buildLibsFromSource": false ... }, "configurations": { ... }, "defaultConfiguration": "production" }, "serve": { "executor": "@nx/angular:dev-server", "options": { "buildTarget": "my-app:build", "buildLibsFromSource": false }, "configurations": { "production": { "buildTarget": "my-app:build:production" } } }, ... }},
Add Executor to Target Defaults
Section titled “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
:
{ "targetDefaults": { "@nx/angular:application": { "dependsOn": ["^build"] } }}
{ "targetDefaults": { "@nx/angular:browser-esbuild": { "dependsOn": ["^build"] } }}
{ "targetDefaults": { "@nx/angular:webpack-browser": { "dependsOn": ["^build"] } }}
Running and serving incremental builds
Section titled “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
Section titled “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:
{ "projectType": "application", ... "targets": { "build-base": { "executor": "@nx/angular:webpack-browser", "outputs": [ "{options.outputPath}" ], "options": { "buildLibsFromSource": false ... }, "configurations": { ... } }, "build": { "executor": "nx:run-commands", "outputs": [ "{options.outputPath}" ], "options": { "commands": [ "node ./tools/scripts/important-script.js", "node ./tools/scripts/another-important-script.js" ], ... }, "configurations": { ... } }, "serve": { "executor": "@nx/angular:dev-server", "options": { "buildTarget": "my-app:build-base", "buildLibsFromSource": false }, "configurations": { "production": { "buildTarget": "my-app:build-base:production" } } }, ... }},
And the targetDefaults
configured in the nx.json
as:
{ "targetDefaults": { "build": { "dependsOn": ["build-base"] }, "build-base": { "dependsOn": ["^build-base"] } }}
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
:
{ "projectType": "library", ... "targets": { "build-base": { "executor": "@nx/angular:ng-packagr-lite", "outputs": [ "{workspaceRoot}/dist/libs/my-lib" ], "options": { ... }, "configurations": { ... }, "defaultConfiguration": "production" }, ... }, ...},
Example repository
Section titled “Example repository”Check out the nx-incremental-large-repo for a live example.
Example repository/nrwl/nx-incremental-large-repo