reference-compiler-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAngular Compiler CLI (ngtsc
) Architecture
ngtscAngular Compiler CLI(ngtsc
)架构
ngtscOverview
概述
The package contains the Angular Compiler (Ivy), often referred to as . It is a wrapper around the TypeScript compiler () that extends it with Angular-specific capabilities.
packages/compiler-clingtsctscThe core goal of is to compile Angular decorators (like , , ) into static properties on the class (Ivy instructions, e.g., ). It also performs template type checking and ahead-of-time (AOT) compilation.
ngtsc@Component@Directive@Pipestatic ɵcmp = ...packages/compiler-clingtsctscngtsc@Component@Directive@Pipestatic ɵcmp = ...Mental Model
思维模型
The compiler is designed as a lazy, incremental, and partial compilation pipeline.
- Wrapper Pattern: wraps the standard
NgtscProgram. It intercepts calls to act as a drop-in replacement for standard tooling.ts.Program - Traits System: Every class with an Angular decorator is considered a "Trait". The compiler manages the state of these traits through a state machine:
- Pending: Detected but not processed.
- Analyzed: Metadata extracted, template parsed (but dependencies not yet linked).
- Resolved: Dependencies (directives/pipes in template) resolved, import cycles handled.
- Skipped: Not an Angular class.
- Lazy Analysis: Analysis only happens when necessary (e.g., when diagnostics are requested or emit is prepared).
- Output AST: The compiler generates an intermediate "Output AST" () for the generated code, which is then translated into TypeScript AST nodes during the emit phase.
o.Expression
该编译器被设计为惰性、增量式且部分编译的流水线。
- 包装器模式:包装标准的
NgtscProgram。它拦截调用,作为标准工具的直接替代方案。ts.Program - 特质系统:每个带有Angular装饰器的类都被视为一个“Trait”。编译器通过状态机管理这些特质的状态:
- Pending(待处理):已检测到但未处理。
- Analyzed(已分析):元数据已提取,模板已解析(但依赖尚未关联)。
- Resolved(已解析):依赖(模板中的指令/管道)已解析,导入循环已处理。
- Skipped(已跳过):非Angular类。
- 惰性分析:仅在必要时才进行分析(例如,当请求诊断信息或准备输出时)。
- 输出AST:编译器为生成的代码生成中间“输出AST”(),然后在输出阶段将其转换为TypeScript AST节点。
o.Expression
Key Subsystems
核心子系统
1. Core Orchestration (ngtsc/core
)
ngtsc/core1. 核心编排(ngtsc/core
)
ngtsc/core- : The public API implementing
NgtscProgram. It manages theapi.Programand thets.Program.NgCompiler - : The brain of the compiler. It orchestrates the compilation phases (Analysis, Resolution, Type Checking, Emit). It holds the
NgCompiler.TraitCompiler
- :实现
NgtscProgram的公开API。它管理api.Program和ts.Program。NgCompiler - :编译器的核心。它编排编译阶段(分析、解析、类型检查、输出)。它持有
NgCompiler。TraitCompiler
2. Trait Compilation (ngtsc/transform
)
ngtsc/transform2. 特质编译(ngtsc/transform
)
ngtsc/transform- : Manages the lifecycle of "Traits". It iterates over source files, identifies decorated classes, and delegates to the appropriate
TraitCompiler.DecoratorHandler - : A state container for a class, holding its handler, analysis results, and resolution results.
Trait
- :管理“Trait”的生命周期。它遍历源文件,识别带有装饰器的类,并委托给相应的
TraitCompiler。DecoratorHandler - :类的状态容器,包含其处理程序、分析结果和解析结果。
Trait
3. Decorator Handlers (ngtsc/annotations
)
ngtsc/annotations3. 装饰器处理程序(ngtsc/annotations
)
ngtsc/annotations- : An interface for handling specific decorators.
DecoratorHandler - : The most complex handler. It:
ComponentDecoratorHandler- Extracts metadata (selector, inputs, outputs).
- Parses the template.
- Resolves used directives and pipes ().
R3TargetBinder - Generates the instruction.
ɵcmp
- ,
DirectiveDecoratorHandler,PipeDecoratorHandler: Handle their respective decorators.NgModuleDecoratorHandler
- :用于处理特定装饰器的接口。
DecoratorHandler - :最复杂的处理程序。它:
ComponentDecoratorHandler- 提取元数据(选择器、输入、输出)。
- 解析模板。
- 解析使用的指令和管道(通过)。
R3TargetBinder - 生成指令。
ɵcmp
- 、
DirectiveDecoratorHandler、PipeDecoratorHandler:处理各自对应的装饰器。NgModuleDecoratorHandler
4. Template Type Checking (ngtsc/typecheck
)
ngtsc/typecheck4. 模板类型检查(ngtsc/typecheck
)
ngtsc/typecheck- : Generates "Type Check Blocks" (TCBs). A TCB is a block of TypeScript code that represents the template's logic in a way
TemplateTypeCheckercan understand and check for errors.tsc - : The actual generated code that validates bindings, events, and structural directives.
TypeCheckBlock
- :生成“类型检查块”(TCB)。TCB是一段TypeScript代码,以
TemplateTypeChecker可以理解并检查错误的方式表示模板逻辑。tsc - :实际生成的代码,用于验证绑定、事件和结构指令。
TypeCheckBlock
5. Metadata & Scope (ngtsc/metadata
, ngtsc/scope
)
ngtsc/metadatangtsc/scope5. 元数据与作用域(ngtsc/metadata
、ngtsc/scope
)
ngtsc/metadatangtsc/scope- : Reads Angular metadata from source files (using
MetadataReader) andLocalMetadataRegistryfiles (using.d.ts).DtsMetadataReader - : Determines the "compilation scope" of a component (which directives/pipes are available to it), handling
ScopeRegistrytransitive exports and Standalone Component imports.NgModule
- :从源文件(使用
MetadataReader)和LocalMetadataRegistry文件(使用.d.ts)中读取Angular元数据。DtsMetadataReader - :确定组件的“编译作用域”(哪些指令/管道可用于该组件),处理
ScopeRegistry的传递导出和独立组件的导入。NgModule
6. Emit & Transformation (ngtsc/transform
)
ngtsc/transform6. 输出与转换(ngtsc/transform
)
ngtsc/transform- : A TypeScript transformer factory.
ivyTransformFactory - : Visits classes, triggers compilation via
IvyCompilationVisitor, and collects the Output AST.TraitCompiler - : Translates the Output AST into TypeScript AST, injects the
IvyTransformationVisitorfields, and removes the original decorators.static ɵ...
- :TypeScript转换器工厂。
ivyTransformFactory - :遍历类,通过
IvyCompilationVisitor触发编译,并收集输出AST。TraitCompiler - :将输出AST转换为TypeScript AST,注入
IvyTransformationVisitor字段,并移除原始装饰器。static ɵ...
Compilation Phases
编译阶段
- Construction: creates
NgtscProgram, which sets up all registries and theNgCompiler.TraitCompiler - Analysis ():
analyzeSync- The scans files.
TraitCompiler - s extract metadata and parse templates.
DecoratorHandler - No cross-file resolution happens here (allowing for parallelism and caching).
- The
- Resolution ():
resolve- resolves traits.
TraitCompiler - Components link their templates to specific Directives and Pipes (found via ).
ScopeRegistry - Import cycles are detected and handled (e.g., via "remote scoping").
- Type Checking:
- creates TCBs for all components.
TemplateTypeChecker - TypeScript diagnostics are retrieved for these TCBs.
- Emit ():
prepareEmit- is created.
ivyTransformFactory - TS is called.
emit - The transformers run, injecting the compiled Ivy instructions into the JS/DTS output.
- 构建:创建
NgtscProgram,后者设置所有注册表和NgCompiler。TraitCompiler - 分析():
analyzeSync- 扫描文件。
TraitCompiler - 提取元数据并解析模板。
DecoratorHandler - 此阶段不进行跨文件解析(允许并行处理和缓存)。
- 解析():
resolve- 解析特质。
TraitCompiler - 组件将其模板与特定的指令和管道关联(通过查找)。
ScopeRegistry - 检测并处理导入循环(例如,通过“远程作用域”)。
- 类型检查:
- 为所有组件创建TCB。
TemplateTypeChecker - 从这些TCB中获取TypeScript诊断信息。
- 输出():
prepareEmit- 创建。
ivyTransformFactory - 调用TS的方法。
emit - 转换器运行,将编译后的Ivy指令注入到JS/DTS输出中。
- 创建
Important File Locations
重要文件位置
- : Entry point (
packages/compiler-cli/src/ngtsc/program.ts).NgtscProgram - : Core logic (
packages/compiler-cli/src/ngtsc/core/src/compiler.ts).NgCompiler - : Trait state machine.
packages/compiler-cli/src/ngtsc/transform/src/trait.ts - : Component compilation logic.
packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts - : Type checking logic.
packages/compiler-cli/src/ngtsc/typecheck/src/template_type_checker.ts - : AST transformation logic.
packages/compiler-cli/src/ngtsc/transform/src/transform.ts
- :入口点(
packages/compiler-cli/src/ngtsc/program.ts)。NgtscProgram - :核心逻辑(
packages/compiler-cli/src/ngtsc/core/src/compiler.ts)。NgCompiler - :特质状态机。
packages/compiler-cli/src/ngtsc/transform/src/trait.ts - :组件编译逻辑。
packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts - :类型检查逻辑。
packages/compiler-cli/src/ngtsc/typecheck/src/template_type_checker.ts - :AST转换逻辑。
packages/compiler-cli/src/ngtsc/transform/src/transform.ts