Class: Js2coffee::JsCompiler

Inherits:
Compiler show all
Defined in:
lib/js2coffee/js_compiler.rb

Class Method Summary collapse

Methods inherited from Compiler

compile, compile_file, source, source=

Class Method Details

.compilerObject



27
28
29
# File 'lib/js2coffee/js_compiler.rb', line 27

def compiler
  @coffee ||= ExecJS.compile("var window = {}; #{source}")
end

.watch_file(file) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/js2coffee/js_compiler.rb', line 31

def watch_file(file)
  file_path = Pathname(file)
  source_code = file_path.read
  target_file, target_map = Js2coffee::PathHelper.new(file_path).create_target_path!
  # generate coffee and SourceMap.
  result = compiler.call(wrapper, source_code)
  target_file.write(result['code'])
  target_map.write(result['sourceMap'])

  puts "==> #{target_file}"
  target_file
rescue ExecJS::RuntimeError
  error_msg = "[#{file_path}]: #{$!.message}"
  `notify-send "#{error_msg}" -t 5000` if system 'which notify-send &>/dev/null'
  puts error_msg
end

.wrapperObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/js2coffee/js_compiler.rb', line 9

def wrapper
  <<-WRAPPER
(function(script, options) {
    try {
  var result;
  result = window.js2coffee.build(script, options);
  return {code: result.code, sourceMap: result.map, warnings: result.warnings};
    } catch (err) {
  if (err instanceof SyntaxError && err.location) {
      throw new SyntaxError([options.filename, err.location.first_line + 1, err.location.first_column + 1].join(":") + ": " + err.message)
  } else {
      throw err;
  }
    }
})
WRAPPER
end