Class: Js2coffee::CoffeeCompiler

Inherits:
Compiler
  • Object
show all
Defined in:
lib/js2coffee/coffee_compiler.rb

Class Method Summary collapse

Methods inherited from Compiler

compile, compile_file, compiler, source, source=

Class Method Details

.watch_file(file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/js2coffee/coffee_compiler.rb', line 32

def watch_file(file)
  file_path = Pathname(file)
  source_code = file_path.read
  target_file, target_map = Js2coffee::PathHelper.new(file_path).create_js_path!
  # generate js and SourceMap.
  result = compiler.call(wrapper, source_code, bare: false, sourceMap: true,
                         sourceFiles: [file_path.relative_path_from(target_file.dirname).to_s],
                         generatedFile: target_file.relative_path_from(target_file.dirname).to_s
                        )
  target_file.write("#{ENV['JS_SHEBANG']}#{result['code']}
//# sourceMappingURL=#{target_map.relative_path_from(target_file.dirname)}")
  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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/js2coffee/coffee_compiler.rb', line 14

def wrapper
  <<-WRAPPER
(function(script, options) {
    try {
  var result;
  result = CoffeeScript.compile(script, options);
  return {code: result.js, sourceMap: result.v3SourceMap};
    } 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