Transpile TypeScript code to JavaScript with Closure annotations.
—
Converts CommonJS require() imports to goog.module() and goog.require() calls, with comprehensive support for ES6 module syntax transformation to Closure Compiler's module system.
Main transformer that converts CommonJS and ES6 modules to Closure's goog.module format.
/**
* Main transformer factory for CommonJS to goog.module conversion
*/
function commonJsToGoogmoduleTransformer(
host: GoogModuleProcessorHost,
modulesManifest: ModulesManifest,
typeChecker: ts.TypeChecker
): ts.TransformerFactory<ts.SourceFile>;Usage Example:
import { commonJsToGoogmoduleTransformer, ModulesManifest } from "tsickle";
const manifest = new ModulesManifest();
const host: GoogModuleProcessorHost = {
pathToModuleName: (context, importPath) => importPath.replace(/\.tsx?$/, ''),
fileNameToModuleId: (fileName) => fileName,
isJsTranspilation: false
};
const transformer = commonJsToGoogmoduleTransformer(host, manifest, typeChecker);Functions for resolving and managing module names in the goog.module system.
/**
* Resolves module names for goog.module
*/
function resolveModuleName(
typechecker: ts.TypeChecker,
node: ts.Node,
pathToModuleName: (context: string, importPath: string) => string,
host: ts.ModuleResolutionHost
): string | undefined;
/**
* Gets namespace for import URL
*/
function namespaceForImportUrl(
host: GoogModuleProcessorHost,
context: string,
importUrl: string
): string;
/**
* Gets ambient module symbol
*/
function getAmbientModuleSymbol(
typeChecker: ts.TypeChecker,
moduleName: string
): ts.Symbol | undefined;Utility for extracting module marker symbols from TypeScript library options.
/**
* Extracts module marker symbols
*/
function extractModuleMarker(tsOptionsLib: readonly string[]): ts.Symbol[];Functions for handling existing goog.module comments and markers.
/**
* Gets original goog.module name from comment
*/
function getOriginalGoogModuleFromComment(sourceFile: ts.SourceFile): string | undefined;Configuration interface for goog.module processing behavior.
interface GoogModuleProcessorHost {
/** Converts import paths to module names */
pathToModuleName(context: string, importPath: string): string;
/** Converts file names to module IDs */
fileNameToModuleId(fileName: string): string;
/** Whether this is a JS transpilation (optional) */
isJsTranspilation?: boolean;
}Configuration Example:
const host: GoogModuleProcessorHost = {
pathToModuleName: (context, importPath) => {
// Convert relative imports to module names
if (importPath.startsWith('./')) {
return path.resolve(path.dirname(context), importPath).replace(/\.tsx?$/, '');
}
return importPath;
},
fileNameToModuleId: (fileName) => {
// Convert file paths to module IDs
return fileName.replace(/^\/project\/src\//, '').replace(/\.tsx?$/, '');
},
isJsTranspilation: false
};Manages the module dependency graph of output JavaScript files.
/**
* A class that maintains the module dependency graph of output JS files
*/
class ModulesManifest {
/** Merges another manifest */
addManifest(other: ModulesManifest): void;
/** Registers a module */
addModule(fileName: string, module: string): void;
/** Adds module reference */
addReferencedModule(fileName: string, resolvedModule: string): void;
/** Gets filename from module name */
getFileNameFromModule(module: string): string;
/** Gets referenced modules */
getReferencedModules(fileName: string): string[];
/** All registered modules */
get modules(): string[];
/** All registered file names */
get fileNames(): string[];
}Usage Example:
const manifest = new ModulesManifest();
// Register modules
manifest.addModule('src/utils.js', 'app.utils');
manifest.addModule('src/main.js', 'app.main');
// Track dependencies
manifest.addReferencedModule('src/main.js', 'app.utils');
// Query dependencies
const dependencies = manifest.getReferencedModules('src/main.js');
console.log(dependencies); // ['app.utils']Generic interface for file-based mappings.
interface FileMap<T> {
[fileName: string]: T;
}// Before transformation (TypeScript/ES6)
import { Component } from '@angular/core';
import * as utils from './utils';
import defaultExport from './default-export';
// After transformation (goog.module)
goog.module('app.component');
const Component = goog.require('angular.core.Component');
const utils = goog.require('app.utils');
const defaultExport = goog.require('app.default_export');// Before transformation
export class MyClass {}
export const MY_CONSTANT = 'value';
export default function defaultFunction() {}
// After transformation
goog.module('app.my_module');
class MyClass {}
const MY_CONSTANT = 'value';
function defaultFunction() {}
exports.MyClass = MyClass;
exports.MY_CONSTANT = MY_CONSTANT;
exports = defaultFunction;// Before transformation
const module = await import('./dynamic-module');
// After transformation
const module = await goog.module.get('app.dynamic_module');// Before transformation
import * as fs from 'fs';
fs.readFileSync('file.txt');
// After transformation
const fs = goog.require('node.fs');
fs.readFileSync('file.txt');The module system transformation includes comprehensive error handling and diagnostics for common issues:
The module system transformation integrates with:
Install with Tessl CLI
npx tessl i tessl/npm-tsickle