This is an updated version of Angular CLI under the hood — builders demystified. The article is relevant for Angular 8+.
Hey folks. Hope the sun is shining for you today.
In the previous article we talked about customizing Angular build configuration without ejecting the underlying Webpack configuration.
The proposed solution was to use an existing custom builder.
Today we’ll take a look under the hood and create our own custom builder from scratch.
Angular CLI builders
Angular CLI 6 came with a new architecture, basically a rewrite of the old CLI, which was broken down into small pieces. Though its API was still experimental and subject to change.
Angular CLI 8 was an important milestone as this was the first time the experimental API has been well defined and stabilized.
Now we’re going to review the API and understand what it consists of and how to use it.
Here are 3 important parts of Angular CLI:
- Angular CLI package contains pre-defined commands, help and CLI related stuff.
- Architect package handles the configuration from angular.json. It is responsible for mapping the architect target into the relevant builder, create the builder and trigger it with the configuration specified in angular.json for this builder.
- Builders are the ones who do the actual job. Thus, BrowserBuilder runs Webpack build for browser target, KarmaBuilder starts the Karma server and runs Webpack build for unit tests and so on.
Angular CLI commands and architect targets
When you run
ng build or
ng test or any of the predefined Angular CLI commands a few things happen:
- Angular CLI command is transformed into a relevant architect target
- A relevant builder is created
- A relevant builder is triggered with the relevant configuration
When you run a custom architect target, the following happens:
- A relevant builder is created
- A relevant builder is triggered with the relevant configuration
As you can see the only difference between the predefined command and custom architect target is that in the latter there is no mapping from the Angular CLI command to an architect target.
In a nutshell there is one generic command
ng run , that receives an architect target as an argument (in format
project:target ) and asks the architect to execute this command.
Thus, each one of the predefined Angular CLI commands that are mapped to an architect target can be executed with
ng run. E.g:
ng run my-cool-project:build
ng run my-cool-project:test
And so on…
The beauty is that once you’ve created your own builder you can put it in any architect target you want:
You can create your own target, call it
my-target and execute it with
ng run my-cool-project:my-target
You can replace the builder in one of the existing targets (say,
build target) and execute it with the predefined Angular CLI command (
ng build ), because as we’ve seen, Angular CLI commands are just mappings into relevant architect targets.
Architect targets configuration
Let’s take a closer look on angular.json file:
Inside each project there is an entry called
architect and it contains architect targets configurations. Thus, in this particular example we have only one project called
example which, in turn, has two architect targets:
If you wanted to add another architect target called, say,
format , the file would become:
Every architect target configuration has 3 properties:
builder— path to the builder.
The format of the path is
[package-path]is a path to a folder with package.json containing builders entry and
[builder-name]is one of the entries in builders.json (we’ll return to this later)
options— the configuration of the builder. Must match the builder configuration schema, otherwise the command will fail.
configurations— a map of alternative target options (prod, dev etc.). This is an optional property.
That’s pretty much it for the theoretical background.
Enough talk, let’s do something real!
Creating you own builder
I’m not a fan of doing things in vain, so I had to come up with something more than just Hello World Builder, yet as simple as Hello World Builder.
So imagine you want to display the date and the time on which your application was built last time. The system is loading up, fetching some file that contains the timestamp for the latest build and the date is displayed in the page footer.
What we’re going to do is implement a builder that creates this timestamp file.
Creating the package
A single package can contain multiple builders but in our case it will contain only one.
First thing after you’ve created a folder for your builders package is adding package.json into this folder (architect assumes that builders package is an npm package).
This package.json is just a plain package.json file with one additional entry:
Spoiler: the file doesn’t have to be builders.json, can be any name you choose.
builders.json is a file that describes your builders. It’s a json file that follows Angular builders schema and has the following structure:
... more builders definitions
builders.json can contain definitions for multiple builders.
Each builder is defined by two properties:
Architect will parse the configuration and create an instance of the builder.
schema— path to json schema that defines builder configuration
optionsproperty in architect target definition).
Architect verifies the configuration against this schema and if the configuration is wrong it will fail the target.
Here what our builders.json will look like:
"description": "Builder that creates timestamp"
Let’s say we want to allow the user to modify the format of the timestamp and the name of the file to which the timestamp will be saved.
Thus, our schema.json will look like this:
"title": "Timestamp builder",
"description": "Timestamp builder options",
"description": "Timestamp format",
"description": "Path to the timestamp file",
If the user hasn’t specified any options in the architect target configuration, architect will pick up the defaults from the schema.
To format the Date we will use dateformat package, let’s install it:
npm i dateformat
We’re going to develop our builder with Typescript (though it’s not mandatory) so we have to install it too.
We will also seize the functionality of
@angular-devkit/core as well as some of the types from
To benefit from Typescript static typing we will probably want to install
This is it for devDependencies (
@angular-devkit will be used at runtime but rather as a peer dependency). Let’s install them:
npm i -D @angular-devkit/core @angular-devkit/architect @types/node @types/dateformat typescript
Now we’re ready to implement the builder itself.
First of all let’s define our builder configuration as an interface in schema.d.ts:
Another option is to generate the interface from
schema.json using quicktype.
Once we have the interface we can implement the skeleton of a builder:
createTimestamp is our builder handler function, which receives an input from the
architect and returns
Promise<BuilderOutput> or just
BuilderOutput . Which one of them is up to you to decide. In our example it will return an Observable.
Here is an official type definition of builder handler function, taken from here:
BuilderOutput is an object whose purpose is to notify the architect of the build result. Here is the typing (inferred from this schema):
BuilderOutput will notify the architect of successful or unsuccessful execution, and architect, in turn, will pass the execution result to CLI that will eventually finish the process with appropriate exit value.
Finally, we create the builder implementation by invoking
createBuilder function. This function essentially connects our builder handler to the architect and creates and actual builder. We then export the result of this function as
default export which is later consumed by architect.
Below is the
createBuilder typing taken from here:
Now when we understand the API, we can implement the builder.
In our case we want to return success if the file with the timestamp was successfully created and failure otherwise:
Let’s break it down:
- We get
- We retrieve the
formatfrom the options. These should be specified in architect target configuration in angular.json of host application. If none were specified, the default values will be taken from the builder’s schema.
getSystemPathis a utility function that returns system specific path.
We concatenate it with the relative
pathfrom the options, while using
normalizeto create a
Pathobject from a
- We use
fsmodule but since we have to return an Observable and
writeFileworks with callbacks, we use
bindNodeCallbackfunction to transform it into a function that returns Observable.
- We create a child logger to separate our builder output from the rest of the architect logs. All the
timestampbuilder logs will be prepended with Timestamp prefix.
- We format the date with the
formatDatefunction while using the
formatwe’ve got from the options and write the formatted date to the file.
- Finally we return success if the file was created successfully and return failure otherwise.
Side node: use the logger to provide build information to the user
Using the builder
Now when the builder is ready you can use it by either specifying a relative path to the folder in angular.json:
… or packing it into npm package and installing it locally:
cp angular-builders-timestamp-1.0.0.tgz [host-application-root]
npm i -D angular-builders-timestamp-1.0.0.tgz
… or publishing it on npm and installing from there.
I hope you enjoyed the article and understand the concept better now.
I also hope the sun is still shining and you didn’t spend all the day on this booooooring stuff.
If you’re into open source and have some brilliant ideas for a builder that can be useful for everyone, you’re welcome to contribute to the angular-builders project.
All the source code of the timestamp builder (as well as the example app that uses this builder) is available on GitHub.