Module: Typescript::Rails::Compiler

Defined in:
lib/typescript/rails/compiler.rb

Class Method Summary collapse

Class Method Details

.compile(ts_path, source, *options) ⇒ String

Returns compiled JavaScript source code.

Parameters:

  • ts_path (String)
  • source (String)

    TypeScript source code

Returns:

  • (String)

    compiled JavaScript source code



33
34
35
36
# File 'lib/typescript/rails/compiler.rb', line 33

def compile(ts_path, source, *options)
  s = replace_relative_references(ts_path, source)
  ::TypeScript::Node.compile(s, *default_options, *options)
end

.default_optionsObject



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.

Parameters:

  • ts_path (String)

    Source .ts path

  • source. (String)

    It might be pre-processed by erb.

Returns:

  • (String)

    replaces source



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.expand_path(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