What are Schematics?
Schematics are template-based code generators that support complex logic. They are the engine behindng generate commands and ng add package installations. Schematics can create, modify, delete, and move files while applying rules and templates to scaffold or modify code.
Think of schematics as “code recipes” - they describe how to generate or transform code in a repeatable, configurable way.
How Schematics Work
Schematics operate on a virtual file system called a tree. Changes are staged in memory before being committed to disk, allowing for validation and rollback.Tree
Virtual file system representing project structure
Rule
Transformation applied to the tree
Template
File template with placeholders
Action
Create, update, delete, or rename operations
Official Schematics
Angular provides built-in schematics for common tasks:Code Generation Schematics
- Component
- Service
- Module
- Directive
- Pipe
- Guard
- Component TypeScript file
- Component template (HTML)
- Component styles (CSS/SCSS)
- Component test file
--standalone: Create standalone component--inline-template: Use inline template--inline-style: Use inline styles--skip-tests: Skip test file--flat: Don’t create folder--export: Export from module
Migration Schematics
Angular provides powerful migration schematics:Standalone Migration
Standalone Migration
Convert components, directives, and pipes to standalone:Three-phase migration:Options:
1
Convert to Standalone
Convert declarations to standalone and update imports
2
Remove NgModules
Delete unnecessary NgModule classes
3
Update Bootstrap
Switch to
bootstrapApplication APImode: Migration mode (convert, prune, bootstrap)path: Relative path to migrate
Control Flow Migration
Control Flow Migration
Migrate to new control flow syntax:Transforms:After:
*ngIf→@if*ngFor→@for*ngSwitch→@switch
Signal Migrations
Signal Migrations
Migrate to signal-based APIs:Signal Inputs:Signal Queries:Output Migration:
Style Migrations
Style Migrations
NgStyle to Style:NgClass to Class:
Other Migrations
Other Migrations
Inject Migration:Router Testing Module:Cleanup Unused Imports:
All migration schematics support a
--path option to migrate specific directories.Creating Custom Schematics
Build your own code generators for common patterns in your application.Setup
1
Install Schematics CLI
2
Create Schematics Collection
3
Project Structure
Define Schema
schema.json:Implement Schematic
index.ts:Create Templates
files/__name@dasherize__/__name@dasherize__.component.ts:Template Functions
Angular provides utility functions for string transformation:Schematic Rules
Rules define transformations applied to the tree:Basic Rules
Chaining Rules
Conditional Rules
Advanced Features
Working with AST
Modify TypeScript files using Abstract Syntax Trees:External Schematics
Call other schematics from your schematic:Task Scheduling
Schedule tasks to run after schematic completes:Testing Schematics
Test schematics using the testing utilities:Publishing Schematics
1
Build the Package
2
Test Locally
3
Publish to npm
4
Use in Projects
Collection Configuration
collection.json:string
required
Path to schematic implementation and export name
string
Path to JSON schema file defining options
string[]
Alternative names for the schematic
Hide from
ng generate listBest Practices
Validation
Validation
Always validate options and check for existing files:
Logging
Logging
Provide helpful feedback:
Dry Run
Dry Run
Test with Shows what would be generated without creating files.
--dry-run flag:Path Handling
Path Handling
Use
normalize and join for cross-platform compatibility:Next Steps
CLI Overview
Return to CLI overview
Builders
Learn about custom builders
Resources
Schematics Guide
Official schematics documentation
Angular Schematics
Official Angular schematics source
DevKit Schematics
Schematics DevKit source
Example Schematics
Angular Core migration schematics
