es6_module_transpiler-rails 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -6
- data/lib/es6_module_transpiler/rails.rb +8 -0
- data/lib/es6_module_transpiler/rails/version.rb +1 -1
- data/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb +32 -10
- data/test/template_test.rb +61 -6
- data/test/test_helper.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ae863baf9c1ae97e82556f3e136ea9578e18693
|
4
|
+
data.tar.gz: bc4015b0fe5858cfc7ff8c71072ac89854616066
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d138a75a6dabd31af45c6ea00c86a90bd872c66bd32a6fb4ec6483fcac617569d6bb299e79716844ff5f46d759afcdc87d9bed99a0fd82145f13950bbdc8ceb1
|
7
|
+
data.tar.gz: c3f600e9c76fcc1df71b87a1dfd04479c4ae69020aae3abd4cd40c6eb24b0ba47fe685ab8dc985fd01b5b4478f9197d6e246d1c6048d9925e2dfb9ebe14b988d
|
data/README.md
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# ES6ModuleTranspiler-Rails #
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/dockyard/es6_module_transpiler-rails.png?branch=master)](http://travis-ci.org/dockyard/es6_module_transpiler-rails)
|
4
|
+
[![Dependency Status](https://gemnasium.com/dockyard/es6_module_transpiler-rails.png?travis)](https://gemnasium.com/dockyard/es6_module_transpiler-rails)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/dockyard/es6_module_transpiler-rails.png)](https://codeclimate.com/github/dockyard/es6_module_transpiler-rails)
|
2
6
|
|
3
7
|
Transpile ES6 Modules in the Rails Asset Pipeline
|
4
8
|
|
9
|
+
Uses [Square's ES6 Module Transpiler](https://github.com/square/es6-module-transpiler)
|
10
|
+
|
5
11
|
## Installation ##
|
6
12
|
|
7
13
|
**Node.js must be installed for the transpiling to happen**
|
@@ -12,7 +18,7 @@ gem 'es6_module_transpiler-rails'
|
|
12
18
|
|
13
19
|
## Usage ##
|
14
20
|
|
15
|
-
Your modules will transpile are named based upon their directory
|
21
|
+
Your modules will transpile and are named based upon their directory
|
16
22
|
nesting + filename, as long as the file has the `.es6` extension.
|
17
23
|
For example, `app/assets/javascripts/controllers/fooController.js.es6`
|
18
24
|
|
@@ -24,10 +30,10 @@ var fooController = function() {
|
|
24
30
|
export default = fooController;
|
25
31
|
```
|
26
32
|
|
27
|
-
will compile to
|
33
|
+
will compile to `/assets/controllers/fooController.js`
|
28
34
|
|
29
35
|
```js
|
30
|
-
define("controllers/fooController",
|
36
|
+
define("controllers/fooController",
|
31
37
|
["exports"],
|
32
38
|
function(__exports__) {
|
33
39
|
"use strict";
|
@@ -41,12 +47,27 @@ define("controllers/fooController",
|
|
41
47
|
|
42
48
|
### Compiling ###
|
43
49
|
|
44
|
-
By default your module will compile to an AMD.
|
50
|
+
By default your module will compile to an AMD. You can also compile it to globals or CommonJS by making the following switch:
|
45
51
|
|
46
52
|
```ruby
|
47
|
-
ES6ModuleTranspiler.compile_to = :
|
53
|
+
ES6ModuleTranspiler.compile_to = :globals
|
54
|
+
# or
|
55
|
+
ES6ModuleTranspiler.compile_to = :cjs
|
48
56
|
```
|
49
57
|
|
58
|
+
### Custom Module Prefix ###
|
59
|
+
|
60
|
+
You can match module names based upon a pattern to apply a prefix to the
|
61
|
+
name:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
ES6ModuleTranspiler.prefix_pattern = [/^(controllers|models|views|helpers|routes|router|store)/, 'app']
|
65
|
+
```
|
66
|
+
|
67
|
+
This would match names that start with the pattern and prepend with
|
68
|
+
`app/`. For example, `controllers/fooController` would now be named
|
69
|
+
`app/controllers/fooController`.
|
70
|
+
|
50
71
|
## Authors ##
|
51
72
|
|
52
73
|
[Brian Cardarella](http://twitter.com/bcardarella)
|
@@ -60,6 +81,7 @@ This gem follows [Semantic Versioning](http://semver.org)
|
|
60
81
|
## Want to help? ##
|
61
82
|
|
62
83
|
Please do! We are always looking to improve this gem.
|
84
|
+
|
63
85
|
## Legal ##
|
64
86
|
|
65
87
|
[DockYard](http://dockyard.com), LLC © 2013
|
@@ -1,19 +1,23 @@
|
|
1
|
+
require 'execjs'
|
2
|
+
|
1
3
|
module Tilt
|
2
4
|
class ES6ModuleTranspilerTemplate < Tilt::Template
|
3
5
|
self.default_mime_type = 'application/javascript'
|
4
|
-
|
6
|
+
|
7
|
+
Node = ::ExecJS::ExternalRuntime.new(
|
8
|
+
name: 'Node.js (V8)',
|
9
|
+
command: ['nodejs', 'node'],
|
10
|
+
runner_path: File.expand_path('../../support/es6_node_runner.js', __FILE__),
|
11
|
+
encoding: 'UTF-8'
|
12
|
+
)
|
5
13
|
|
6
14
|
def prepare
|
7
|
-
|
8
|
-
|
9
|
-
command: ['nodejs', 'node'],
|
10
|
-
runner_path: File.expand_path('../../support/es6_node_runner.js', __FILE__),
|
11
|
-
encoding: 'UTF-8'
|
12
|
-
)
|
15
|
+
# intentionally left empty
|
16
|
+
# Tilt requires this method to be defined
|
13
17
|
end
|
14
18
|
|
15
19
|
def evaluate(scope, locals, &block)
|
16
|
-
@output ||=
|
20
|
+
@output ||= Node.exec(generate_source(scope))
|
17
21
|
end
|
18
22
|
|
19
23
|
private
|
@@ -26,9 +30,27 @@ module Tilt
|
|
26
30
|
source = <<-SOURCE
|
27
31
|
var Compiler, compiler, output;
|
28
32
|
Compiler = require("#{transpiler_path}").Compiler;
|
29
|
-
compiler = new Compiler(#{::JSON.generate(data, quirks_mode: true)}, '#{scope.logical_path}');
|
30
|
-
return output = compiler.#{
|
33
|
+
compiler = new Compiler(#{::JSON.generate(data, quirks_mode: true)}, '#{module_name(scope.logical_path)}');
|
34
|
+
return output = compiler.#{compiler_method}();
|
31
35
|
SOURCE
|
32
36
|
end
|
37
|
+
|
38
|
+
def module_name(path)
|
39
|
+
if ES6ModuleTranspiler.prefix_pattern[0] === path
|
40
|
+
path = "#{ES6ModuleTranspiler.prefix_pattern[1]}/#{path}"
|
41
|
+
end
|
42
|
+
|
43
|
+
path
|
44
|
+
end
|
45
|
+
|
46
|
+
def compiler_method
|
47
|
+
type = {
|
48
|
+
amd: 'AMD',
|
49
|
+
cjs: 'CJS',
|
50
|
+
globals: 'Globals'
|
51
|
+
}[ES6ModuleTranspiler.compile_to.to_sym]
|
52
|
+
|
53
|
+
"to#{type}"
|
54
|
+
end
|
33
55
|
end
|
34
56
|
end
|
data/test/template_test.rb
CHANGED
@@ -14,10 +14,12 @@ var foo = function() {
|
|
14
14
|
export default = foo;
|
15
15
|
JS
|
16
16
|
@source.rstrip!
|
17
|
+
@scope = Scope.new('foo')
|
17
18
|
end
|
18
19
|
|
19
20
|
after do
|
20
21
|
ES6ModuleTranspiler.compile_to = nil
|
22
|
+
ES6ModuleTranspiler.prefix_pattern = []
|
21
23
|
end
|
22
24
|
|
23
25
|
it 'transpiles es6 into amd by default' do
|
@@ -36,12 +38,11 @@ JS
|
|
36
38
|
expected.rstrip!
|
37
39
|
|
38
40
|
template = Tilt::ES6ModuleTranspilerTemplate.new { @source }
|
39
|
-
|
40
|
-
template.render(scope).must_equal expected
|
41
|
+
template.render(@scope).must_equal expected
|
41
42
|
end
|
42
43
|
|
43
|
-
it 'transpiles es6 into
|
44
|
-
ES6ModuleTranspiler.compile_to = :
|
44
|
+
it 'transpiles es6 into globals when set' do
|
45
|
+
ES6ModuleTranspiler.compile_to = :globals
|
45
46
|
|
46
47
|
expected = <<-JS
|
47
48
|
(function(__exports__) {
|
@@ -56,7 +57,61 @@ JS
|
|
56
57
|
expected.rstrip!
|
57
58
|
|
58
59
|
template = Tilt::ES6ModuleTranspilerTemplate.new { @source }
|
59
|
-
|
60
|
-
|
60
|
+
template.render(@scope).must_equal expected
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'transpiles es6 into CommonJS when set' do
|
64
|
+
ES6ModuleTranspiler.compile_to = :cjs
|
65
|
+
|
66
|
+
expected = <<-JS
|
67
|
+
"use strict";
|
68
|
+
var foo = function() {
|
69
|
+
console.log('bar');
|
70
|
+
};
|
71
|
+
|
72
|
+
exports["default"] = foo;
|
73
|
+
JS
|
74
|
+
expected.rstrip!
|
75
|
+
|
76
|
+
template = Tilt::ES6ModuleTranspilerTemplate.new { @source }
|
77
|
+
template.render(@scope).must_equal expected
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'transpiles with a prefixed name matching a pattern' do
|
81
|
+
ES6ModuleTranspiler.prefix_pattern = [/^controllers/, 'app']
|
82
|
+
|
83
|
+
expected = <<-JS
|
84
|
+
define("app/controllers/foo",
|
85
|
+
["exports"],
|
86
|
+
function(__exports__) {
|
87
|
+
"use strict";
|
88
|
+
var foo = function() {
|
89
|
+
console.log('bar');
|
90
|
+
};
|
91
|
+
|
92
|
+
__exports__["default"] = foo;
|
93
|
+
});
|
94
|
+
JS
|
95
|
+
expected.rstrip!
|
96
|
+
@scope = Scope.new('controllers/foo')
|
97
|
+
template = Tilt::ES6ModuleTranspilerTemplate.new { @source }
|
98
|
+
template.render(@scope).must_equal expected
|
99
|
+
|
100
|
+
expected = <<-JS
|
101
|
+
define("foo",
|
102
|
+
["exports"],
|
103
|
+
function(__exports__) {
|
104
|
+
"use strict";
|
105
|
+
var foo = function() {
|
106
|
+
console.log('bar');
|
107
|
+
};
|
108
|
+
|
109
|
+
__exports__["default"] = foo;
|
110
|
+
});
|
111
|
+
JS
|
112
|
+
expected.rstrip!
|
113
|
+
@scope = Scope.new('foo')
|
114
|
+
template = Tilt::ES6ModuleTranspilerTemplate.new { @source }
|
115
|
+
template.render(@scope).must_equal expected
|
61
116
|
end
|
62
117
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: es6_module_transpiler-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Cardarella
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: execjs
|