Class: Js2coffee::CoffeeCompiler
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_target_path!
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 "[1m[32m==>[0m #{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
|
.wrapper ⇒ Object
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
"(function(script, options) {\n try {\n var result;\n result = CoffeeScript.compile(script, options);\n return {code: result.js, sourceMap: result.v3SourceMap};\n } catch (err) {\n if (err instanceof SyntaxError && err.location) {\n throw new SyntaxError([options.filename, err.location.first_line + 1, err.location.first_column + 1].join(\":\") + \": \" + err.message)\n } else {\n throw err;\n }\n }\n})\n"
end
|