sprockets-babel 0.0.6.rc2 → 0.0.6.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sprockets/babel/version.rb +1 -1
- data/lib/sprockets/babel.rb +12 -13
- data/lib/sprockets-babel-inline-module-loader.js +48 -0
- data/test/test_es6.rb +25 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 990ba9c41d55e0fc6b18270c576cd591326394e8
|
4
|
+
data.tar.gz: f47bbc5918f52f160d0931382c742a6394facb27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0114201e425837dd7c3eae621b63b9fc0062c91c0dd35754847d5536f82d002b40e5c10e9eefe67e65d5bf27621f2150b9432a456d326d1c6ed91aeb8be7e34b
|
7
|
+
data.tar.gz: 0aa7a83f1857ce3dade829f766cbf9a08cf853dadcd7155fdcd028a6a5e69210539e7415ab410919cae9c53ee6f4a9c5cc377166aa6bba43a5223ee7630d5c57
|
data/lib/sprockets/babel.rb
CHANGED
@@ -103,9 +103,13 @@ module Sprockets
|
|
103
103
|
private
|
104
104
|
|
105
105
|
def self.transform_inline(code, options)
|
106
|
+
module_var = '$__' + ERB::Util.url_encode(options[:moduleId].gsub(/^\.\//, ''))
|
107
|
+
.gsub(/%|-/, '') + '__'
|
106
108
|
prefix = <<-JS
|
107
|
-
|
109
|
+
(function(global) {
|
108
110
|
var exports = {};
|
111
|
+
global.#{module_var} = exports;
|
112
|
+
|
109
113
|
var define = function(moduleId, importIds, body) {
|
110
114
|
var resolveRelativeModuleId = function(targetId) {
|
111
115
|
var targetParts = targetId.split(/\\//),
|
@@ -141,20 +145,19 @@ module Sprockets
|
|
141
145
|
var importId = resolveRelativeModuleId(importIds[i]),
|
142
146
|
variable = '$__' + encodeURIComponent(importId.replace(/^\\.\\//, ''))
|
143
147
|
.replace(/%|-/, '') + '__';
|
144
|
-
imports.push(
|
148
|
+
imports.push(global[variable]);
|
145
149
|
}
|
146
|
-
body.apply(
|
150
|
+
body.apply(global, imports);
|
147
151
|
if (module.exports != null) {
|
148
|
-
|
152
|
+
global.#{module_var} = module.exports;
|
149
153
|
}
|
150
154
|
};
|
151
155
|
JS
|
156
|
+
|
157
|
+
# code transpiled by babel is inserted here
|
158
|
+
|
152
159
|
suffix = <<-JS
|
153
|
-
|
154
|
-
})();
|
155
|
-
if ((typeof window === 'undefined') && (typeof global !== 'undefined')) {
|
156
|
-
global.#{escape_module_id(options[:moduleId])} = #{escape_module_id(options[:moduleId])};
|
157
|
-
}
|
160
|
+
})(this);
|
158
161
|
JS
|
159
162
|
prefix + "\n" + code + "\n" + suffix
|
160
163
|
end
|
@@ -178,10 +181,6 @@ module Sprockets
|
|
178
181
|
end
|
179
182
|
parts.join('/') + '/' + basename
|
180
183
|
end
|
181
|
-
|
182
|
-
def self.escape_module_id(module_id)
|
183
|
-
'$__' + ERB::Util.url_encode(module_id.gsub(/^\.\//, '')).gsub(/%|-/, '') + '__'
|
184
|
-
end
|
185
184
|
end
|
186
185
|
|
187
186
|
register_engine '.es6', Babel::Template
|
@@ -0,0 +1,48 @@
|
|
1
|
+
(function(global) {
|
2
|
+
global.define = function(moduleId, importIds, body) {
|
3
|
+
var resolveRelativeModuleId = function(targetId) {
|
4
|
+
var targetParts = targetId.split(/\//),
|
5
|
+
basename = targetParts.pop();
|
6
|
+
if ((targetParts.length == 0) || (targetParts[0] != '.' && targetParts[0] != '..')) {
|
7
|
+
return targetId;
|
8
|
+
}
|
9
|
+
if (targetParts[0] == '.') {
|
10
|
+
targetParts.shift();
|
11
|
+
}
|
12
|
+
|
13
|
+
var fromParts = moduleId.split(/\//);
|
14
|
+
fromParts.pop();
|
15
|
+
while (targetParts.length > 0 && targetParts[0] == '..') {
|
16
|
+
targetParts.shift();
|
17
|
+
if (fromParts.length > 0) {
|
18
|
+
fromParts.pop();
|
19
|
+
}
|
20
|
+
}
|
21
|
+
var parts = fromParts.concat(targetParts);
|
22
|
+
return (parts.length == 0) ? basename : parts.join('/') + '/' + basename;
|
23
|
+
};
|
24
|
+
var toVariableName = function(moduleId) {
|
25
|
+
return '$__' + encodeURIComponent(moduleId.replace(/^\.\//, ''))
|
26
|
+
.replace(/%|-/, '') + '__';
|
27
|
+
};
|
28
|
+
|
29
|
+
importIds.shift();
|
30
|
+
var exports = {},
|
31
|
+
imports = [exports],
|
32
|
+
module = {exports: null};
|
33
|
+
for (var i = 0; i < importIds.length; i++) {
|
34
|
+
if (importIds[i] == 'module') {
|
35
|
+
imports.push(module);
|
36
|
+
continue;
|
37
|
+
}
|
38
|
+
|
39
|
+
var varName = toVariableName(resolveRelativeModuleId(importIds[i]));
|
40
|
+
imports.push(global[varName]);
|
41
|
+
}
|
42
|
+
body.apply(global, imports);
|
43
|
+
if (module.exports != null) {
|
44
|
+
exports = module.exports;
|
45
|
+
}
|
46
|
+
global[toVariableName(moduleId)] = exports;
|
47
|
+
};
|
48
|
+
})(this);
|
data/test/test_es6.rb
CHANGED
@@ -52,23 +52,41 @@ class TestES6 < MiniTest::Test
|
|
52
52
|
assert_equal 'foobar', ctx[:output]
|
53
53
|
end
|
54
54
|
|
55
|
+
def test_js_inline_module_loader
|
56
|
+
ctx = TestES6.create_inline_module_loader_context
|
57
|
+
ctx.eval TestES6.compile_js_source 'test/fixtures/test-import.js.es6', 'inline'
|
58
|
+
assert_equal 'foobar', ctx[:output]
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_js_inline_module_loader_relative_import
|
62
|
+
ctx = TestES6.create_inline_module_loader_context
|
63
|
+
ctx.eval TestES6.compile_js_source 'test/fixtures/lib2/test-relative-import.js.es6', 'inline',
|
64
|
+
'lib2/test-relative-import'
|
65
|
+
assert_equal 'foobar', ctx[:output]
|
66
|
+
end
|
67
|
+
|
55
68
|
private
|
56
69
|
|
57
|
-
def self.
|
58
|
-
|
70
|
+
def self.create_inline_module_loader_context
|
71
|
+
inline_module_loader = File.read('lib/sprockets-babel-inline-module-loader.js')
|
72
|
+
TestES6.create_test_import_context inline_module_loader, 'amd'
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.create_test_import_context(code = nil, module_format = 'inline')
|
76
|
+
ctx = TestES6.create_js_context code
|
77
|
+
ctx.eval <<-JS
|
59
78
|
var output = '', print = function(data) { output += data};
|
60
79
|
JS
|
61
|
-
ctx.eval TestES6.compile_js_source 'test/fixtures/lib/Imported1.js.es6',
|
80
|
+
ctx.eval TestES6.compile_js_source 'test/fixtures/lib/Imported1.js.es6', module_format,
|
62
81
|
'lib/Imported1'
|
63
|
-
ctx.eval TestES6.compile_js_source 'test/fixtures/lib/Imported2.js.es6',
|
82
|
+
ctx.eval TestES6.compile_js_source 'test/fixtures/lib/Imported2.js.es6', module_format,
|
64
83
|
'lib/Imported2'
|
65
84
|
ctx
|
66
85
|
end
|
67
86
|
|
68
|
-
def self.create_js_context(code)
|
87
|
+
def self.create_js_context(code = nil)
|
69
88
|
ctx = V8::Context.new
|
70
|
-
ctx.eval(
|
71
|
-
ctx.eval(code)
|
89
|
+
ctx.eval(code) unless code.nil?
|
72
90
|
ctx
|
73
91
|
end
|
74
92
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-babel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.6.
|
4
|
+
version: 0.0.6.rc3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brad Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sprockets
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- README.md
|
94
94
|
- Rakefile
|
95
95
|
- lib/babel.rb
|
96
|
+
- lib/sprockets-babel-inline-module-loader.js
|
96
97
|
- lib/sprockets-babel.rb
|
97
98
|
- lib/sprockets/babel.rb
|
98
99
|
- lib/sprockets/babel/version.rb
|