traceur-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +20 -0
  6. data/Rakefile +12 -0
  7. data/lib/traceur-rails.rb +5 -0
  8. data/lib/traceur/config.rb +38 -0
  9. data/lib/traceur/sprockets.rb +4 -0
  10. data/lib/traceur/support/runner.js +21 -0
  11. data/lib/traceur/support/traceur/README.md +40 -0
  12. data/lib/traceur/support/traceur/bin/traceur-runtime.js +2039 -0
  13. data/lib/traceur/support/traceur/bin/traceur.js +23037 -0
  14. data/lib/traceur/support/traceur/package.json +60 -0
  15. data/lib/traceur/support/traceur/src/node/System.js +38 -0
  16. data/lib/traceur/support/traceur/src/node/api.js +124 -0
  17. data/lib/traceur/support/traceur/src/node/command.js +213 -0
  18. data/lib/traceur/support/traceur/src/node/compile-single-file.js +69 -0
  19. data/lib/traceur/support/traceur/src/node/compiler.js +113 -0
  20. data/lib/traceur/support/traceur/src/node/deferred.js +110 -0
  21. data/lib/traceur/support/traceur/src/node/file-util.js +73 -0
  22. data/lib/traceur/support/traceur/src/node/getopt.js +147 -0
  23. data/lib/traceur/support/traceur/src/node/inline-module.js +150 -0
  24. data/lib/traceur/support/traceur/src/node/interpreter.js +33 -0
  25. data/lib/traceur/support/traceur/src/node/nodeLoader.js +41 -0
  26. data/lib/traceur/support/traceur/src/node/require.js +85 -0
  27. data/lib/traceur/support/traceur/src/node/to-amd-compiler.js +33 -0
  28. data/lib/traceur/support/traceur/src/node/to-commonjs-compiler.js +33 -0
  29. data/lib/traceur/support/traceur/src/node/traceur.js +32 -0
  30. data/lib/traceur/support/traceur/traceur +3 -0
  31. data/lib/traceur/template.rb +61 -0
  32. data/lib/traceur/tilt.rb +4 -0
  33. data/lib/traceur/version.rb +3 -0
  34. data/test/template_test.rb +45 -0
  35. data/test/test_helper.rb +13 -0
  36. data/traceur-rails.gemspec +26 -0
  37. metadata +151 -0
