|
1 | | -require 'typescript-node' |
2 | | -require 'tilt' |
| 1 | +require 'typescript/rails/compiler' |
3 | 2 |
|
4 | | -module Typescript |
5 | | - module Rails |
6 | | - |
7 | | - class TypeScriptTemplate < ::Tilt::Template |
8 | | - self.default_mime_type = 'application/javascript' |
9 | | - |
10 | | - @@default_bare = false |
11 | | - |
12 | | - def self.default_bare |
13 | | - @@default_bare |
14 | | - end |
15 | | - |
16 | | - def self.default_bare=(value) |
17 | | - @@default_bare = value |
18 | | - end |
19 | | - |
20 | | - # @deprecated |
21 | | - def self.default_no_wrap |
22 | | - @@default_bare |
23 | | - end |
24 | | - |
25 | | - # @deprecated |
26 | | - def self.default_no_wrap=(value) |
27 | | - @@default_bare = value |
28 | | - end |
29 | | - |
30 | | - def self.engine_initialized? |
31 | | - defined? ::TypeScript |
32 | | - end |
33 | | - |
34 | | - def initialize_engine |
35 | | - require_template_library 'coffee_script' |
36 | | - end |
37 | | - |
38 | | - def prepare |
39 | | - if !options.key?(:bare) and !options.key?(:no_wrap) |
40 | | - options[:bare] = self.class.default_bare |
41 | | - end |
42 | | - end |
43 | | - |
44 | | - def evaluate(scope, locals, &block) |
45 | | - source = Typescript::Rails.replace_relative_references(file, data) |
46 | | - # TODO: employs source-maps |
47 | | - @output ||= TypeScript::Node.compile(source, '--target', 'ES5') |
48 | | - end |
49 | | - |
50 | | - def allows_script? |
51 | | - false |
52 | | - end |
| 3 | +class Typescript::Rails::TemplateHandler |
| 4 | + class << self |
| 5 | + def erb_handler |
| 6 | + @erb_handler ||= ActionView::Template.registered_template_handler(:erb) |
53 | 7 | end |
54 | 8 |
|
55 | | - class TemplateHandler |
56 | | - |
57 | | - def self.erb_handler |
58 | | - @@erb_handler ||= ActionView::Template.registered_template_handler(:erb) |
59 | | - end |
60 | | - |
61 | | - def self.call(template) |
62 | | - compiled_source = erb_handler.call(template) |
63 | | - escaped_path = template.identifier.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\' |
64 | | - <<-EOS |
65 | | - TypeScript::Node.compile( |
66 | | - Typescript::Rails.replace_relative_references( |
67 | | - '#{escaped_path}', (begin;#{compiled_source};end) |
68 | | - ), |
69 | | - '--target', 'ES5' |
70 | | - ) |
71 | | - EOS |
72 | | - end |
73 | | - end |
74 | | - |
75 | | - # Replace relative paths specified in /// <reference path="..." /> with absolute paths. |
76 | | - # |
77 | | - # @param [String] ts_path Source .ts path |
78 | | - # @param [String] ts source. It might be pre-processed by erb. |
79 | | - # @return [String] replaces source |
80 | | - def self.replace_relative_references(ts_path, source) |
81 | | - ts_dir = File.dirname(File.expand_path(ts_path)) |
82 | | - escaped_dir = ts_dir.gsub(/["\\]/, '\\\\\&') # "\"" => "\\\"", '\\' => '\\\\' |
83 | | - |
84 | | - # Why don't we just use gsub? Because it display odd behavior with File.join on Ruby 2.0 |
85 | | - # So we go the long way around. |
86 | | - output = '' |
87 | | - source.each_line do |l| |
88 | | - if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path="([^"]+)"\s*/>\s*!.match(l)).nil? |
89 | | - l = l.sub(m.captures[0], File.join(escaped_dir, m.captures[0])) |
90 | | - end |
91 | | - |
92 | | - output = output + l + $/ |
93 | | - end |
94 | | - |
95 | | - output |
| 9 | + def call(template) |
| 10 | + compiled_source = erb_handler.call(template) |
| 11 | + path = template.identifier.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\' |
| 12 | + <<-EOS |
| 13 | + ::Typescript::Rails::Compiler.compile('#{path}', (begin;#{compiled_source};end)) |
| 14 | + EOS |
96 | 15 | end |
97 | 16 | end |
98 | 17 | end |
|
0 commit comments