CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-google-closure-compiler

Node.js wrapper and build tool plugins for Google's Closure Compiler that compiles, optimizes, and compresses JavaScript code

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

The google-closure-compiler package provides a command-line interface that wraps the Closure Compiler with additional Node.js-specific features like platform selection and argument processing.

Capabilities

CLI Executable

The package installs a google-closure-compiler executable that can be used directly from the command line.

# Basic usage
google-closure-compiler --js=input.js --js_output_file=output.js

# With compilation level
google-closure-compiler --js=src/**/*.js --compilation_level=ADVANCED --js_output_file=dist/app.min.js

# Platform selection (CLI-specific flag)
google-closure-compiler --platform=native --js=app.js --js_output_file=app.min.js
google-closure-compiler --platform=java --js=app.js --js_output_file=app.min.js
google-closure-compiler --platform=native,java --js=app.js --js_output_file=app.min.js

Platform Selection

The CLI supports a special --platform flag for choosing the compiler implementation.

# Platform options:
--platform=native         # Use native binary (fastest, platform-specific)
--platform=java           # Use Java JAR (requires Java runtime)
--platform=native,java    # Try native first, fallback to Java
--platform=java,native    # Try Java first, fallback to native

Usage Examples:

# Force native binary usage
google-closure-compiler --platform=native --js=app.js --js_output_file=app.min.js

# Force Java usage (useful for CI/CD environments)
google-closure-compiler --platform=java --js=app.js --js_output_file=app.min.js

# Use with multiple input files
google-closure-compiler \
  --platform=native \
  --js=src/utils.js \
  --js=src/main.js \
  --compilation_level=SIMPLE \
  --js_output_file=dist/bundle.js

# Module compilation with CommonJS support
google-closure-compiler \
  --js=src/**/*.js \
  --module_resolution=NODE \
  --process_common_js_modules \
  --entry_point=src/main.js \
  --compilation_level=ADVANCED \
  --js_output_file=dist/bundle.js

# Type checking only (no output generation)
google-closure-compiler \
  --js=src/**/*.js \
  --compilation_level=ADVANCED \
  --checks_only \
  --warning_level=VERBOSE

Argument Processing

The CLI automatically processes and normalizes command-line arguments.

# Boolean arguments - all equivalent:
google-closure-compiler --debug=true --js=app.js
google-closure-compiler --debug true --js=app.js  
google-closure-compiler --debug --js=app.js

# Array arguments - multiple values:
google-closure-compiler --js=file1.js --js=file2.js --js=file3.js
google-closure-compiler file1.js file2.js file3.js  # Default arguments become --js

# String escaping for complex paths:
google-closure-compiler --js="src/**/*.js" --js_output_file="dist/app.min.js"

Standard Closure Compiler Flags

All standard Google Closure Compiler flags are supported. Common examples:

# Compilation levels
--compilation_level=WHITESPACE_ONLY    # Remove whitespace only
--compilation_level=SIMPLE             # Basic optimizations
--compilation_level=ADVANCED           # Advanced optimizations with type checking

# Language options
--language_in=ECMASCRIPT_2018
--language_out=ECMASCRIPT5

# Source maps
--create_source_map=output.js.map
--source_map_format=V3

# Module resolution
--module_resolution=NODE
--process_common_js_modules

# Output formatting
--formatting=PRETTY_PRINT
--output_wrapper="(function(){%output%})()"

# Debugging and warnings
--debug
--warning_level=VERBOSE
--jscomp_warning=checkTypes

Usage Examples:

# Advanced compilation with source maps
google-closure-compiler \
  --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

# Simple optimization for legacy browsers
google-closure-compiler \
  --js=modern-app.js \
  --compilation_level=SIMPLE \
  --language_out=ECMASCRIPT5 \
  --js_output_file=legacy-app.js

# Node.js module compilation
google-closure-compiler \
  --js=src/**/*.js \
  --compilation_level=ADVANCED \
  --module_resolution=NODE \
  --process_common_js_modules \
  --js_output_file=dist/bundle.js

Exit Codes and Error Handling

The CLI returns standard exit codes for script integration.

# Exit codes:
# 0 - Success
# 1 - Compilation error
# 1 - Process spawn error (Java not found, etc.)

Usage in Scripts:

#!/bin/bash

# Check compilation success
if google-closure-compiler --js=app.js --js_output_file=app.min.js; then
  echo "Compilation successful"
else
  echo "Compilation failed with exit code $?"
  exit 1
fi

# With error capture
ERROR_OUTPUT=$(google-closure-compiler --js=app.js --js_output_file=app.min.js 2>&1)
if [ $? -ne 0 ]; then
  echo "Compilation failed: $ERROR_OUTPUT"
fi

NPX Usage

The package can be used with npx without installation:

# One-time usage with npx
npx google-closure-compiler --js=app.js --js_output_file=app.min.js

# Platform selection with npx
npx google-closure-compiler --platform=native --js=app.js --js_output_file=app.min.js

Implementation Details

The CLI performs several preprocessing steps:

  1. Platform Detection: Determines available compiler implementations
  2. Argument Parsing: Uses minimist to parse command-line arguments
  3. Boolean Conversion: Converts string 'true'/'false' to actual booleans
  4. Default Arguments: Treats non-flagged arguments as --js inputs
  5. Platform Flag Removal: Strips --platform before passing to compiler
// Internal CLI processing (for reference):
interface CLIProcessing {
  // Parse arguments with minimist
  parseArgs(argv: string[]): Object;
  
  // Convert platform preference to supported platform
  getFirstSupportedPlatform(platforms: string[]): 'native' | 'java';
  
  // Move default arguments to --js flag
  processDefaultArguments(args: Object): Object;
  
  // Convert string booleans to actual booleans
  normalizeBooleans(args: Object): Object;
}

docs

cli.md

core-compiler.md

grunt-plugin.md

gulp-plugin.md

index.md

tile.json