closure-compiler-stream

raw JSON →
0.1.15 verified Fri May 01 auth: no javascript

A streaming interface for Google Closure Compiler that provides full pipe support for Node.js streams. Version 0.1.15 is the latest stable release. It wraps the Closure Compiler Java application as a Node.js stream, allowing it to be used in gulp pipelines or with plain streams. Key differentiators from other Closure Compiler wrappers are its simple stream-based API and support for Gulp with sourcemaps. It supports module definitions and custom jar paths. The package is minimal and focuses on streaming rather than complex configuration.

error Error: spawn java ENOENT
cause Java is not installed or not in PATH.
fix
Install Java (JRE) and ensure 'java' command is available.
error Error: write after end
cause Attempting to write to the stream after it has ended, often due to incorrect piping.
fix
Ensure piping is set up correctly: source.pipe(closure()).pipe(dest);
error TypeError: closure is not a function
cause Importing the module incorrectly, e.g., as an ESM default import without bundler or using .default.
fix
Use require('closure-compiler-stream') and call it directly as closure().
gotcha The module requires Java to be installed because it spawns the Closure Compiler Java jar.
fix Ensure Java (JRE) is installed and available in PATH.
gotcha Options passed to the stream are flags for the Closure Compiler. Not all options are validated; misspelled flags may be ignored silently.
fix Refer to the Closure Compiler flags documentation and use correct flag names.
gotcha The stream returns a Writable stream but is intended to be used in a pipeline; piping to it will cause it to consume input and produce output on the next stream.
fix Always pipe from a readable stream into closure() and then to a writable stream.
deprecated The package is not actively maintained; last release was 0.1.15 in 2016. Consider using more modern alternatives like google-closure-compiler.
fix Migrate to google-closure-compiler package which provides a similar API and is actively maintained.
npm install closure-compiler-stream
yarn add closure-compiler-stream
pnpm add closure-compiler-stream

Creates a read stream from a source file, pipes through the Closure Compiler, and writes the minified output to another file.

const closure = require('closure-compiler-stream');
const fs = require('fs');

// Compile JavaScript from a file and output to another file
fs.createReadStream('input.js')
  .pipe(closure())
  .pipe(fs.createWriteStream('output.min.js'));

// Or with options
fs.createReadStream('input.js')
  .pipe(closure({ compilation_level: 'SIMPLE_OPTIMIZATIONS' }))
  .pipe(fs.createWriteStream('output.min.js'));