Transpile TypeScript code to JavaScript with Closure annotations.
—
The core transformation functionality orchestrates the entire TypeScript to Closure Compiler conversion process, providing the main entry point and configuration system for all tsickle transformations.
The primary function that transforms a TypeScript program into Closure-compatible JavaScript with proper JSDoc annotations.
/**
* Main tsickle emit function that transforms TypeScript to Closure-compatible JavaScript
* @param program - TypeScript program to transform
* @param host - Configuration host with transformation options
* @param writeFile - Callback for writing transformed files
* @param targetSourceFile - Optional specific file to transform
* @param cancellationToken - Optional cancellation token
* @param emitOnlyDtsFiles - Whether to emit only declaration files
* @param customTransformers - Optional custom transformer configuration
* @returns EmitResult with diagnostics and generated content
*/
function emit(
program: ts.Program,
host: TsickleHost,
writeFile: ts.WriteFileCallback,
targetSourceFile?: ts.SourceFile,
cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers?: EmitTransformers
): EmitResult;Usage Example:
import * as ts from "typescript";
import { emit, TsickleHost } from "tsickle";
const program = ts.createProgram(["src/app.ts"], {
rootDir: "/project/src",
outDir: "/project/dist",
target: ts.ScriptTarget.ES2015,
module: ts.ModuleKind.CommonJS,
});
const host: TsickleHost = {
shouldSkipTsickleProcessing: (fileName) => fileName.includes("node_modules"),
shouldIgnoreWarningsForPath: (filePath) => filePath.includes("third_party"),
pathToModuleName: (context, importPath) => importPath.replace(/\.tsx?$/, ""),
fileNameToModuleId: (fileName) => fileName,
googmodule: true,
transformTypesToClosure: true,
generateExtraSuppressions: true,
};
const result = emit(program, host, (fileName, content) => {
fs.writeFileSync(fileName, content);
});Legacy emit function maintained for backward compatibility with Angular.
/**
* @deprecated Exposed for backward compat with Angular. Use emit() instead.
*/
function emitWithTsickle(
program: ts.Program,
host: TsickleHost,
tsHost: ts.CompilerHost,
tsOptions: ts.CompilerOptions,
targetSourceFile?: ts.SourceFile,
writeFile?: ts.WriteFileCallback,
cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers?: EmitTransformers
): EmitResult;Utility function for combining multiple emit results into a single consolidated result.
/**
* Merges multiple EmitResult objects into a single consolidated result
* @param emitResults - Array of EmitResult objects to merge
* @returns Single merged EmitResult
*/
function mergeEmitResults(emitResults: EmitResult[]): EmitResult;Usage Example:
// Process multiple programs
const results = programs.map(program => emit(program, host, writeFile));
const mergedResult = mergeEmitResults(results);
console.log(`Total files emitted: ${mergedResult.emittedFiles?.length || 0}`);
console.log(`Total diagnostics: ${mergedResult.diagnostics.length}`);Main configuration interface that controls all aspects of the transformation process.
interface TsickleHost extends GoogModuleProcessorHost, TsMigrationExportsShimProcessorHost, AnnotatorHost {
/** Whether to downlevel decorators */
transformDecorators?: boolean;
/** Whether to convert types to closure */
transformTypesToClosure?: boolean;
/** Are tsMigrationExports calls allowed and should shim files be emitted? */
generateTsMigrationExportsShim?: boolean;
/** Whether to add aliases to the .d.ts files to add the exports to the ಠ_ಠ.clutz namespace */
addDtsClutzAliases?: boolean;
/** If true, tsickle and decorator downlevel processing will be skipped for that file */
shouldSkipTsickleProcessing(fileName: string): boolean;
/** Tsickle treats warnings as errors, if true, ignore warnings. This might be useful for e.g. third party code */
shouldIgnoreWarningsForPath(filePath: string): boolean;
/** Whether to convert CommonJS require() imports to goog.module() and goog.require() calls */
googmodule: boolean;
/** Whether to transform declaration merging namespaces */
useDeclarationMergingTransformation?: boolean;
/** Whether to add suppressions by default */
generateExtraSuppressions: boolean;
}Configuration for additional custom transformers to run alongside tsickle transformations.
interface EmitTransformers {
/** Custom transformers to evaluate before built-in .js transformations */
beforeTs?: ts.CustomTransformers['before'];
/** Custom transformers to evaluate after built-in .js transformations */
afterTs?: ts.CustomTransformers['after'];
/** Custom transformers to evaluate after built-in .d.ts transformations */
afterDeclarations?: ts.CustomTransformers['afterDeclarations'];
}Extended emit result containing tsickle-specific outputs and metadata.
interface EmitResult extends ts.EmitResult {
/** The manifest of JS modules output by the compiler */
modulesManifest: ModulesManifest;
/** externs.js files produced by tsickle, if any. module IDs are relative paths from fileNameToModuleId */
externs: { [moduleId: string]: string };
/** Content for the generated files, keyed by their intended filename. Filenames are google3 relative */
tsMigrationExportsShimFiles: Map<string, string>;
}The EmitResult includes all standard TypeScript emit results plus:
Converts import paths to googmodule module names (re-exported from cli_support).
/**
* Takes a context (ts.SourceFile.fileName of the current file) and the import URL of an ES6
* import and generates a googmodule module name for the imported module
*/
function pathToModuleName(rootModulePath: string, context: string, fileName: string): string;Install with Tessl CLI
npx tessl i tessl/npm-tsickle