The metadata collection system analyzes TypeScript source files and extracts decorator metadata that would otherwise be lost during TypeScript compilation. This information is essential for Angular's template compiler and other build-time optimizations.
Core class that collects decorator metadata from TypeScript source files.
/**
* Collects decorator metadata from TypeScript modules
*/
class MetadataCollector {
constructor(options?: CollectorOptions);
/**
* Extract metadata from a TypeScript source file
* @param sourceFile - TypeScript source file to analyze
* @param strict - Whether to use strict metadata collection
* @returns Module metadata containing decorator information
*/
getMetadata(sourceFile: ts.SourceFile, strict?: boolean): ModuleMetadata;
}Usage Example:
import { MetadataCollector } from "@angular/tsc-wrapped";
import * as ts from "typescript";
const collector = new MetadataCollector({
version: 3,
quotedNames: false,
verboseInvalidExpression: false
});
// Create or get a TypeScript source file
const sourceFile = ts.createSourceFile(
"example.ts",
"export class MyComponent { @Component() }",
ts.ScriptTarget.ES2015
);
const metadata = collector.getMetadata(sourceFile, true);
console.log("Collected metadata:", metadata);Configuration options for customizing metadata collection behavior.
/**
* Options for configuring metadata collection
*/
interface CollectorOptions {
/** Version of the metadata format to use (default: 3) */
version?: number;
/** Whether to quote property names in metadata (default: false) */
quotedNames?: boolean;
/** Whether to provide verbose error messages for invalid expressions (default: false) */
verboseInvalidExpression?: boolean;
}Usage Example:
import { MetadataCollector, CollectorOptions } from "@angular/tsc-wrapped";
const options: CollectorOptions = {
version: 3,
quotedNames: true,
verboseInvalidExpression: true
};
const collector = new MetadataCollector(options);The root metadata structure representing a TypeScript module.
/**
* Metadata structure for a TypeScript module
*/
interface ModuleMetadata {
/** Symbolic marker indicating this is module metadata */
__symbolic: 'module';
/** Version of the metadata format */
version: number;
/** Map of exported symbol names to their metadata */
metadata: {[name: string]: MetadataEntry};
/** Information about re-exported symbols */
exports?: ModuleExportMetadata[];
/** Import alias information */
importAs?: string;
}Metadata for symbols that are re-exported from other modules.
/**
* Metadata for re-exported symbols
*/
interface ModuleExportMetadata {
/** Name of the exported symbol */
export?: string;
/** Source module being re-exported from */
from: string;
/** Original name in the source module */
as?: string;
}Union type representing different kinds of metadata entries.
/**
* Union type for all possible metadata entries
*/
type MetadataEntry =
| ClassMetadata
| InterfaceMetadata
| FunctionMetadata
| MetadataValue;
/**
* Union type for metadata values
*/
type MetadataValue =
| string
| number
| boolean
| undefined
| null
| MetadataObject
| MetadataArray
| MetadataSymbolicExpression;Metadata structure for TypeScript classes.
/**
* Metadata for TypeScript classes
*/
interface ClassMetadata {
/** Symbolic marker indicating this is class metadata */
__symbolic: 'class';
/** Decorator metadata applied to the class */
decorators?: MetadataSymbolicExpression[];
/** Metadata for class members (methods, properties) */
members?: MetadataMap;
/** Metadata for static members */
statics?: {[name: string]: MetadataValue};
}Metadata structure for TypeScript functions.
/**
* Metadata for TypeScript functions
*/
interface FunctionMetadata {
/** Symbolic marker indicating this is function metadata */
__symbolic: 'function';
/** Function parameters metadata */
parameters?: MetadataSymbolicExpression[];
/** Function return type or body metadata */
value: MetadataSymbolicExpression;
}Metadata structure for TypeScript interfaces.
/**
* Metadata for TypeScript interfaces
*/
interface InterfaceMetadata {
/** Symbolic marker indicating this is interface metadata */
__symbolic: 'interface';
}Error structure for metadata collection failures.
/**
* Error information for metadata collection failures
*/
interface MetadataError {
/** Symbolic marker indicating this is an error */
__symbolic: 'error';
/** Error message */
message: string;
/** Additional context about the error */
context?: {[name: string]: string};
}