Node.js wrapper and build tool plugins for Google's Closure Compiler that compiles, optimizes, and compresses JavaScript code
npx @tessl/cli install tessl/npm-google-closure-compiler@20250901.0.0Google Closure Compiler is a Node.js wrapper and build tool integration for Google's Closure Compiler, providing JavaScript optimization, compilation, and compression with support for multiple compilation levels and runtime platforms (native binaries and Java).
npm install google-closure-compilerimport ClosureCompiler, {
JAR_PATH,
CONTRIB_PATH,
EXTERNS_PATH,
grunt,
gulp,
javaPath
} from "google-closure-compiler";For CommonJS:
const ClosureCompiler = require("google-closure-compiler");
const { grunt, gulp, JAR_PATH } = require("google-closure-compiler");import ClosureCompiler from "google-closure-compiler";
// Basic compilation
const compiler = new ClosureCompiler({
js: 'src/app.js',
compilation_level: 'SIMPLE',
js_output_file: 'dist/app.min.js'
});
compiler.run((exitCode, stdout, stderr) => {
if (exitCode === 0) {
console.log('Compilation successful');
} else {
console.error('Compilation failed:', stderr);
}
});Google Closure Compiler npm package is built around several key components:
Compiler class that wraps the Java or native binaryMain compiler functionality for direct JavaScript compilation and optimization with support for various compilation levels and platform selection.
class Compiler {
constructor(args: Object | Array<string>, extraCommandArgs?: Array<string>);
run(callback?: (exitCode: number, stdout: string, stderr: string) => void): ChildProcess;
getFullCommand(): string;
formatArgument(key: string, val?: string | boolean): string;
}Executable command-line interface with argument parsing, platform selection, and direct compiler invocation.
// CLI executable available as: google-closure-compiler
// Supports all Closure Compiler flags plus:
// --platform: 'native' | 'java' | 'native,java'Grunt task integration for automated builds with support for multiple file sets and parallel compilation.
function gruntPlugin(grunt: any, pluginOptions?: {
platform?: string | Array<string>;
extraArguments?: Array<string>;
max_parallel_compilations?: number;
}): Function;Gulp plugin providing stream-based compilation with vinyl file support and source map integration.
function gulpPlugin(
compilationOptions: Object | Array<string>,
pluginOptions?: {
streamMode?: 'BOTH' | 'IN';
logger?: any;
pluginName?: string;
platform?: string | Array<string>;
requireStreamInput?: boolean;
}
): CompilationStream;Platform detection and native binary utilities for determining compiler availability. These are internal utilities used by the CLI and build plugins.
// Internal utilities (not exported from main module)
// Available from: import { getNativeImagePath, getFirstSupportedPlatform } from 'google-closure-compiler/lib/utils.js';
/**
* Get path to native binary compiler for current platform
* @returns Path to native compiler binary or undefined if not available
*/
function getNativeImagePath(): string | undefined;
/**
* Get first supported platform from preference list
* @param platforms - Array of platform preferences ('native' | 'java')
* @returns First available platform
* @throws Error if no platform is supported
*/
function getFirstSupportedPlatform(platforms: Array<'native' | 'java'>): 'native' | 'java';// Compiler JAR file path (when using Java platform)
const JAR_PATH: string;
// Path to contrib directory containing additional resources
const CONTRIB_PATH: string;
// Path to externs directory containing type definitions
const EXTERNS_PATH: string;
// Default Java executable path
const javaPath: string;interface CompilerOptions {
// Input/Output
js?: string | Array<string>;
js_output_file?: string;
externs?: string | Array<string>;
// 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';
// Modules and Resolution
module_resolution?: 'BROWSER' | 'NODE' | 'WEBPACK';
process_common_js_modules?: boolean;
entry_point?: string | Array<string>;
// Source Maps
create_source_map?: boolean | string;
source_map_format?: 'DEFAULT' | 'V3';
source_map_location_mapping?: Array<string>;
// Output Formatting
output_wrapper?: string;
output_manifest?: string;
formatting?: 'PRETTY_PRINT' | 'PRINT_INPUT_DELIMITER' | 'SINGLE_QUOTES';
// Warnings and Debugging
warning_level?: 'QUIET' | 'DEFAULT' | 'VERBOSE';
debug?: boolean;
checks_only?: boolean;
// Advanced Options
define?: Array<string>;
jscomp_warning?: string;
jscomp_error?: string;
[key: string]: any; // All Closure Compiler flags supported
}
interface PlatformOptions {
platform?: 'native' | 'java' | Array<'native' | 'java'>;
extraArguments?: Array<string>;
}
interface CompilerCallbackResult {
exitCode: number;
stdout: string;
stderr: string;
}
// Error types
interface PluginError extends Error {
plugin: string;
message: string;
cause?: Error;
}