@@ -41,7 +41,8 @@ def prepare
41
41
end
42
42
43
43
def evaluate ( scope , locals , &block )
44
- @output ||= TypeScript ::Node . compile ( data )
44
+ source = Typescript ::Rails . replace_relative_references ( file , data )
45
+ @output ||= TypeScript ::Node . compile ( source )
45
46
end
46
47
47
48
def allows_script?
@@ -57,9 +58,27 @@ def self.erb_handler
57
58
58
59
def self . call ( template )
59
60
compiled_source = erb_handler . call ( template )
60
- "TypeScript::Node.compile(begin;#{ compiled_source } ;end)"
61
+ escaped_path = template . identifier . gsub ( /['\\ ]/ , '\\\\\&' ) # "'" => "\\'", '\\' => '\\\\'
62
+ <<-EOS
63
+ TypeScript::Node.compile(
64
+ Typescript::Rails.replace_relative_references(
65
+ '#{ escaped_path } ', (begin;#{ compiled_source } ;end)
66
+ )
67
+ )
68
+ EOS
61
69
end
62
70
end
71
+
72
+ # Replace relative paths specified in /// <reference path="..." /> with absolute paths.
73
+ #
74
+ # @param [String] ts_path Source .ts path
75
+ # @param [String] ts source. It might be pre-processed by erb.
76
+ # @return [String] replaces source
77
+ def self . replace_relative_references ( ts_path , source )
78
+ ts_dir = File . dirname ( File . expand_path ( ts_path ) )
79
+ escaped_dir = ts_dir . gsub ( /["\\ ]/ , '\\\\\&' ) # "\"" => "\\\"", '\\' => '\\\\'
80
+ source . gsub ( %r!(^///\s *<reference\s +path=")([^/"][^"]+)("\s */>)! , '\1' + File . join ( escaped_dir , '\2' ) + '\3' )
81
+ end
63
82
end
64
83
end
65
84
0 commit comments