Sub topic 17
Schematics and builders: how the CLI is extended
This chapter assumes you have read the chapters on angular.json and ng generate. It covers the two extension points behind most CLI commands.
Two kinds of plugin
The CLI itself does little work. It reads angular.json and hands the job to a plugin.
- A schematic is a code generator or code transformer. It reads and edits your source files.
ng generate,ng addandng updaterun schematics. - A builder is a function that runs a task on your project.
ng build,ng serveandng testrun builders.
Schematics
A schematic works on a virtual file tree, not directly on disk. It makes its changes in memory, and the CLI writes them out only if the whole run succeeds. This is why ng generate --dry-run can show what would change without touching anything.
Schematics are grouped in a collection. The ng generate component command uses the component schematic from the @schematics/angular collection. You can name the collection explicitly:
ng generate @schematics/angular:component products/product-listYou can change the defaults of a schematic in angular.json, so you do not repeat flags:
{
"projects": {
"my-app": {
"schematics": {
"@schematics/angular:component": {
"style": "scss",
"changeDetection": "OnPush"
}
}
}
}
}A custom schematic is a function that returns a Rule. This one creates a file:
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
export function hello(options: { name: string }): Rule {
return (tree: Tree, _context: SchematicContext) => {
tree.create(`hello-${options.name}.txt`, `Hello, ${options.name}!`);
return tree;
};
}A collection.json file registers it, and the factory value points to the exported function:
{
"schematics": {
"hello": {
"description": "Creates a greeting file.",
"factory": "./hello/index#hello"
}
}
}Library authors use the same mechanism. When a library ships an ng-add schematic, ng add installs the package and then runs that schematic to configure your project.
Builders
Each entry under architect in angular.json is a target. The builder property names the function that runs it, in the form package:builder-name. In the earlier excerpt, @angular/build:application is the builder for the build target. The options and configurations objects are passed to it as input.
A custom builder uses createBuilder:
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
interface Options {
message: string;
}
export default createBuilder(
async (options: Options, context: BuilderContext): Promise<BuilderOutput> => {
context.logger.info(options.message);
return { success: true };
}
);After you register it in the package's builders.json, you can add a target that uses it:
"announce": {
"builder": "my-builders:announce",
"options": { "message": "Build starting" }
}Then run it with ng run my-app:announce.
Common mistakes
- Treating a schematic as a runtime feature. Schematics only change files during a CLI run. They are not part of your shipped application.
- Forgetting that builder
optionsare validated against the builder's schema. A misspelled option fails the target before the build starts.
Exercise
You want every new component in your project to use SCSS and OnPush change detection without typing flags. Which part of the CLI do you configure, and where?
Show answer
Configure the defaults of the component schematic. Add a "schematics" entry for "@schematics/angular:component" with "style": "scss" and "changeDetection": "OnPush" in the project's section of angular.json.