Skip to content

Commit fda85ef

Browse files
committed
automatically depend on all files listed in reference tags
1 parent b6459a7 commit fda85ef

File tree

7 files changed

+41
-4
lines changed

7 files changed

+41
-4
lines changed

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

lib/typescript/rails/compiler.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,36 @@ def replace_relative_references(ts_path, source)
2828
output
2929
end
3030

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+
3151
# @param [String] ts_path
3252
# @param [String] source TypeScript source code
53+
# @param [Sprockets::Context] sprockets context object
3354
# @return [String] compiled JavaScript source code
34-
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
3561
s = replace_relative_references(ts_path, source)
3662
::TypeScript::Node.compile(s, *default_options, *options)
3763
end

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

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
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);
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+
}
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)