Class: Closure::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/closure/compiler.rb

Defined Under Namespace

Classes: Compilation, Error, Util

Constant Summary collapse

OUTPUT_OPTIONS =

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

%w{
  --js_output_file
  --create_source_map
  --output_manifest
  --property_map_output_file
  --variable_map_output_file
  --module_output_path_prefix
}
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.

%w{
  --js
  --externs
  --property_map_input_file
  --variable_map_input_file
}

Class Method Summary collapse

Class Method Details

.compile(args, dependencies = [], env = {}) ⇒ Object

Compile Javascript. Checks file modification times but does not support namespaces like Goog#compile does.

Parameters:

  • args (Array)

    Arguments for the compiler.

  • dependencies (Array) (defaults to: [])

    Any other files to check mtime on, like makefiles.

  • env (Hash) (defaults to: {})

    Rack environment. Supply if you want a response that is cacheable.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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