Module: Typescript::Rails::Compiler
- Defined in:
- lib/typescript/rails/compiler.rb
Class Method Summary collapse
-
.compile(ts_path, source, *options) ⇒ String
Compiled JavaScript source code.
- .default_options ⇒ Object
-
.replace_relative_references(ts_path, source) ⇒ String
Replace relative paths specified in /// <reference path=“…” /> with absolute paths.
Class Method Details
.compile(ts_path, source, *options) ⇒ String
Returns compiled JavaScript source code.
33 34 35 36 |
# File 'lib/typescript/rails/compiler.rb', line 33 def compile(ts_path, source, *) s = replace_relative_references(ts_path, source) ::TypeScript::Node.compile(s, *, *) end |
.default_options ⇒ Object
7 |
# File 'lib/typescript/rails/compiler.rb', line 7 cattr_accessor :default_options |
.replace_relative_references(ts_path, source) ⇒ String
Replace relative paths specified in /// <reference path=“…” /> with absolute paths.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/typescript/rails/compiler.rb', line 14 def replace_relative_references(ts_path, source) ts_dir = File.dirname(File.(ts_path)) escaped_dir = ts_dir.gsub(/["\\]/, '\\\\\&') # "\"" => "\\\"", '\\' => '\\\\' # Why don't we just use gsub? Because it display odd behavior with File.join on Ruby 2.0 # So we go the long way around. output = (source.each_line.map do |l| if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path="([^"]+)"\s*/>\s*!.match(l)).nil? l = l.sub(m.captures[0], File.join(escaped_dir, m.captures[0])) end next l end).join output end |