sprockets-babel 0.0.6.rc1 → 0.0.6.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d545a87648a7a523571ad139b3903245c303814
4
- data.tar.gz: eb3a4c49ae9f33492931a5b2053916a3282923a7
3
+ metadata.gz: 71eb44be1f8198118cf8c4067012877c9703c17c
4
+ data.tar.gz: d7f421c041a41e75c047e057ef979997f0745111
5
5
  SHA512:
6
- metadata.gz: 0c1dd528760ce57a4a6a38716add8956fad69b9a674763784f02b4ce15fd279d27a823a6ee946d7c5c6cf6f8bdf71aff036c35a2380bf20a723a995241d6f2f9
7
- data.tar.gz: 0ba51ca9ab660bfc6c1418ddf40a3f561b9d545f2eafe35348d15383402c58cc3acfd31a3606132932fb5f9e91075396595fd3d2caf0ab499bd03c5729647fd0
6
+ metadata.gz: 4c50c037ec7844b6c3bcb645bfdfee4167832cb4127c8d245b54d563db5fa93cbf2720643106e6a72b040c844f1fddd19957fc1dc5f185a897bd7c5d3a8000cb
7
+ data.tar.gz: 7d74eb5d902a9935ccf278fc26a651298a6d0cfebf6368859b52a08174c80bc83485c4ea1effde79d112b79ab5ac0b74b4d787438b674a879e47995665eb18b9
@@ -103,64 +103,60 @@ module Sprockets
103
103
  private
104
104
 
105
105
  def self.transform_inline(code, options)
