class Closure::Compiler

Constants

INPUT_OPTIONS

These are filename options and will be expanded to a new base. These will have their modification times checked against js_output_file.

OUTPUT_OPTIONS

These are filename options and will be expanded to a new base.

Public Class Methods

compile(args, dependencies = [], env = {}) click to toggle source

Compile Javascript. Checks file modification times but does not support namespaces like {Goog#compile} does. @param (Array) args Arguments for the compiler. @param (Array) dependencies Any other files to check mtime on, like makefiles. @param (Hash) env Rack environment. Supply if you want a response that is cacheable.

# File lib/closure/compiler.rb, line 50
def self.compile(args, dependencies = [], env = {})
  if Util.arg_values(args, '--js').empty?
    return Compilation.new env, nil, 'No source javascript specified'
  end
  # We don't bother compiling if we can detect that no sources were modified
  js_output_file = Util.arg_values(args, '--js_output_file').last
  if js_output_file
    js_mtime = File.mtime js_output_file rescue Errno::ENOENT
    compiled = !!File.size?(js_output_file) # catches empty files too
    files = Util.arg_values(args, INPUT_OPTIONS) + dependencies
    files.each do |filename|
      break unless compiled
      mtime = File.mtime filename
      compiled = false if !mtime or mtime > js_mtime
    end
    if compiled
      return Compilation.new env, js_output_file
    end
    File.unlink js_output_file rescue Errno::ENOENT
  end
  stdout, log = Closure.run_java Closure.config.compiler_jar, 'com.google.javascript.jscomp.CommandLineRunner', args
  if log.empty?
    log = nil
  else
    # Totals at the end make sense for the command line.  But at
    # the start makes more sense for html and the Javascript console
    split_log = log.split("\n")
    if split_log.last =~ /^\d+ err/i
      error_message = split_log.pop
    else
      error_message = split_log.shift
    end
    if split_log.empty?
      log = error_message
    else
      log = error_message + "\n\n" + split_log.join("\n")
    end
    raise Error, log unless error_message =~ /^0 err/i
  end
  Compilation.new(env, js_output_file, log) << stdout
end