Skip to content

Commit ec898c9

Browse files
committed
Allowing multiple js files to be specified for compilation
1 parent 4c5f010 commit ec898c9

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

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

0 commit comments

Comments
 (0)