106
- result = remove_use_strict(code).sub(/\Adefine\((.+?), function \(([^)]+)\) \{\n/m) do
107
- raw_import_names = Regexp.last_match[1]
108
- raw_import_vars = Regexp.last_match[2]
109
- import_names = raw_import_names.gsub(/['"\[\]]/, '').split(', ')
110
- import_vars = raw_import_vars.split(', ')
111
- import_statements = ''
112
- import_names.each_with_index { |item, i|
113
- next if i == 0
114
- next if item == 'exports' || item == 'module'
115
- import_module_id = escape_module_id(resolve_relative_module_id(options[:moduleId], item))
116
- import_statements += 'var ' + import_vars[i - 1] + ' = ' + import_module_id + ';'
106
+ prefix = <<-JS
107
+ var #{escape_module_id(options[:moduleId])} = (function() {
108
+ var exports = {};
109
+ var define = function(moduleId, importIds, body) {
110
+ var resolveRelativeModuleId = function(targetId) {
111
+ var targetParts = targetId.split(/\\//),
112
+ basename = targetParts.pop();
113
+ if ((targetParts.length == 0) || (targetParts[0] != '.' && targetParts[0] != '..')) {
114
+ return targetId;
115
+ }
116
+ if (targetParts[0] == '.') {
117
+ targetParts.shift();
118
+ }
119
+
120
+ var fromParts = moduleId.split(/\\//);
121
+ fromParts.pop();
122
+ while (targetParts.length > 0 && targetParts[0] == '..') {
123
+ targetParts.shift();
124
+ if (fromParts.length > 0) {
125
+ fromParts.pop();
126
+ }
127
+ }
128
+ var parts = fromParts.concat(targetParts);
129
+ return (parts.length == 0) ? basename : parts.join('/') + '/' + basename;
130
+ };
131
+
132
+ importIds.shift();
133
+ var imports = [exports],
134
+ module = {exports: null};
135
+ for (var i = 0; i < importIds.length; i++) {
136
+ if (importIds[i] == 'module') {
137
+ imports.push(module);
138
+ continue;
139
+ }
140
+
141
+ var importId = resolveRelativeModuleId(importIds[i]),
142
+ variable = '$__' + encodeURIComponent(importId.replace(/^\\.\\//, ''))
143
+ .replace(/%|-/, '') + '__';
144
+ imports.push((typeof window === 'undefined') ? global[variable] : window[variable]);
145
+ }
146
+ body.apply(undefined, imports);
147
+ if (module.exports != null) {
148
+ exports = module.exports;
149
+ }
150
+ };
151
+ JS
152
+ suffix = <<-JS
153
+ return exports;
154
+ })();
155
+ if ((typeof window === 'undefined') && (typeof global !== 'undefined')) {
156
+ global.#{escape_module_id(options[:moduleId])} = #{escape_module_id(options[:moduleId])};
117
157
  }
118
- "var exports = {}, module = {};\n" + import_statements
119
- end
120
-
121
- # deal with trailing stuff (such as source maps)
122
- index = find_closing_brace result
123
- stripped = result[0..index - 4] # - 4 to remove the closing });
124
- trailing_text = result[index..-1]
125
-
126
- 'var ' + escape_module_id(options[:moduleId]) + " = (function() {\n" +
127
- "'use strict';\n" +
128
- stripped + "\n" +
129
- 'return (typeof module.exports === \'undefined\') ? exports : module.exports;' +
130
- "})();\n" + trailing_text + "\n"
131
- end
132
-
133
- # Detect the closing });
134
- def self.find_closing_brace(code)
135
- curr = code.length - 1
136
- last_line_break = curr
137
- line_comment = false
138
- block_comment = false
139
- while curr > 0
140
- if code[curr] == "\n"
141
- if line_comment
142
- last_line_break = curr
143
- line_comment = false
144
- elsif !block_comment
145
- # first line without a comment found
146
- return last_line_break
147
- end
148
- end
149
-
150
- if code[curr - 1, 2] == '//'
151
- line_comment = true
152
- end
153
- if code[curr - 1, 2] == '*/'
154
- block_comment = true
155
- end
156
- if code[curr - 1, 2] == '/*'
157
- line_comment = true
158
- block_comment = false
159
- last_line_break = curr - 2
160
- end
161
- curr -= 1
162
- end
163
- last_line_break
158
+ JS
159
+ prefix + "\n" + code + "\n" + suffix
164
160
  end
165
161
 
166
162
  def self.resolve_relative_module_id(source_module_id, target_module_id)
@@ -183,10 +179,6 @@ module Sprockets
183
179
  parts.join('/') + '/' + basename
184
180
  end
185
181
 
186
- def self.remove_use_strict(code)
187
- code.gsub(/['"]use strict['"];\n/m, '')
188
- end
189
-
190
182
  def self.escape_module_id(module_id)
191
183
  '$__' + ERB::Util.url_encode(module_id.gsub(/^\.\//, '')).gsub(/%|-/, '') + '__'
192
184
  end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Babel
3
- VERSION = '0.0.6.rc1'
3
+ VERSION = '0.0.6.rc2'
4
4
  end
5
5
  end
@@ -0,0 +1,7 @@
1
+ export default class Imported1 {
2
+
3
+ static foo() {
4
+ return 'foo';
5
+ }
6
+
7
+ }
@@ -0,0 +1,7 @@
1
+ export class Imported2 {
2
+
3
+ static bar() {
4
+ return 'bar';
5
+ }
6
+
7
+ }
@@ -0,0 +1,5 @@
1
+ import Imported1 from '../lib/Imported1';
2
+ import {Imported2} from '../lib/Imported2';
3
+
4
+ print(Imported1.foo());
5
+ print(Imported2.bar());
@@ -0,0 +1,5 @@
1
+ import Imported1 from './lib/Imported1';
2
+ import {Imported2} from './lib/Imported2';
3
+
4
+ print(Imported1.foo());
5
+ print(Imported2.bar());
data/test/test_es6.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'minitest/autorun'
2
- require 'sprockets'
3
2
  require 'sprockets/babel'
4
- require 'babel'
5
3
 
6
4
  class Environment
7
5
  def paths
@@ -19,14 +17,14 @@ class TestES6 < MiniTest::Test
19
17
  def test_transform_arrow_function
20
18
  ctx = TestES6.create_js_context TestES6.compile_js_source('test/fixtures/math.js.es6')
21
19
  square = ctx[:square]
22
- assert_equal square.call(2), 4
20
+ assert_equal 4, square.call(2)
23
21
  end
24
22
 
25
23
  def test_transform_large_file
26
24
  ctx = TestES6.create_js_context TestES6.compile_js_source('test/fixtures/Regions.js.es6')
27
25
  regions = ctx[:Regions]
28
- assert_equal regions.keys.length, 103
29
- assert_equal regions['CA'][7]['name'], 'Ontario'
26
+ assert_equal 103, regions.keys.length
27
+ assert_equal 'Ontario', regions['CA'][7]['name']
30
28
  end
31
29
 
32
30
  def test_transform_empty_file
@@ -41,17 +39,45 @@ class TestES6 < MiniTest::Test
41
39
  assert !ctx['$__empty2__'].nil?
42
40
  end
43
41
 
42
+ def test_import_export
43
+ ctx = TestES6.create_test_import_context
44
+ ctx.eval TestES6.compile_js_source 'test/fixtures/test-import.js.es6', 'inline'
45
+ assert_equal 'foobar', ctx[:output]
46
+ end
47
+
48
+ def test_relative_import
49
+ ctx = TestES6.create_test_import_context
50
+ ctx.eval TestES6.compile_js_source 'test/fixtures/lib2/test-relative-import.js.es6', 'inline',
51
+ 'lib2/test-relative-import'
52
+ assert_equal 'foobar', ctx[:output]
53
+ end
54
+
55
+ private
56
+
57
+ def self.create_test_import_context
58
+ ctx = TestES6.create_js_context <<-JS
59
+ var output = '', print = function(data) { output += data};
60
+ JS
61
+ ctx.eval TestES6.compile_js_source 'test/fixtures/lib/Imported1.js.es6', 'inline',
62
+ 'lib/Imported1'
63
+ ctx.eval TestES6.compile_js_source 'test/fixtures/lib/Imported2.js.es6', 'inline',
64
+ 'lib/Imported2'
65
+ ctx
66
+ end
67
+
44
68
  def self.create_js_context(code)
45
69
  ctx = V8::Context.new
70
+ ctx.eval('var global = {};')
46
71
  ctx.eval(code)
47
72
  ctx
48
73
  end
49
74
 
50
- def self.compile_js_source(path, modules = 'ignore')
75
+ def self.compile_js_source(path, modules = 'ignore', name = '')
51
76
  es6_source = File.read(path)
52
77
  Babel.options[:modules] = modules
53
78
 
54
- template = Sprockets::Babel::Template.new(File.basename(path), 1) { es6_source }
79
+ name = name.empty? ? File.basename(path) : name
80
+ template = Sprockets::Babel::Template.new(name, 1) { es6_source }
55
81
  template.render(Scope.new)
56
82
  end
57
83
  end
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.rc1
4
+ version: 0.0.6.rc2
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-09-30 00:00:00.000000000 Z
11
+ date: 2015-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -100,7 +100,11 @@ files:
100
100
  - test/fixtures/Regions.js.es6
101
101
  - test/fixtures/empty.js.es6
102
102
  - test/fixtures/empty2.js.es6
103
+ - test/fixtures/lib/Imported1.js.es6
104
+ - test/fixtures/lib/Imported2.js.es6
105
+ - test/fixtures/lib2/test-relative-import.js.es6
103
106
  - test/fixtures/math.js.es6
107
+ - test/fixtures/test-import.js.es6
104
108
  - test/test_es6.rb
105
109
  homepage: https://github.com/70mainstreet/sprockets-babel
106
110
  licenses:
@@ -130,5 +134,9 @@ test_files:
130
134
  - test/fixtures/Regions.js.es6
131
135
  - test/fixtures/empty.js.es6
132
136
  - test/fixtures/empty2.js.es6
137
+ - test/fixtures/lib/Imported1.js.es6
138
+ - test/fixtures/lib/Imported2.js.es6
139
+ - test/fixtures/lib2/test-relative-import.js.es6
133
140
  - test/fixtures/math.js.es6
141
+ - test/fixtures/test-import.js.es6
134
142
  - test/test_es6.rb