sprockets-typescript 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ require "sprockets"
2
+ require "sprockets/typescript/compiler"
3
+ require "sprockets/typescript/template"
4
+
5
+ Sprockets.register_engine ".ts", Sprockets::Typescript::Template
@@ -0,0 +1,84 @@
1
+ require "sprockets"
2
+ require "v8"
3
+
4
+ module Sprockets
5
+ module Typescript
6
+ class Compiler
7
+ DEFAULT_LIB_PATH = File.expand_path("../../../../bundledjs/lib.d.ts", __FILE__)
8
+
9
+ class Console
10
+ def log(s)
11
+ $stderr.puts(s)
12
+ end
13
+ end
14
+
15
+ class Unit
16
+ attr_reader :path, :content
17
+
18
+ def initialize(path, content = nil)
19
+ @path = path
20
+ @content = content
21
+ end
22
+ end
23
+
24
+ class Context
25
+ def initialize(context)
26
+ @context = context
27
+ end
28
+
29
+ def resolve(path)
30
+ if path == DEFAULT_LIB_PATH
31
+ path
32
+ else
33
+ @context.resolve(path, :content_type => :self).to_s
34
+ end
35
+ end
36
+
37
+ def evaluate(path)
38
+ pathname = Pathname.new(path)
39
+ attributes = @context.environment.attributes_for(path)
40
+ processors = attributes.processors
41
+ processors = processors.reject { |p| p == Sprockets::Typescript::Template }
42
+ if defined?(Sprockets::CommonJS)
43
+ processors = processors.reject { |p| p == Sprockets::CommonJS }
44
+ end
45
+
46
+ context = @context.environment.context_class.new(@context.environment, attributes.logical_path, pathname)
47
+ content = context.evaluate(pathname, :processors => processors)
48
+ { :content => content, :context => self.class.new(context) }
49
+ end
50
+
51
+ def depends_on(path)
52
+ @context.depend_on_asset(path)
53
+ end
54
+
55
+ def require(path)
56
+ @context.require_asset(path)
57
+ end
58
+ end
59
+
60
+ def initialize
61
+ @ctx = V8::Context.new
62
+ %w(typescript.patched compiler).each do |filename|
63
+ @ctx.load(File.expand_path("../../../../bundledjs/#{filename}.js", __FILE__))
64
+ end
65
+ end
66
+
67
+ def eval(*args)
68
+ @ctx.eval(*args)
69
+ end
70
+
71
+ def compile(path, content, context = nil)
72
+ libdts = Unit.new(DEFAULT_LIB_PATH, File.read(DEFAULT_LIB_PATH))
73
+ additional_units = [libdts]
74
+ @ctx["Ruby"] = {
75
+ "source" => Unit.new(path, content),
76
+ "additionalUnits" => additional_units,
77
+ "context" => context.nil? ? nil : Context.new(context),
78
+ "console" => Console.new
79
+ }
80
+ @ctx.eval("Compiler.compile()")
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,18 @@
1
+ require "tilt"
2
+ require "sprockets/typescript/compiler"
3
+
4
+ module Sprockets
5
+ module Typescript
6
+ class Template < ::Tilt::Template
7
+ self.default_mime_type = "text/javascript"
8
+
9
+ def prepare
10
+ @compiler = Compiler.new
11
+ end
12
+
13
+ def evaluate(context, locals, &block)
14
+ @compiler.compile(context.pathname.to_s, data, context)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ var x: number = <%= 2 + 3 %>;
@@ -0,0 +1 @@
1
+ var add = (x: number, y: number): number => x + y
@@ -0,0 +1,5 @@
1
+ module Bar {
2
+ export function bar() {
3
+ return "<%= "bar" %>";
4
+ }
5
+ }
@@ -0,0 +1,3 @@
1
+ ///<reference path="./sub/moo"/>
2
+
3
+ Moo.moo();
@@ -0,0 +1,7 @@
1
+ ///<reference path="bar"/>
2
+
3
+ module Moo {
4
+ export function moo() {
5
+ return Bar.bar() + "moo";
6
+ }
7
+ }
@@ -0,0 +1,3 @@
1
+ export function moo() {
2
+ return "moo";
3
+ }
@@ -0,0 +1,3 @@
1
+ import foo = module("bar");
2
+
3
+ foo.moo();
@@ -0,0 +1,5 @@
1
+ module Bar {
2
+ export function bar(): string {
3
+ return "bar";
4
+ }
5
+ }
@@ -0,0 +1,3 @@
1
+ ///<reference path="bar"/>
2
+
3
+ Bar.bar();
@@ -0,0 +1,5 @@
1
+ //= require bar
2
+
3
+ ///<reference path="bar"/>
4
+
5
+ Bar.bar();
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe Sprockets::Typescript::Compiler do
4
+ context "#new" do
5
+ it { expect { described_class.new }.to_not raise_error }
6
+ end
7
+
8
+ context "instance" do
9
+ let(:instance) { described_class.new }
10
+
11
+ context "#eval('typeof TypeScript')" do
12
+ subject { instance.eval("typeof TypeScript") }
13
+ it { should eql "object" }
14
+ end
15
+
16
+ context "#compile source " do
17
+ context "Math.abs(<number>-5);" do
18
+ subject { instance.compile("test.ts", "Math.abs(<number>-5);").chomp }
19
+ it { should eql "Math.abs(-5);" }
20
+ end
21
+
22
+ context "Math.abs('hello');" do
23
+ let(:source) { "Math.abs('hello');" }
24
+ it "should raise error" do
25
+ expect { instance.compile("test.ts", source) }.to raise_error
26
+ end
27
+
28
+ context "exception message" do
29
+ subject do
30
+ begin
31
+ instance.compile("test.ts", source)
32
+ rescue V8::JSError => e
33
+ e.message
34
+ end
35
+ end
36
+ it { should eql "TypeScript compiler: test.ts (1,18): Supplied parameters do not match any signature of call target" }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1 @@
1
+ require "sprockets-typescript"
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ describe Sprockets::Environment do
4
+ let(:environment) { described_class.new(File.expand_path("../../", __FILE__)) }
5
+
6
+ context "assets/template1 directory" do
7
+ before(:all) do
8
+ environment.clear_paths
9
+ environment.append_path("spec/assets/template1")
10
+ end
11
+
12
+ context "#find_asset('foo')" do
13
+ subject { environment.find_asset("foo") }
14
+ it { should_not be_nil }
15
+ it { subject.to_s.should eql "var add = function (x, y) {\n return x + y;\n};\n" }
16
+ end
17
+
18
+ context "#find_assets('bar')" do
19
+ subject { environment.find_asset("bar") }
20
+ it { should_not be_nil }
21
+ it { subject.to_s.should eql "var x = 5;\n" }
22
+ end
23
+ end
24
+
25
+ context "assets/template2 directory" do
26
+ before(:all) do
27
+ environment.clear_paths
28
+ environment.append_path("spec/assets/template2")
29
+ end
30
+
31
+ context "#find_asset('foo')" do
32
+ subject { environment.find_asset("foo") }
33
+ it { should_not be_nil }
34
+ it { subject.to_s.should eql "Moo.moo();\n" }
35
+ end
36
+ end
37
+
38
+ context "assets/template3 directory" do
39
+ before(:all) do
40
+ environment.clear_paths
41
+ environment.append_path("spec/assets/template3")
42
+ end
43
+
44
+ context "#find_asset('foo')" do
45
+ subject { environment.find_asset("foo") }
46
+ it { should_not be_nil }
47
+ it { subject.to_s.should eql "function moo() {\n return \"moo\";\n}\nexports.moo = moo;\n\nvar foo = require(\"./bar\")\nfoo.moo();\n\n" }
48
+ end
49
+ end
50
+
51
+ context "assets/template4 directory" do
52
+ before(:all) do
53
+ environment.clear_paths
54
+ environment.append_path("spec/assets/template4")
55
+ end
56
+
57
+ context "#find_asset('foo')" do
58
+ subject { environment.find_asset("foo") }
59
+ it { should_not be_nil }
60
+ it { subject.to_s.should eql "Bar.bar();\n" }
61
+ end
62
+
63
+ context "#find_asset('moo')" do
64
+ subject { environment.find_asset("moo") }
65
+ it { should_not be_nil }
66
+ it { subject.to_s.should eql "var Bar;\n(function (Bar) {\n function bar() {\n return \"bar\";\n }\n Bar.bar = bar;\n})(Bar || (Bar = {}));\n\nBar.bar();\n" }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sprockets-typescript"
3
+ s.version = "1.0"
4
+ s.authors = "Anton Ageev"
5
+ s.email = "antage@gmail.com"
6
+ s.summary = "TypeScript compiler for Sprockets"
7
+
8
+ s.files = `git ls-files`.split("\n")
9
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
10
+
11
+ s.add_runtime_dependency "sprockets", "~> 2.1"
12
+ s.add_runtime_dependency "tilt"
13
+ s.add_runtime_dependency "therubyracer"
14
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-typescript
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anton Ageev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sprockets
16
+ requirement: &16375700 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *16375700
25
+ - !ruby/object:Gem::Dependency
26
+ name: tilt
27
+ requirement: &16375080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *16375080
36
+ - !ruby/object:Gem::Dependency
37
+ name: therubyracer
38
+ requirement: &16374140 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *16374140
47
+ description:
48
+ email: antage@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - .travis.yml
56
+ - Gemfile
57
+ - MIT-LICENSE
58
+ - README.md
59
+ - bundledjs/compiler.js
60
+ - bundledjs/lib.d.ts
61
+ - bundledjs/typescript.js
62
+ - bundledjs/typescript.patched.js
63
+ - lib/sprockets-typescript.rb
64
+ - lib/sprockets/typescript/compiler.rb
65
+ - lib/sprockets/typescript/template.rb
66
+ - spec/assets/template1/bar.js.ts.erb
67
+ - spec/assets/template1/foo.js.ts
68
+ - spec/assets/template2/bar.js.ts.erb
69
+ - spec/assets/template2/foo.js.ts
70
+ - spec/assets/template2/sub/moo.js.ts
71
+ - spec/assets/template3/bar.js.ts
72
+ - spec/assets/template3/foo.js.ts
73
+ - spec/assets/template4/bar.js.ts
74
+ - spec/assets/template4/foo.js.ts
75
+ - spec/assets/template4/moo.js.ts
76
+ - spec/compiler_spec.rb
77
+ - spec/spec_helper.rb
78
+ - spec/template_spec.rb
79
+ - sprockets-typescript.gemspec
80
+ homepage:
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.15
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: TypeScript compiler for Sprockets
104
+ test_files: []