Class: Closure::Compiler
- Inherits:
-
Object
- Object
- Closure::Compiler
- Defined in:
- lib/closure/compiler.rb
Overview
The Closure::Compiler is a basic wrapper around the actual JAR. There’s not much to see here.
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :warning_level => 'QUIET', :language_in => 'ECMASCRIPT5' }
Instance Method Summary collapse
-
#compile(io) {|StringIO.new(result)| ... } ⇒ Object
(also: #compress)
Can compile a JavaScript string or open IO object.
-
#compile_files(files) {|StringIO.new(result)| ... } ⇒ Object
(also: #compile_file)
Takes an array of javascript file paths or a single path.
-
#initialize(options = {}) ⇒ Compiler
constructor
When you create a Compiler, pass in the flags and options.
Constructor Details
#initialize(options = {}) ⇒ Compiler
When you create a Compiler, pass in the flags and options.
19 20 21 22 23 |
# File 'lib/closure/compiler.rb', line 19 def initialize(={}) @java = .delete(:java) || JAVA_COMMAND @jar = .delete(:jar_file) || COMPILER_JAR @options = DEFAULT_OPTIONS.merge() end |
Instance Method Details
#compile(io) {|StringIO.new(result)| ... } ⇒ Object Also known as: compress
Can compile a JavaScript string or open IO object. Returns the compiled JavaScript as a string or yields an IO object containing the response to a block, for streaming.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/closure/compiler.rb', line 28 def compile(io) tempfile = Tempfile.new('closure_compiler') if io.respond_to? :read while buffer = io.read(4096) do tempfile.write(buffer) end else tempfile.write(io.to_s) end tempfile.flush begin result = compile_files(tempfile.path) rescue Exception => e raise e ensure tempfile.close! end yield(StringIO.new(result)) if block_given? result end |
#compile_files(files) {|StringIO.new(result)| ... } ⇒ Object Also known as: compile_file
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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/closure/compiler.rb', line 55 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 |