Skip to content
A Ruby Wrapper for the Google Closure Compiler
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib bump closure-compiler versions. May 31, 2018
test Update test fixture files for new compiler version (#46) Nov 21, 2017
.gitignore Ignore DS_Store files Nov 12, 2015
LICENSE new version of the closure compiler brings us to 0.2.0 Feb 2, 2010
README.textile bump closure-compiler versions. May 31, 2018
Rakefile Don't use sudo in gem rake tasks Jan 6, 2016


The Closure Compiler (as a Ruby Gem)

The closure-compiler gem is a svelte wrapper around the Google Closure Compiler for JavaScript compression.

Latest Version: 1.1.14

The Closure Compiler’s 2018-05-06 JAR-file is included with the gem.


sudo gem install closure-compiler


The Closure::Compiler has a compile method, which can be passed a string or an open IO object, and returns the compiled JavaScript. The result is returned as a string, or, if a block is passed, yields as an IO object for streaming writes.

require 'rubygems'
require 'closure-compiler''underscore.js', 'r'))

=> "(function(){var j=this,m=j._;function i(a){......

The Closure::Compiler also has compile_file and compile_files methods, which can be passed a file path or an array of file paths respectively. The files are concatenated and compiled and, like the compile method, the result is returned as a string or, if block is passed, yields an IO object.

require 'rubygems'
require 'closure-compiler'['underscore.js', 'jasmine.js']))

=> "(function(){var j=this,m=j._;function i(a){......

When creating a Closure::Compiler, you can pass any options that the command-line compiler accepts to the initializer and they’ll be forwarded. For example, to raise the compilation level up a notch:

closure.compile('underscore.js', 'r'))

=> "(function(){var j=this,m=j.h;function i(a){......

The default values of all the compiler flags are identical to the command-line version. The default compilation_level is “SIMPLE_OPTIMIZATIONS”.

A Closure::Error exception will be raised, explaining the JavaScript syntax error, if compilation fails for any reason.

YUI Compressor Compatibility

Effort has been made to make the “closure-compiler” gem a drop-in alternative to the “ruby-yui-compressor”. To that end, Closure::Compiler#compile has been aliased as compress, and can take the same string or IO argument that a YUI::JavaScriptCompressor#compress can. In addition, the Closure::Compiler initializer can take java and jar_file options, overriding the location of the Java command and the Closure Compiler JAR file, respectively.

compiler =
  :java     => '/usr/local/bin/java16',
  :jar_file => '/usr/src/closure/build/latest.jar'
You can’t perform that action at this time.