Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A Ruby Wrapper for the Google Closure Compiler

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 COPYING
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.textile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 closure-compiler.gemspec
README.textile

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.4

The Closure Compiler’s 2011-10-03 JAR-file is included with the gem.

Installation

sudo gem install closure-compiler

Usage

The Closure::Compiler has a single method, compile, 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'
Closure::Compiler.new.compile(File.open('underscore.js', 'r'))

=> "(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 = Closure::Compiler.new(:compilation_level => 'ADVANCED_OPTIMIZATIONS')
closure.compile(File.open('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 = Closure::Compiler.new(
  :java     => '/usr/local/bin/java16',
  :jar_file => '/usr/src/closure/build/latest.jar'
)
Something went wrong with that request. Please try again.