Angular CLI 6 under the hood  - builders demystified
Tags: angular, javascript, tutorial, webdev
Published: 2018-08-07 15:43:44

---
title: Angular CLI 6 under the hood  - builders demystified
published: true
description: What are builders in Angular CLI 6, how do they work and how can you create your own builder.
tags: angular, javascript, tutorial, webdev
cover_image: https://cdn-images-1.medium.com/max/2000/1*jgXJNmCQW8-KxichJ-2Kgg.jpeg
---

### *Update*:
***The article is relevant up to Angular 7.2.x. 
In 7.3.0 this API has been deprecated (yet supported) and in 8.0.0 it will be replaced with the new API.
The updated article is yet to come.***

Hey folks. Hope the sun is shining for you today.

In the [previous article](https://dev.to/meltedspark/customizing-angular-cli-6-buildan-alternative-to-ng-eject-1oc4) we talked about customizing Angular 6 build configuration without ejecting the underlying webpack configuration.

The proposed solution was to use an *existing* [custom builder](https://github.com/meltedspark/angular-builders/tree/master/packages/custom-webpack).

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.

In fact, Angular CLI *itself* has nothing to do with the configuration you provide in *angular.json*, not anymore. Instead, it wraps [Angular Dev Kit](https://github.com/angular/angular-cli/tree/7.2.x/packages/angular_devkit) and triggers *architect targets*.

Briefly speaking:

* [Angular CLI
package](https://github.com/angular/angular-cli/tree/7.2.x/packages/angular/cli)
contains pre-defined commands, help and CLI related stuff.
* [Architect package](https://github.com/angular/angular-cli/tree/7.2.x/packages/angular_devkit/architect) 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](https://github.com/angular/angular-cli/tree/7.2.x/packages/angular_devkit/build_angular/src/browser) runs webpack build for browser target, [KarmaBuilder](https://github.com/angular/angular-cli/tree/7.2.x/packages/angular_devkit/build_angular/src/karma) 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](https://github.com/angular/angular-cli/wiki) 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 build`: `ng run my-cool-project:build`
* `ng test` : `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`

OR

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:

    {
      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
      "version": 1,
      "newProjectRoot": "projects",
      "projects": {
        "example": {
          "root": "",
          "sourceRoot": "src",
          "projectType": "application",
          "prefix": "app",
          "schematics": {},
          "architect": {
            "build": {
                ...
            },
            "serve": {
                ...
            },
          }          
        }
      }
    }

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: `build` and `serve`.
If you wanted to add another architect target called, say, `format` , the file would become:

    {
      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
      "version": 1,
      "newProjectRoot": "projects",
      "projects": {
        "example": {
          "root": "",
          "sourceRoot": "src",
          "projectType": "application",
          "prefix": "app",
          "schematics": {},
          "architect": {
            "build": {
                ...
            },
            "serve": {
                ...
            },            
            "format": {
                ...
            }
          }
        }
      }
    }

Every architect target configuration has 3 properties:

* `builder` — path to the builder.    
<br>The format of the path is `[package-path]:[builder-name]`, where `[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](https://docs.npmjs.com/files/package.json) file with one additional entry:

    "builders": "builders.json"

*Spoiler: the file doesn’t have to be builders.json, can be any name you choose.*

### builders.json

`builders.json` is a file that describes your builders. It’s a json file that
follows Angular builders [schema](https://github.com/angular/angular-cli/blob/7.2.x/packages/angular_devkit/architect/src/builders-schema.json) and has the following structure:

    {
      "$schema": "@angular-devkit/architect/src/builders-schema.json",
      "builders": {
        "builder-name": {
          "class": "path-to-builder-class",
          "schema": "path-to-builder-schema",
          "description": "builder-description"
        },
        ... more builders definitions
      }
    }

Single `builders.json` can contain definitions for multiple builders.

### Builder definition

Each builder is defined by two properties:

1.  `class` — path to Javascript class that implements `Builder` interface.
Architect will parse the configuration and create an instance of this class.
You can find the definition of the interface [here](https://github.com/angular/angular-cli/blob/7.2.x/packages/angular_devkit/architect/src/architect.ts).
2.  `schema` — path to json schema that defines builder configuration
<br>( `options` property in architect target definition).    
<br>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:

    {
      "$schema": "@angular-devkit/architect/src/builders-schema.json",
      "builders": {
        "file": {
          "class": "./timestamp.builder.js",
          "schema": "./schema.json",
          "description": "Builder that creates timestamp"
        }
      }
    }

### schema.json

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:

    {
      "id": "TimestampBuilderSchema",
      "title": "Timestamp builder",
      "description": "Timestamp builder options",
      "properties": {
        "format": {
          "type": "string",
          "description": "Timestamp format",
          "default": "dd/mm/yyyy"
        },
        "path": {
          "type": "string",
          "description": "Path to the timestamp file",
          "default": "./timestamp"
        }
      }
    }

If the user hasn’t specified any options in the architect target configuration, architect will pick up the defaults from the schema.

### Installing dependencies

To format the Date we will use [dateformat](https://www.npmjs.com/package/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 interfaces from `@angular-devkit/architect`. 
To benefit from Typescript static typing we will probably want to install `@types` for `node` and `dateformat`.

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

### The builder

Now we’re ready to implement the builder itself.<br> First of all let’s define our builder configuration as an interface in *schema.d.ts*:

{% gist https://gist.github.com/meltedspark/dfc5d317d4944be439c73a79f05c152e %}

Once we have the interface we can implement the generic `Builder` interface:

{% gist https://gist.github.com/meltedspark/3c70e8201d2c6f7ddbb205e3f5a8640f %}

`run` method should return an Observable of `BuildEvent` that looks like this:

{% gist https://gist.github.com/meltedspark/aeef232baceb65998ad6e41a2e1dc3da %}

`BuildEvent` 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.

In our case we want to return success if the file with the timestamp was
successfully created and failure otherwise:

{% gist https://gist.github.com/meltedspark/b206fea21f78a30e8f6c44a6b989de21 %}

Let’s break it down:

1.  First of all we get the root (which is the root folder of the host application)
2.  Next we retrieve the `path` and the `format` from 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.
3.  `getSystemPath` is a utility function that returns system specific path and we concatenate it with the relative `path` from the options.
4.  We use `writeFile` function from `fs` module but since we have to return an Observable and `writeFile` works with callbacks, we use `bindNodeCallback` function to transform it into a function that returns Observable.
5.  We format the date with the `formatDate` function while using the `format` we’ve got from the options and write the formatted date to the file.
6.  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*

Compile the source code to JavaScript and you’re good to go.

### 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:*

    "architect": {
            "timestamp": {
              "builder": "[relative-path-to-package]/timestamp:file",
              "options": {}
            }
    }

… or packing it into npm package and installing it locally:

    npm pack
    cp angular-builders-timestamp-1.0.0.tgz [host-application-root]
    cd [host-application-root]
    npm i -D angular-builders-timestamp-1.0.0.tgz

*angular.json:*

    "architect": {
            "timestamp": {
              "builder": "@angular-builders/timestamp:file",
              "options": {}
            }
    }

… or publishing it on npm and installing from there.

## Finishing words

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 idea for a builder that can be useful for everyone, you’re welcome to contribute to [angular-builders](https://github.com/meltedspark/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](https://github.com/meltedspark/angular-builders/tree/master/packages/timestamp).