Skip to content

Commit

Permalink
Merge fda85ef into cef909f
Browse files Browse the repository at this point in the history
  • Loading branch information
wtmcc committed Feb 10, 2015
2 parents cef909f + fda85ef commit ea10a27
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Configurations:
Typescript::Rails::Compiler.default_options = [ ... ]
```

## Referenced TypeScript dependencies

`typescript-rails` recurses through all [TypeScript-style](https://github.com/teppeis/typescript-spec-md/blob/master/en/ch11.md#1111-source-files-dependencies) referenced files and tells its [`Sprockets::Context`](https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb) that the TS file being processed [`depend`s`_on`](https://github.com/sstephenson/sprockets#the-depend_on-directive) each file listed as a reference. This activates Sprocket’s cache-invalidation behavior when any of the descendant references of the root TS file is changed.

## Contributing

Expand Down
33 changes: 30 additions & 3 deletions lib/typescript/rails/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,46 @@ def replace_relative_references(ts_path, source)
# 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]))
if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path=(?:"([^"]+)"|'([^']+)')\s*/>\s*!.match(l)).nil?
matched_path = m.captures.compact[0]
l = l.sub(matched_path, File.join(escaped_dir, matched_path))
end
next l
end).join

output
end

# Get all references
#
# @param [String] path Source .ts path
# @param [String] source. It might be pre-processed by erb.
# @yieldreturn [String] matched ref abs_path
def get_all_reference_paths(path, source, visited_paths=Set.new, &block)
visited_paths << path
source ||= File.read(path)
source.each_line do |l|
if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path=(?:"([^"]+)"|'([^']+)')\s*/>\s*!.match(l)).nil?
matched_path = m.captures.compact[0]
abs_matched_path = File.expand_path(matched_path, File.dirname(path))
unless visited_paths.include? abs_matched_path
block.call abs_matched_path
get_all_reference_paths(abs_matched_path, nil, visited_paths, &block)
end
end
end
end

# @param [String] ts_path
# @param [String] source TypeScript source code
# @param [Sprockets::Context] sprockets context object
# @return [String] compiled JavaScript source code
def compile(ts_path, source, *options)
def compile(ts_path, source, context=nil, *options)
if context
get_all_reference_paths(File.expand_path(ts_path), source) do |abs_path|
context.depend_on abs_path
end
end
s = replace_relative_references(ts_path, source)
::TypeScript::Node.compile(s, *default_options, *options)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/typescript/rails/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def prepare
end
end

def evaluate(scope, locals, &block)
@output ||= ::Typescript::Rails::Compiler.compile(file, data)
def evaluate(context, locals, &block)
@output ||= ::Typescript::Rails::Compiler.compile(file, data, context)
end

# @override
Expand Down
1 change: 1 addition & 0 deletions test/assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def assets

test "assets .js.ts is compiled from TypeScript to JavaScript" do
assert { assets["javascripts/hello"].present? }
assert { assets["javascripts/hello"].send(:dependency_paths).map(&:pathname).map(&:to_s).include? File.expand_path("#{File.dirname(__FILE__)}/fixtures/assets/javascripts/included.ts") }
assert { assets["javascripts/hello"].body.include?('var s = "Hello, world!";') }
end
end
3 changes: 2 additions & 1 deletion test/fixtures/assets/javascripts/hello.js.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/// <reference path="reference.ts" />
var s: string = "Hello, world!";
console.log(s)
log_to_console(s);
4 changes: 4 additions & 0 deletions test/fixtures/assets/javascripts/included.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// <reference path="reference.ts" />
var log_to_console = function(x: string): void {
console.log(x)
}
2 changes: 2 additions & 0 deletions test/fixtures/assets/javascripts/reference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="hello.js.ts" />
/// <reference path="included.ts" />

0 comments on commit ea10a27

Please sign in to comment.