or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdcore-compiler.mdgrunt-plugin.mdgulp-plugin.mdindex.md
tile.json

core-compiler.mddocs/

Core Compiler

The core Compiler class provides direct access to Google Closure Compiler functionality with support for both Java and native binary execution modes.

Capabilities

Compiler Class

Main compiler class that spawns and manages the Closure Compiler process.

/**
 * Creates a new Compiler instance with the specified options
 * @param args - Compilation arguments as object or array
 * @param extraCommandArgs - Additional command line arguments
 */
class Compiler {
  constructor(args: Object | Array<string>, extraCommandArgs?: Array<string>);
  
  // Instance properties
  commandArguments: Array<string>;
  extraCommandArgs?: Array<string>;
  JAR_PATH: string;
  javaPath: string;
  logger?: Function;
  spawnOptions?: Object;
}

Run Method

Executes the compilation process and returns a ChildProcess instance.

/**
 * Execute the compilation process
 * @param callback - Optional callback with compilation results
 * @returns ChildProcess instance for the spawned compiler
 */
run(callback?: (exitCode: number, stdout: string, stderr: string) => void): ChildProcess;

Usage Examples:

import ClosureCompiler from "google-closure-compiler";

// Basic compilation with callback
const compiler = new ClosureCompiler({
  js: ['src/file1.js', 'src/file2.js'],
  compilation_level: 'ADVANCED',
  js_output_file: 'build/compiled.js'
});

compiler.run((exitCode, stdout, stderr) => {
  if (exitCode === 0) {
    console.log('Compilation successful:', stdout);
  } else {
    console.error('Compilation failed:', stderr);
  }
});

// Advanced compilation with source maps
const advancedCompiler = new ClosureCompiler({
  js: 'src/**/*.js',
  compilation_level: 'ADVANCED',
  js_output_file: 'dist/app.min.js',
  create_source_map: 'dist/app.min.js.map',
  source_map_format: 'V3',
  warning_level: 'VERBOSE'
});

const process = advancedCompiler.run();
process.on('close', (code) => {
  console.log(`Compiler finished with exit code ${code}`);
});

// ES2015+ to ES5 compilation with module bundling
const moduleCompiler = new ClosureCompiler({
  js: ['src/main.js', 'src/utils.js', 'src/api.js'],
  compilation_level: 'SIMPLE',
  language_in: 'ECMASCRIPT_2018',
  language_out: 'ECMASCRIPT5',
  module_resolution: 'NODE',
  process_common_js_modules: true,
  entry_point: 'src/main.js',
  js_output_file: 'dist/bundle.js',
  output_wrapper: '(function(){%output%})();'
});

// Type checking without compilation
const typeChecker = new ClosureCompiler({
  js: ['src/**/*.js'],
  compilation_level: 'ADVANCED',
  checks_only: true,
  warning_level: 'VERBOSE',
  jscomp_error: 'checkTypes'
});

typeChecker.run((exitCode, stdout, stderr) => {
  if (exitCode === 0) {
    console.log('Type checking passed');
  } else {
    console.error('Type errors found:', stderr);
  }
});

Command Generation Methods

Utility methods for inspecting and formatting compiler commands.

/**
 * Get the complete command that will be executed
 * @returns Complete command line string
 */
getFullCommand(): string;

/**
 * Prepend the full command to a message for error reporting
 * @param msg - Message to prepend command to
 * @returns Message with command prepended
 */
prependFullCommand(msg: string): string;

/**
 * Format a key-value pair as a command line argument
 * @param key - Argument name (will be converted to snake_case with -- prefix)
 * @param val - Argument value (optional for boolean flags)
 * @returns Formatted command line argument
 */
formatArgument(key: string, val?: string | boolean): string;

Usage Examples:

const compiler = new ClosureCompiler({
  compilationLevel: 'SIMPLE', // camelCase converted to compilation_level
  jsOutputFile: 'output.js',   // camelCase converted to js_output_file
  debug: true                  // boolean flag
});

console.log(compiler.getFullCommand());
// Output: java -jar /path/to/compiler.jar --compilation_level=SIMPLE --js_output_file=output.js --debug

// Format individual arguments
console.log(compiler.formatArgument('warningLevel', 'VERBOSE'));
// Output: --warning_level=VERBOSE

console.log(compiler.formatArgument('debug'));
// Output: --debug

Platform and Path Configuration

Properties for controlling compiler execution environment.

// Platform-specific configuration
JAR_PATH: string;        // Path to Java compiler JAR (null for native)
javaPath: string;        // Java executable path ('java' by default)
logger?: Function;       // Optional logging function
spawnOptions?: Object;   // child_process.spawn options

Usage Examples:

import ClosureCompiler from "google-closure-compiler";

// Use specific Java path
const compiler = new ClosureCompiler({js: 'app.js'});
compiler.javaPath = '/usr/bin/java11';

// Use native binary (set JAR_PATH to null)
compiler.JAR_PATH = null;
compiler.javaPath = '/path/to/closure-compiler-binary';

// Add logging
compiler.logger = (message) => console.log('[COMPILER]', message);

// Configure spawn options
compiler.spawnOptions = {
  stdio: 'inherit',
  env: { ...process.env, JAVA_OPTS: '-Xmx2g' }
};

Compilation Options

All Google Closure Compiler flags are supported through the constructor arguments.

Common Options

interface CommonCompilerOptions {
  // Input/Output
  js?: string | Array<string>;           // Input JavaScript files
  js_output_file?: string;               // Output file path
  
  // Compilation
  compilation_level?: 'WHITESPACE_ONLY' | 'SIMPLE' | 'ADVANCED';
  language_in?: 'ECMASCRIPT3' | 'ECMASCRIPT5' | 'ECMASCRIPT_2015' | 'ECMASCRIPT_2017' | 'ECMASCRIPT_2018' | 'ECMASCRIPT_2019' | 'ECMASCRIPT_2020' | 'ECMASCRIPT_NEXT';
  language_out?: 'ECMASCRIPT3' | 'ECMASCRIPT5' | 'ECMASCRIPT_2015' | 'ECMASCRIPT_2017' | 'ECMASCRIPT_2018' | 'ECMASCRIPT_2019' | 'ECMASCRIPT_2020';
  
  // Source Maps
  create_source_map?: boolean | string;
  source_map_format?: 'DEFAULT' | 'V3';
  source_map_location_mapping?: Array<string>;
  
  // Modules
  module_resolution?: 'BROWSER' | 'NODE' | 'WEBPACK';
  process_common_js_modules?: boolean;
  
  // Output
  output_wrapper?: string;
  output_manifest?: string;
  
  // Debugging
  debug?: boolean;
  formatting?: 'PRETTY_PRINT' | 'PRINT_INPUT_DELIMITER' | 'SINGLE_QUOTES';
  warning_level?: 'QUIET' | 'DEFAULT' | 'VERBOSE';
  
  // Advanced
  externs?: string | Array<string>;
  define?: Array<string>;
  entry_point?: string | Array<string>;
}

Error Handling

The compiler process can fail for various reasons. Always check the exit code and handle errors appropriately.

const compiler = new ClosureCompiler(options);

compiler.run((exitCode, stdout, stderr) => {
  if (exitCode !== 0) {
    // Common error scenarios:
    // - Syntax errors in JavaScript
    // - Type checking failures (ADVANCED mode)
    // - Missing dependencies
    // - Invalid compiler flags
    console.error('Compilation failed:', stderr);
  }
});

Static Properties

// Default Java executable path
const javaPath: string = 'java';