@angular/compiler
The Angular compiler package provides APIs for compiling Angular templates and components into executable JavaScript code. This package is primarily used internally by the Angular framework and build tools.Overview
The@angular/compiler package handles the transformation of Angular templates, components, and decorators into optimized JavaScript code that can be executed by the browser. It includes:
- Template parsing and compilation
- Expression parsing and evaluation
- Code generation for Ahead-of-Time (AOT) compilation
- Just-in-Time (JIT) compilation support
- Internationalization (i18n) support
- Style encapsulation
Installation
Key Features
Template Compilation
The compiler transforms Angular templates into efficient render instructions:Expression Parser
Parses Angular template expressions and bindings:i18n Support
Provides internationalization capabilities for templates:Core APIs
Compiler Configuration
Configuration options for the Angular compiler
Template Parser
Parses an Angular template into an Abstract Syntax Tree (AST)
Expression Parser
Parses Angular expressions in templates
Metadata Types
Component Metadata
The compiler works with component metadata to generate code:View Encapsulation
Emulated
Emulate Shadow DOM using prefixed CSS (default)
ShadowDom
Use native Shadow DOM encapsulation
None
No style encapsulation
Compilation Modes
Ahead-of-Time (AOT)
Compiles templates during the build process:Just-in-Time (JIT)
Compiles templates in the browser at runtime:Advanced Features
Template AST
The compiler generates multiple AST representations:Template AST Nodes
Template AST Nodes
- Element: HTML elements (
<div>,<span>, etc.) - Text: Text content and interpolations
- BoundAttribute: Property bindings
[property]="value" - BoundEvent: Event bindings
(event)="handler()" - Reference: Template references
#ref - Variable: Template variables
let item - DeferredBlock: Deferrable views
@defer - IfBlock: Conditional blocks
@if - ForLoopBlock: Loop blocks
@for - SwitchBlock: Switch blocks
@switch
Render3 Compiler
The modern Ivy compiler (Render3):Output AST
Low-level code generation AST:Schema and Validation
DOM Security Schema
The compiler includes a security schema to prevent XSS attacks by validating property bindings and sanitizing values.
Element Schema Registry
Internationalization (i18n)
Message Extraction
Message ID Generation
Constant Pool
The compiler uses a constant pool to deduplicate and optimize generated code:Best Practices
Use AOT Compilation
Always use AOT compilation for production builds to get smaller bundles and better performance
Avoid Direct Usage
Avoid using compiler APIs directly unless building Angular tooling
Type Safety
Enable strict template checking for better type safety
Template Analysis
Use the Angular Language Service for template analysis during development
Related Packages
@angular/compiler-cli
Command-line interface for the Angular compiler
@angular/platform-browser-dynamic
JIT compilation support for browsers
Resources
Angular Compiler Guide
Learn about AOT compilation
Template Syntax
Template syntax reference
View Encapsulation
Component styling and encapsulation
i18n Guide
Internationalization documentation
Exports Reference
Core Exports
core- Core compiler utilitiesoutputAst- Output AST for code generationCompilerConfig- Compiler configurationConstantPool- Constant pool management
Template Parsing
parseTemplate()- Template parsermakeBindingParser()- Binding parser factoryTmplAst*- Template AST node types
Expression Parsing
Parser- Expression parserLexer- Expression lexerAST- Expression AST types
Code Generation
compileComponentFromMetadata()- Component compilercompileDirectiveFromMetadata()- Directive compilercompileNgModule()- NgModule compilercompilePipeFromMetadata()- Pipe compiler
Render3 (Ivy)
R3Identifiers- Ivy runtime identifiersR3*Metadata- Metadata types for compilation- Compilation functions for declarations
Schema & Validation
SECURITY_SCHEMA- DOM security schemaDomElementSchemaRegistry- Element schema registryCUSTOM_ELEMENTS_SCHEMA- Custom elements schemaNO_ERRORS_SCHEMA- Disable schema validation
Internationalization
I18NHtmlParser- i18n HTML parserSerializer- Message serializerscomputeMsgId()- Message ID generation
Version CompatibilityThis documentation reflects the latest stable version of Angular. API signatures and behavior may vary between versions.