@@ -0,0 +1,33 @@
1
+ // Copyright 2013 Traceur Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ 'use strict';
16
+
17
+ var fs = require('fs');
18
+ var traceur = require('./traceur.js');
19
+ var nodeLoader = require('./nodeLoader.js');
20
+
21
+ function interpret(filename, argv, flags) {
22
+ // Interpret the filename argument as a platform-independent,
23
+ // normalized module name.
24
+ var moduleName = filename.replace(/\\/g, '/').replace(/\.js$/,'');
25
+ System.import(moduleName).then(function() {
26
+
27
+ }).catch(function(err) {
28
+ console.error(err.stack || err);
29
+ process.exit(8);
30
+ });
31
+ }
32
+
33
+ module.exports = interpret;
@@ -0,0 +1,41 @@
1
+ // Copyright 2013 Traceur Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ var fs = require('fs');
16
+
17
+ function stripShebang(data) {
18
+ if (/^#!/.test(data))
19
+ data = '//' + data;
20
+ return data;
21
+ }
22
+
23
+ var nodeLoader = {
24
+ load: function(url, callback, errback) {
25
+ fs.readFile(url, 'utf8', function(err, data) {
26
+ if (err) {
27
+ err.message = err.message.replace('ENOENT, open', 'File not found');
28
+ errback(err);
29
+ } else {
30
+ callback(stripShebang(data));
31
+ }
32
+ });
33
+
34
+ // Returns an abort function.
35
+ return function() {
36
+ callback = function() {};
37
+ };
38
+ }
39
+ };
40
+
41
+ module.exports = nodeLoader;
@@ -0,0 +1,85 @@
1
+ // Copyright 2013 Traceur Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ 'use strict';
16
+
17
+ var fs = require('fs');
18
+ var Module = require('module');
19
+ var traceur = require('./traceur.js');
20
+
21
+ var ErrorReporter = traceur.util.ErrorReporter;
22
+ var FromOptionsTransformer = traceur.codegeneration.FromOptionsTransformer;
23
+ var Parser = traceur.syntax.Parser;
24
+ var SourceFile = traceur.syntax.SourceFile;
25
+ var TreeWriter = traceur.outputgeneration.TreeWriter;
26
+
27
+ var ext = '.traceur-compiled';
28
+
29
+ Module._extensions[ext] = function(module, filename) {
30
+ module.filename = filename.slice(0, -ext.length);
31
+ module._compile(module.compiledCode, module.filename);
32
+ };
33
+
34
+ function compile(filename) {
35
+ traceur.options.modules = 'commonjs';
36
+
37
+ var contents = fs.readFileSync(filename, 'utf-8');
38
+ var sourceFile = new SourceFile(filename, contents);
39
+ var parser = new Parser(sourceFile);
40
+ var tree = parser.parseModule();
41
+ var reporter = new ErrorReporter();
42
+ var transformer = new FromOptionsTransformer(reporter);
43
+ tree = transformer.transform(tree);
44
+ if (reporter.hadError())
45
+ throw new Error('Error transforming ' + filename);
46
+ return TreeWriter.write(tree);
47
+ }
48
+
49
+ function traceurRequire(filename) {
50
+ var source = compile(filename);
51
+ var module = new Module(filename, require.main);
52
+ module.compiledCode = source;
53
+ module.load(filename + ext);
54
+ return module.exports;
55
+ }
56
+
57
+ var filters = [];
58
+ var originalRequireJs = Module._extensions['.js'];
59
+
60
+ function shouldCompile(filename) {
61
+ if (filters.length === 0)
62
+ return true;
63
+ for (var i = 0; i < filters.length; i++) {
64
+ if (filters[i].call(null, filename))
65
+ return true;
66
+ }
67
+ return false;
68
+ }
69
+
70
+ traceurRequire.makeDefault = function(filter) {
71
+ if (!filter)
72
+ filters = [];
73
+ else
74
+ filters.push(filter);
75
+
76
+ Module._extensions['.js'] = function(module, filename) {
77
+ if (shouldCompile(filename)) {
78
+ var source = compile(filename)
79
+ return module._compile(source, filename);
80
+ }
81
+ return originalRequireJs(module, filename);
82
+ };
83
+ };
84
+
85
+ module.exports = traceurRequire;
@@ -0,0 +1,33 @@
1
+ // Copyright 2013 Traceur Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ 'use strict';
16
+
17
+ var traceur = require('./traceur.js');
18
+ var compileAllJsFilesInDir =
19
+ require('./compile-single-file.js').compileAllJsFilesInDir;
20
+
21
+ if (process.argv.length < 4) {
22
+ console.log('Not enough arguments!\n' +
23
+ ' Usage node src/node/to-amd-compiler.js <inputDirectory> <outputDirectory>');
24
+ process.exit(1);
25
+ }
26
+
27
+ // Nasty, we should rather pass the options to FromOptionsTransformer
28
+ traceur.options.modules = 'amd';
29
+
30
+ var inputDir = process.argv[2];
31
+ var outputDir = process.argv[3];
32
+
33
+ compileAllJsFilesInDir(inputDir, outputDir, true);
@@ -0,0 +1,33 @@
1
+ // Copyright 2013 Traceur Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ 'use strict';
16
+
17
+ var traceur = require('./traceur.js');
18
+ var compileAllJsFilesInDir =
19
+ require('./compile-single-file.js').compileAllJsFilesInDir;
20
+
21
+ if (process.argv.length < 4) {
22
+ console.log('Not enough arguments!\n' +
23
+ ' Usage node src/node/to-commonjs-compiler.js <inputDirectory> <outputDirectory>');
24
+ process.exit(1);
25
+ }
26
+
27
+ // Nasty, we should rather pass the options to FromOptionsTransformer
28
+ traceur.options.modules = 'commonjs';
29
+
30
+ var inputDir = process.argv[2];
31
+ var outputDir = process.argv[3];
32
+
33
+ compileAllJsFilesInDir(inputDir, outputDir, true);
@@ -0,0 +1,32 @@
1
+ // Copyright 2012 Traceur Authors.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ var fs = require('fs');
16
+ var path = require('path');
17
+
18
+ var filename = '../../bin/traceur.js';
19
+ filename = path.join(path.dirname(module.filename), filename);
20
+ var data = fs.readFileSync(filename, 'utf8');
21
+ if (!data)
22
+ throw new Error('Failed to import ' + filename);
23
+
24
+ ('global', eval)(data);
25
+
26
+ // traceur is a module and thus frozen.
27
+ module.exports = {
28
+ __proto__: traceur,
29
+ get require() {
30
+ return require('./require.js');
31
+ }
32
+ };
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('./src/node/command.js');
@@ -0,0 +1,61 @@
1
+ require 'execjs'
2
+ require 'json'
3
+ require 'tilt'
4
+
5
+ module Traceur
6
+ class Template < ::Tilt::Template
7
+ self.default_mime_type = 'application/javascript'
8
+
9
+ Node = ::ExecJS::ExternalRuntime.new(
10
+ name: 'Node.js (V8)',
11
+ command: ['nodejs', 'node'],
12
+ runner_path: File.expand_path('../support/runner.js', __FILE__),
13
+ encoding: 'UTF-8'
14
+ )
15
+
16
+ def prepare
17
+ # required
18
+ end
19
+
20
+ def evaluate(scope, locals, &block)
21
+ @output ||= Node.exec(generate_source(scope))
22
+ end
23
+
24
+ private
25
+
26
+ def generate_source(scope)
27
+ <<-SOURCE
28
+ var traceur = require("#{traceur_path}");
29
+ var result = traceur.compile(#{::JSON.generate(data, quirks_mode: true)}, {
30
+ filename: '#{module_name(scope.root_path, scope.logical_path)}',
31
+ modules: 'register'
32
+ });
33
+
34
+ if (result.error) {
35
+ throw result.error;
36
+ }
37
+
38
+ return result.js;
39
+ SOURCE
40
+ end
41
+
42
+ def module_name(root_path, logical_path)
43
+ path = ''
44
+ if prefix = Traceur::Config.lookup_prefix(File.join(root_path, logical_path))
45
+ path = File.join(prefix, logical_path)
46
+ else
47
+ path = logical_path
48
+ end
49
+
50
+ if Traceur::Config.transform
51
+ path = Traceur::Config.transform.call(path)
52
+ end
53
+
54
+ path
55
+ end
56
+
57
+ def traceur_path
58
+ File.expand_path('../support/traceur/src/node/api.js', __FILE__)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ require 'tilt'
2
+ require 'traceur/template'
3
+
4
+ Tilt.register Traceur::Template, '.tc'
@@ -0,0 +1,3 @@
1
+ module Traceur
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+ require 'traceur-rails'
3
+ require 'execjs'
4
+
5
+ describe Traceur::Template do
6
+
7
+ Scope = Struct.new('Scope', :root_path, :logical_path)
8
+
9
+ before do
10
+ @source = <<-JS
11
+ import dep from 'dep';
12
+
13
+ var foo = function() {
14
+ console.log('bar');
15
+ };
16
+
17
+ export default foo;
18
+ JS
19
+ @source.strip!
20
+ @scope = Scope.new('', 'foo')
21
+ end
22
+
23
+ it 'transpiles tc files' do
24
+ expected = <<-JS
25
+ System.register("foo", [], function() {
26
+ "use strict";
27
+ var __moduleName = "foo";
28
+ var dep = $traceurRuntime.assertObject(System.get("dep")).default;
29
+ var foo = function() {
30
+ console.log('bar');
31
+ };
32
+ var $__default = foo;
33
+ return {get default() {
34
+ return $__default;
35
+ }};
36
+ });
37
+ JS
38
+ expected.lstrip!
39
+
40
+ template = Traceur::Template.new { @source }
41
+ template.render(@scope).must_equal expected
42
+ end
43
+
44
+
45
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+
3
+ if defined?(M)
4
+ require 'minitest/spec'
5
+ else
6
+ require 'minitest/autorun'
7
+ end
8
+
9
+ class MiniTest::Spec
10
+ class << self
11
+ alias :context :describe
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'traceur/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'traceur-rails'
8
+ spec.version = Traceur::VERSION
9
+ spec.authors = ['Aaron Ackerman']
10
+ spec.email = ['theron17@gmail.com']
11
+ spec.summary = %q{Traceur Compiler integration with Sprockets}
12
+ spec.description = %q{Compile Traceur Compiler supported Javascript with Sprockets}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'execjs'
22
+ spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'tilt'
25
+ spec.add_development_dependency 'sprockets', '> 2.0.0'
26
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traceur-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Ackerman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: execjs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tilt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sprockets
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ description: Compile Traceur Compiler supported Javascript with Sprockets
84
+ email:
85
+ - theron17@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/traceur-rails.rb
96
+ - lib/traceur/config.rb
97
+ - lib/traceur/sprockets.rb
98
+ - lib/traceur/support/runner.js
99
+ - lib/traceur/support/traceur/README.md
100
+ - lib/traceur/support/traceur/bin/traceur-runtime.js
101
+ - lib/traceur/support/traceur/bin/traceur.js
102
+ - lib/traceur/support/traceur/package.json
103
+ - lib/traceur/support/traceur/src/node/System.js
104
+ - lib/traceur/support/traceur/src/node/api.js
105
+ - lib/traceur/support/traceur/src/node/command.js
106
+ - lib/traceur/support/traceur/src/node/compile-single-file.js
107
+ - lib/traceur/support/traceur/src/node/compiler.js
108
+ - lib/traceur/support/traceur/src/node/deferred.js
109
+ - lib/traceur/support/traceur/src/node/file-util.js
110
+ - lib/traceur/support/traceur/src/node/getopt.js
111
+ - lib/traceur/support/traceur/src/node/inline-module.js
112
+ - lib/traceur/support/traceur/src/node/interpreter.js
113
+ - lib/traceur/support/traceur/src/node/nodeLoader.js
114
+ - lib/traceur/support/traceur/src/node/require.js
115
+ - lib/traceur/support/traceur/src/node/to-amd-compiler.js
116
+ - lib/traceur/support/traceur/src/node/to-commonjs-compiler.js
117
+ - lib/traceur/support/traceur/src/node/traceur.js
118
+ - lib/traceur/support/traceur/traceur
119
+ - lib/traceur/template.rb
120
+ - lib/traceur/tilt.rb
121
+ - lib/traceur/version.rb
122
+ - test/template_test.rb
123
+ - test/test_helper.rb
124
+ - traceur-rails.gemspec
125
+ homepage: ''
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.2.2
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Traceur Compiler integration with Sprockets
149
+ test_files:
150
+ - test/template_test.rb
151
+ - test/test_helper.rb