Sha256: ca01186f8296633590c05a66e07b0f2e94e95047021b98dff3455d189ce5c91e

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'typescript/rails'
require 'typescript-node'

module Typescript::Rails::Compiler
  class << self
    # @!scope class
    cattr_accessor :default_options

    # Replace relative paths specified in /// <reference path="..." /> with absolute paths.
    #
    # @param [String] ts_path Source .ts path
    # @param [String] source. It might be pre-processed by erb.
    # @return [String] replaces source
    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 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
        output = output + l + $/
      end

      output
    end

    # @param [String] ts_path
    # @param [String] source TypeScript source code
    # @return [String] compiled JavaScript source code
    def compile(ts_path, source, *options)
      s = replace_relative_references(ts_path, source)
      ::TypeScript::Node.compile(s, *default_options, *options)
    end

  end

  self.default_options = [
      '--target', 'ES5',
      '--noImplicitAny'
  ]
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
typescript-rails-0.4.1 lib/typescript/rails/compiler.rb
typescript-rails-0.4.0 lib/typescript/rails/compiler.rb