closure-compiler

by documentcloud

documentcloud / closure-compiler

A Ruby Wrapper for the Google Closure Compiler

202 Stars 49 Forks Last release: Not found Other 110 Commits 29 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

h1. The Closure Compiler (as a Ruby Gem)

The closure-compiler gem is a svelte wrapper around the "Google Closure Compiler":https://developers.google.com/closure/compiler/ for JavaScript compression.

Latest Version: "1.1.14":https://rubygems.org/gems/closure-compiler

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

h2. Installation

sudo gem install closure-compiler

h2. Usage

The @Closure::[email protected] has a @[email protected] method, which can be passed a string or an open @[email protected] object, and returns the compiled JavaScript. The result is returned as a string, or, if a block is passed, yields as an @[email protected] 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){......

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

require 'rubygems'
require 'closure-compiler'
Closure::Compiler.new.compile_files(['underscore.js', 'jasmine.js']))

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

When creating a @Closure::[email protected], you can pass "any options that the command-line compiler accepts":https://developers.google.com/closure/compiler/docs/gettingstarted_app 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::[email protected] exception will be raised, explaining the JavaScript syntax error, if compilation fails for any reason.

h2. 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#[email protected] has been aliased as @[email protected], and can take the same string or IO argument that a @YUI::JavaScriptCompressor#[email protected] can. In addition, the @Closure::[email protected] initializer can take @[email protected] and @[email protected] 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'
)

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.