02 Oct 2019
2 min

Overriding Angular Schematics

In this blog post, we will learn how we can override some default options in Angular Schematics.

Introduction

Last week I was working on an Angular Library, and in all the components we wanted to have encapsulation set to None and changeDetection to OnPush. We also wanted to add all components and directives to exports, so it can be utilized in other modules.

We realized it will be a waste of effort to add it manually for each component and module.

Solution

The solution to avoid this is to use angular.json and now you must be like Joey and who wants to see the solution.

And it's not only for components we can override Schematics options from directives, classes, services and many more. Feeling like below guy.

We are getting there just hold on, we will do all this without writing any code.

Overriding Schematics Options

Every developer is aware of angular.json and most of the time we keep using default options provided and don’t bother into looking for what more it has to offer.

So let’s look at the options provided byangular.json create one Angular application using CLI.

Below is how your angular.json looks like.

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

And the option we are interested in here is schematics . It opens up a lot of possibilities for us.

As you can see above once we move inside schematics options we can override several options by selecting one of the below, whichever you want to override.

  • @schematics/angular:component
  • @schematics/angular:class
  • @schematics/angular:directive
  • @schematics/angular:module
  • @schematics/angular:pipe
  • @schematics/angular:service

We will take an example of @schematics/angular:component in your project go ahead and below json.

"@schematics/angular:component": {
  "changeDetection": "OnPush",
  "inlineTemplate": true,
  "viewEncapsulation": "None"
}

The next step is to run the below command to create a component.

ng generate component home

And notice the difference from the earlier generated component, you will have no .html file and changeDetection and viewEncapsulation properties will be available for you.

Conclusion

We are not covering all properties, as this will be for you to explore and use the most suitable options for your project. Schematics is really powerful, and this is what drives various commands in Angular CLI like generate component, generate service, but you may not need default options and may need to override some properties of Schematics, by using these options no need to make changes manually. Go ahead and automate and decide the best options for your project.

Frequently asked questions

How do I set default options for generated Angular components?

Set component schematic options in the schematics object for your project in angular.json. Add an @schematics/angular:component entry with the defaults you want, such as "changeDetection": "OnPush" and "viewEncapsulation": "None". New components generated with ng generate component then use those configured options, so you do not have to add them manually each time.

How can I generate Angular components with an inline template?

Set "inlineTemplate": true under @schematics/angular:component in your project’s angular.json schematics configuration. When you run a command such as ng generate component home, the generated component uses an inline template instead of a separate .html file. This option applies to generated components; it does not convert templates in components that already exist.

Can I configure Angular CLI defaults for directives, services, and other schematics?

Yes, angular.json lets you configure options for multiple Angular schematics, including @schematics/angular:directive, @schematics/angular:service, @schematics/angular:class, @schematics/angular:module, and @schematics/angular:pipe. Add an entry for the schematic you want to customize under the project’s schematics object. The available options depend on that schematic.

Does changing Angular schematic options update existing components?

No, changing schematic options in angular.json sets defaults for components generated afterward; it does not automatically modify existing components. For example, configuring changeDetection as OnPush affects components created with the component schematic, but existing component files still need to be changed separately if you want them to use that setting.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.