Skip to content

Commit e56a1dc

Browse files
committed
Merge pull request #24 from wtmcc/depend-on-references
expire root TS cache in response to change in referenced `.ts` files
2 parents cef909f + 7c4e15b commit e56a1dc

File tree

8 files changed

+45
-6
lines changed

8 files changed

+45
-6
lines changed

Diff for: Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ gemspec
55

66
group :test do
77
gem 'rails', '~> 4.0'
8+
gem 'sprockets-rails', '> 2.0'
89
gem 'minitest-power_assert'
910
gem 'coveralls'
1011
gem 'simplecov'

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Configurations:
4343
Typescript::Rails::Compiler.default_options = [ ... ]
4444
```
4545

46+
## Referenced TypeScript dependencies
47+
48+
`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.
4649

4750
## Contributing
4851

Diff for: lib/typescript/rails/compiler.rb

+30-3
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,46 @@ def replace_relative_references(ts_path, source)
1818
# Why don't we just use gsub? Because it display odd behavior with File.join on Ruby 2.0
1919
# So we go the long way around.
2020
output = (source.each_line.map do |l|
21-
if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path="([^"]+)"\s*/>\s*!.match(l)).nil?
22-
l = l.sub(m.captures[0], File.join(escaped_dir, m.captures[0]))
21+
if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path=(?:"([^"]+)"|'([^']+)')\s*/>\s*!.match(l)).nil?
22+
matched_path = m.captures.compact[0]
23+
l = l.sub(matched_path, File.join(escaped_dir, matched_path))
2324
end
2425
next l
2526
end).join
2627

2728
output
2829
end
2930

31+
# Get all references
32+
#
33+
# @param [String] path Source .ts path
34+
# @param [String] source. It might be pre-processed by erb.
35+
# @yieldreturn [String] matched ref abs_path
36+
def get_all_reference_paths(path, source, visited_paths=Set.new, &block)
37+
visited_paths << path
38+
source ||= File.read(path)
39+
source.each_line do |l|
40+
if l.starts_with?('///') && !(m = %r!^///\s*<reference\s+path=(?:"([^"]+)"|'([^']+)')\s*/>\s*!.match(l)).nil?
41+
matched_path = m.captures.compact[0]
42+
abs_matched_path = File.expand_path(matched_path, File.dirname(path))
43+
unless visited_paths.include? abs_matched_path
44+
block.call abs_matched_path
45+
get_all_reference_paths(abs_matched_path, nil, visited_paths, &block)
46+
end
47+
end
48+
end
49+
end
50+
3051
# @param [String] ts_path
3152
# @param [String] source TypeScript source code
53+
# @param [Sprockets::Context] sprockets context object
3254
# @return [String] compiled JavaScript source code
33-
def compile(ts_path, source, *options)
55+
def compile(ts_path, source, context=nil, *options)
56+
if context
57+
get_all_reference_paths(File.expand_path(ts_path), source) do |abs_path|
58+
context.depend_on abs_path
59+
end
60+
end
3461
s = replace_relative_references(ts_path, source)
3562
::TypeScript::Node.compile(s, *default_options, *options)
3663
end

Diff for: lib/typescript/rails/template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def prepare
2121
end
2222
end
2323

24-
def evaluate(scope, locals, &block)
25-
@output ||= ::Typescript::Rails::Compiler.compile(file, data)
24+
def evaluate(context, locals, &block)
25+
@output ||= ::Typescript::Rails::Compiler.compile(file, data, context)
2626
end
2727

2828
# @override

Diff for: test/assets_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def assets
4242

4343
test "assets .js.ts is compiled from TypeScript to JavaScript" do
4444
assert { assets["javascripts/hello"].present? }
45+
assert { assets["javascripts/hello"].send(:dependency_paths).map(&:pathname).map(&:to_s).include? File.expand_path("#{File.dirname(__FILE__)}/fixtures/assets/javascripts/included.ts") }
4546
assert { assets["javascripts/hello"].body.include?('var s = "Hello, world!";') }
4647
end
4748
end

Diff for: test/fixtures/assets/javascripts/hello.js.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
/// <reference path="reference.ts" />
12
var s: string = "Hello, world!";
2-
console.log(s)
3+
log_to_console(s);

Diff for: test/fixtures/assets/javascripts/included.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <reference path="reference.ts" />
2+
var log_to_console = function(x: string): void {
3+
console.log(x)
4+
}

Diff for: test/fixtures/assets/javascripts/reference.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference path="hello.js.ts" />
2+
/// <reference path="included.ts" />

0 commit comments

Comments
 (0)