12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/coffee-rails-source-maps.rb', line 12
def compile(script, options = {})
script = script.read if script.respond_to?(:read)
if options.key?(:no_wrap) && !options.key?(:bare)
options[:bare] = options[:no_wrap]
else
options[:bare] = false
end
pathname = options[:pathname]
if pathname.nil?
return Source.context.call("CoffeeScript.compile", script, options)
else
clean_name = pathname.basename.to_s.split(".").first
rel_path = if pathname.to_s.start_with?(Bundler.bundle_path.to_s)
Pathname('bundler').join(pathname.relative_path_from(Bundler.bundle_path)).dirname
else
pathname.relative_path_from(Rails.root).dirname
end
map_dir = Rails.root.join("public/" + Rails.configuration.assets.prefix, "source_maps", rel_path)
map_dir.mkpath
map_file = map_dir.join("#{clean_name}.map")
coffee_file = map_dir.join("#{clean_name}.coffee")
options[:sourceMap] = true
options[:filename] = "#{clean_name}.coffee"
options[:sourceFiles] = ["/#{coffee_file.relative_path_from(Rails.root.join("public"))}"]
wrapper = " (function(script, options) {\n try {\n return CoffeeScript.compile(script, options);\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 WRAPPER\n\n file_path = \"var sourcePath='\#{pathname.basename}';\\n\"\n ret = Source.context.call(wrapper, file_path + script, options)\n\n puts file_path\n coffee_file.open('w') {|f| f.puts script }\n map_file.open('w') {|f| f.puts ret[\"v3SourceMap\"]}\n\n comment = \"//# sourceMappingURL=/\#{map_file.relative_path_from(Rails.root.join(\"public\"))}\\n\"\n # This will add file source that can be used in directives or controllers\n return file_path + ret['js'] + comment\n end\n\nend\n"
|