Skip to content

Commit 324311f

Browse files
committed
Merge pull request #22 from jdeff/master
Allow for multiple js files to be compiled
2 parents e9ebf39 + 434e9e1 commit 324311f

10 files changed

Lines changed: 460 additions & 95 deletions

README.textile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The *closure-compiler* gem is a svelte wrapper around the "Google Closure Compil
44

55
Latest Version: *"1.1.7":http://rubygems.org/gems/closure-compiler*
66

7-
The Closure Compiler's *2012-07-25* JAR-file is included with the gem.
7+
The Closure Compiler's *2012-09-17* JAR-file is included with the gem.
88

99
h2. Installation
1010

@@ -14,13 +14,23 @@ sudo gem install closure-compiler
1414

1515
h2. Usage
1616

17-
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.
17+
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.
1818

1919
<pre>
2020
require 'rubygems'
2121
require 'closure-compiler'
2222
Closure::Compiler.new.compile(File.open('underscore.js', 'r'))
2323

24+
=> "(function(){var j=this,m=j._;function i(a){......
25+
</pre>
26+
27+
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.
28+
29+
<pre>
30+
require 'rubygems'
31+
require 'closure-compiler'
32+
Closure::Compiler.new.compile_files(['underscore.js', 'jasmine.js']))
33+
2434
=> "(function(){var j=this,m=j._;function i(a){......
2535
</pre>
2636

lib/closure-compiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Closure
22

33
VERSION = "1.1.7"
44

5-
COMPILER_VERSION = "20120710"
5+
COMPILER_VERSION = "20120917"
66

77
JAVA_COMMAND = 'java'
88

lib/closure/compiler.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class Error < StandardError; end
99
# The Closure::Compiler is a basic wrapper around the actual JAR. There's not
1010
# much to see here.
1111
class Compiler
12-
12+
1313
DEFAULT_OPTIONS = {:warning_level => 'QUIET'}
1414

1515
# When you create a Compiler, pass in the flags and options.
1616
def initialize(options={})
1717
@java = options.delete(:java) || JAVA_COMMAND
1818
@jar = options.delete(:jar_file) || COMPILER_JAR
19-
@options = serialize_options(DEFAULT_OPTIONS.merge(options))
19+
@options = DEFAULT_OPTIONS.merge(options)
2020
end
2121

2222
# Can compile a JavaScript string or open IO object. Returns the compiled
@@ -34,21 +34,38 @@ def compile(io)
3434
tempfile.flush
3535

3636
begin
37-
result = `#{command} --js #{tempfile.path} 2>&1`
38-
rescue Exception
39-
raise Error, "compression failed: #{result}"
37+
result = compile_files(tempfile.path)
38+
rescue Exception => e
39+
raise e
4040
ensure
4141
tempfile.close!
4242
end
43+
44+
yield(StringIO.new(result)) if block_given?
45+
result
46+
end
47+
alias_method :compress, :compile
48+
49+
# Takes an array of javascript file paths or a single path. Returns the
50+
# resulting JavaScript as a string or yields an IO object containing the
51+
# response to a block, for streaming.
52+
def compile_files(files)
53+
@options.merge!({js: files})
54+
55+
begin
56+
result = `#{command} 2>&1`
57+
rescue Exception
58+
raise Error, "compression failed: #{result}"
59+
end
60+
4361
unless $?.exitstatus.zero?
4462
raise Error, result
4563
end
4664

4765
yield(StringIO.new(result)) if block_given?
4866
result
4967
end
50-
alias_method :compress, :compile
51-
68+
alias_method :compile_file, :compile_files
5269

5370
private
5471

@@ -64,7 +81,7 @@ def serialize_options(options)
6481
end
6582

6683
def command
67-
[@java, '-jar', "\"#{@jar}\"", @options].flatten.join(' ')
84+
[@java, '-jar', "\"#{@jar}\"", serialize_options(@options)].flatten.join(' ')
6885
end
6986

7087
end

test/fixtures/file1-file2-compiled.js

Lines changed: 251 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/file1.js

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/file2.js

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/precompressed-compiled.js

Lines changed: 79 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_helper.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
if defined?(Gem) and Gem.available?('redgreen')
1+
if begin
2+
Gem::Specification::find_by_name 'redgreen'
3+
rescue Gem::LoadError
4+
false
5+
rescue
6+
Gem.available? 'redgreen'
7+
end
28
require 'redgreen' if RUBY_VERSION < "1.9"
39
end
410
require 'test/unit'

test/unit/closure_compiler_test.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_block_syntax
3232
end
3333
assert result == COMPILED_ADVANCED
3434
end
35-
35+
3636
def test_jar_and_java_specifiation
3737
jar = Dir['vendor/closure-compiler-*.jar'].first
3838
unless java = ( `which java` rescue nil )
@@ -57,7 +57,6 @@ def test_exceptions
5757

5858
def test_stderr_reading
5959
js = Compiler.new.compile(File.read('test/fixtures/precompressed.js'))
60-
File.open('test/fixtures/precompressed-compiled.js', 'w+') {|f| f.write js }
6160
assert js == File.read('test/fixtures/precompressed-compiled.js')
6261
end
6362

@@ -68,15 +67,21 @@ def test_permissions
6867
def test_serialize_options
6968
options = { 'externs' => 'library1.js', "compilation_level" => "ADVANCED_OPTIMIZATIONS" }
7069
# ["--externs", "library1.js", "--compilation_level", "ADVANCED_OPTIMIZATIONS"]
71-
# although Hash in 1.8 might change the order to :
70+
# although Hash in 1.8 might change the order to :
7271
# ["--compilation_level", "ADVANCED_OPTIMIZATIONS", "--externs", "library1.js"]
7372
expected_options = options.to_a.map { |arr| [ "--#{arr[0]}", arr[1] ] }.flatten
7473
assert_equal expected_options, Closure::Compiler.new.send(:serialize_options, options)
7574
end
76-
75+
7776
def test_serialize_options_for_arrays
7877
compiler = Closure::Compiler.new('externs' => ['library1.js', "library2.js"])
7978
assert_equal ["--externs", "library1.js", "--externs", "library2.js"], compiler.send(:serialize_options, 'externs' => ['library1.js', "library2.js"])
8079
end
8180

81+
def test_compiling_array_of_file_paths
82+
files = ['test/fixtures/file1.js', 'test/fixtures/file2.js']
83+
result = Closure::Compiler.new().compile_files(files)
84+
85+
assert_equal result, File.read('test/fixtures/file1-file2-compiled.js')
86+
end
8287
end

0 commit comments

Comments
 (0)