Permalink
Browse files

Merge pull request #22 from jdeff/master

Allow for multiple js files to be compiled
  • Loading branch information...
2 parents e9ebf39 + 434e9e1 commit 324311f1980d5bb7ee86198edf8e6f10951e6e76 @jashkenas jashkenas committed Nov 26, 2012
View
@@ -4,7 +4,7 @@ The *closure-compiler* gem is a svelte wrapper around the "Google Closure Compil
Latest Version: *"1.1.7":http://rubygems.org/gems/closure-compiler*
-The Closure Compiler's *2012-07-25* JAR-file is included with the gem.
+The Closure Compiler's *2012-09-17* JAR-file is included with the gem.
h2. Installation
@@ -14,7 +14,7 @@ sudo gem install closure-compiler
h2. 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.
+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.
<pre>
require 'rubygems'
@@ -23,6 +23,16 @@ Closure::Compiler.new.compile(File.open('underscore.js', 'r'))
=> "(function(){var j=this,m=j._;function i(a){......
</pre>
+
+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.
+
+<pre>
+require 'rubygems'
+require 'closure-compiler'
+Closure::Compiler.new.compile_files(['underscore.js', 'jasmine.js']))
+
+=> "(function(){var j=this,m=j._;function i(a){......
+</pre>
When creating a @Closure::Compiler@, you can pass "any options that the command-line compiler accepts":http://code.google.com/closure/compiler/docs/gettingstarted_app.html to the initializer and they'll be forwarded. For example, to raise the compilation level up a notch:
@@ -2,7 +2,7 @@ module Closure
VERSION = "1.1.7"
- COMPILER_VERSION = "20120710"
+ COMPILER_VERSION = "20120917"
JAVA_COMMAND = 'java'
@@ -9,14 +9,14 @@ class Error < StandardError; end
# The Closure::Compiler is a basic wrapper around the actual JAR. There's not
# much to see here.
class Compiler
-
+
DEFAULT_OPTIONS = {:warning_level => 'QUIET'}
# When you create a Compiler, pass in the flags and options.
def initialize(options={})
@java = options.delete(:java) || JAVA_COMMAND
@jar = options.delete(:jar_file) || COMPILER_JAR
- @options = serialize_options(DEFAULT_OPTIONS.merge(options))
+ @options = DEFAULT_OPTIONS.merge(options)
end
# Can compile a JavaScript string or open IO object. Returns the compiled
@@ -34,21 +34,38 @@ def compile(io)
tempfile.flush
begin
- result = `#{command} --js #{tempfile.path} 2>&1`
- rescue Exception
- raise Error, "compression failed: #{result}"
+ result = compile_files(tempfile.path)
+ rescue Exception => e
+ raise e
ensure
tempfile.close!
end
+
+ yield(StringIO.new(result)) if block_given?
+ result
+ end
+ alias_method :compress, :compile
+
+ # Takes an array of javascript file paths or a single path. Returns the
+ # resulting JavaScript as a string or yields an IO object containing the
+ # response to a block, for streaming.
+ def compile_files(files)
+ @options.merge!({js: files})
+
+ begin
+ result = `#{command} 2>&1`
+ rescue Exception
+ raise Error, "compression failed: #{result}"
+ end
+
unless $?.exitstatus.zero?
raise Error, result
end
yield(StringIO.new(result)) if block_given?
result
end
- alias_method :compress, :compile
-
+ alias_method :compile_file, :compile_files
private
@@ -64,7 +81,7 @@ def serialize_options(options)
end
def command
- [@java, '-jar', "\"#{@jar}\"", @options].flatten.join(' ')
+ [@java, '-jar', "\"#{@jar}\"", serialize_options(@options)].flatten.join(' ')
end
end
Oops, something went wrong.

0 comments on commit 324311f

Please sign in to comment.