@@ -41,7 +41,8 @@ def prepare
4141 end
4242
4343 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 )
4546 end
4647
4748 def allows_script?
@@ -57,9 +58,27 @@ def self.erb_handler
5758
5859 def self . call ( template )
5960 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
6169 end
6270 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
6382 end
6483end
6584
0 commit comments