Specialized compiler hosts that extend TypeScript's standard compiler host with Angular-specific functionality for metadata writing, synthetic file handling, and diagnostic formatting.
Compiler host that automatically writes .metadata.json files alongside .d.ts files during compilation.
/**
* Compiler host that writes metadata files alongside declaration files
*/
class MetadataWriterHost extends DelegatingHost {
/**
* Creates a metadata writer host
* @param delegate - Base TypeScript compiler host to delegate to
* @param ngOptions - Angular compiler options
* @param emitAllFiles - Whether to emit all files or only changed files
*/
constructor(delegate: ts.CompilerHost, ngOptions: NgOptions, emitAllFiles?: boolean);
}Usage Example:
import { MetadataWriterHost } from "@angular/tsc-wrapped";
import * as ts from "typescript";
const baseHost = ts.createCompilerHost({});
const ngOptions = {
basePath: "./src",
skipMetadataEmit: false,
strictMetadataEmit: true
};
const metadataHost = new MetadataWriterHost(baseHost, ngOptions, true);
// Use metadataHost for compilation - it will automatically emit .metadata.json files
const program = ts.createProgram(["./src/app.ts"], {}, metadataHost);
program.emit();Abstract base class for compiler hosts that delegate to another host while adding functionality.
/**
* Abstract base class for compiler hosts that delegate operations to another host
*/
abstract class DelegatingHost extends ts.CompilerHost {
/**
* Creates a delegating host
* @param delegate - Base compiler host to delegate operations to
*/
constructor(delegate: ts.CompilerHost);
/** The underlying compiler host being delegated to */
protected delegate: ts.CompilerHost;
}Usage Example:
import { DelegatingHost } from "@angular/tsc-wrapped";
import * as ts from "typescript";
class CustomHost extends DelegatingHost {
constructor(delegate: ts.CompilerHost) {
super(delegate);
}
// Override specific methods while delegating others
writeFile(fileName: string, data: string, writeByteOrderMark: boolean): void {
console.log(`Writing file: ${fileName}`);
super.writeFile(fileName, data, writeByteOrderMark);
}
}Compiler host that provides synthetic index files for flat module bundling.
/**
* Compiler host that provides synthetic index files for flat module bundling
*/
class SyntheticIndexHost extends DelegatingHost {
/**
* Creates a synthetic index host
* @param delegate - Base compiler host to delegate to
* @param syntheticIndex - Configuration for the synthetic index file
*/
constructor(
delegate: ts.CompilerHost,
syntheticIndex: {
name: string;
content: string;
metadata: string;
}
);
}Usage Example:
import { SyntheticIndexHost } from "@angular/tsc-wrapped";
import * as ts from "typescript";
const baseHost = ts.createCompilerHost({});
const syntheticIndex = {
name: "index.ts",
content: "export * from './public_api';",
metadata: '{"__symbolic":"module","version":3,"metadata":{}}'
};
const syntheticHost = new SyntheticIndexHost(baseHost, syntheticIndex);
// Use syntheticHost for compilation with the synthetic index file
const program = ts.createProgram(["./src/public_api.ts"], {}, syntheticHost);Utility function for formatting TypeScript diagnostics into readable strings.
/**
* Formats TypeScript diagnostics into readable error messages
* @param diagnostics - Array of TypeScript diagnostics to format
* @returns Formatted diagnostic messages as a string
*/
function formatDiagnostics(diagnostics: ts.Diagnostic[]): string;Usage Example:
import { formatDiagnostics } from "@angular/tsc-wrapped";
import * as ts from "typescript";
// During compilation, collect diagnostics
const program = ts.createProgram(["./src/app.ts"], {});
const diagnostics = [
...program.getSyntacticDiagnostics(),
...program.getSemanticDiagnostics()
];
if (diagnostics.length > 0) {
const formattedErrors = formatDiagnostics(diagnostics);
console.error("Compilation errors:", formattedErrors);
}Utility function for checking if an object is a VinylFile.
/**
* Type guard to check if an object is a VinylFile
* @param file - Object to check
* @returns True if the object is a VinylFile
*/
function isVinylFile(file: any): file is VinylFile;Usage Example:
import { isVinylFile } from "@angular/tsc-wrapped";
function processFile(input: string | VinylFile) {
if (isVinylFile(input)) {
console.log("Processing vinyl file:", input.path);
console.log("Contents:", input.contents.toString());
} else {
console.log("Processing file path:", input);
}
}Angular compiler options used by the compiler hosts.
/**
* Angular compiler options (alias for AngularCompilerOptions)
*/
type NgOptions = AngularCompilerOptions;Configuration object for synthetic index files.
/**
* Configuration for synthetic index files
*/
interface SyntheticIndexConfig {
/** Name of the synthetic index file */
name: string;
/** Content of the synthetic index file */
content: string;
/** Metadata content for the synthetic index file */
metadata: string;
}