Angular CLI Builders

Santosh Yadav
Aug 6, 2019 · 3 min read

In this blog post, we will discuss Angular CLI Builders, in case you are following Angular, with version 8 Builders are talked about a lot. So let’s see how we can use the Builders API.

I will be speaking at Ng Srilanka this year, and we are looking for sponsors if you are an organization or your organization wants to sponsor some community-driven conference please get in touch with the organizers.

Introduction

Builders API is released with Angular 8, and it offers a functionality where you can override commands as ng build,ng test and ng lint . Not to be confused with Angular Schematics here, by using which you can add your own commands for ng generate commands or provide support for ng add .

History

Prior to Angular 8, the only way to override the build process was to extract the default webpack config provided by Angular and adding your code. I know like me, you might have hated this overhead, and after ejecting you can not use CLI commands as it used to be replaced with other commands.’

Below is the snippet from .angular-cli.json which was used prior to Angular 6, you can notice it simply takes a config file and build serve and build the section is not even available.

And let's see the snippet for the same in the Angular 8 project. For visibility, I have removed other sections, but you can notice now all commands have an option called builder where there are pre-defined builders from angular and you can easily override by providing your own builder.

CLI builders API

With Angular 8 release the CLI Builders API was made public, and with Builders being a public API, it opened a lot of possibilities for Angular lovers.

With CLI builders it has become easy to create and use your own builders without ejecting the webpack config. Even it is not possible to ng eject command anymore.

Let’s create our own builder to see how it works and how you can create your own.

There are already many custom builders which are available before you create your own check Angular Builders to see if one already exists

To create a new builders project you can use the sample project provided on Angular Docs.

Before we can write our own builder let’s take a look at the API.

  1. BuilderContext
  2. BuilderOutput
  3. createBuilder

BuilderContext

Let's see the useful methods and properties provided:

  • reportStatus: Use reportStatus method to report status on running tasks to the command prompt.
  • logger: logger provides methods like log, debug, info, warn, error and fatal for logging purpose.
  • scheduleTarget: this method lets us schedule other tasks with provided config, a simple example is running build task as part of your own task.

createBuilder

This is the entry point into the builder, it can return Promise or Observable of type BuilderOutput

BuilderOutput

BuilderOutput can return below values:

  • error: error to be sent to the application, this is optional value
  • info: this is returns [key, value] pair and optional.
  • success: can return true/false value and is required.
  • target: it can return configuration project and target .

For example, let’s modify the existing sample code and add a task to build and run the test together with the new custom builder.

  1. Replace the code in downloaded repository index.ts file with below code:

2. To test the builder locally run the below command npm run build

3. Run npm link to link the package locally

4. Create a new Angular Application using CLI

5. run npm link @example/command-runner you can change the package name as per your package name mentioned in builder’s package.json

6. Add below configuration to angular.json

"[your-command]": {
"builder": "@example/command-runner:command",
"options": {
"command": "[your-command]",
"args": [
"src/main.ts"
]
}
}

7. To test the builder runng run [project-name]: [your-command] or ng deploy if your command is deploythis is available after Angular CLI 8.3.0.

You can also publish this on npm to use it across multiple applications.

Conclusion

The use case for builders API is a lot bigger there are many custom builders that are already available. The custom builder to deploy became so popular that the CLI team decided to make ng deploy command available.

If you love Netlify here is and Custom Builder to deploy the application from CLI https://github.com/ngx-builders/netlify-builder

Both projects are open source, so contributions are welcome.

Angular In Depth

The place where advanced Angular concepts are explained

Santosh Yadav

Written by

Santosh is GDE for Angular and Web Technologies, he is open source contributor for Angular, NgRx and Writer at AngularInDepth and DotNetTricks.

Angular In Depth

The place where advanced Angular concepts are explained

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade