sprockets-babel 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +39 -1
- data/lib/sprockets/babel.rb +75 -75
- data/lib/sprockets/babel/version.rb +1 -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: 95e200196bcc77161ad962be61ea750c29e5a843
|
4
|
+
data.tar.gz: d4f8c86320439d40d1ab567aee5a493e922358f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01c858ce6d9e7336e3841de03f47524b1baf2b9a43f3ffafcd559d761f9fff4823a7dba44bddf61552f44b8c06cd3a3c588bc50a3daa42ef40d8d6f854377077
|
7
|
+
data.tar.gz: 6ac922edba6d118268bb1ba4b2e4c99211bb1c45bd10aa910ef9871331aabe55958de41eb75767e235306db92302326c30084ea26b39e67083b7e303aaec6ad5
|
data/README.md
CHANGED
@@ -4,4 +4,42 @@ This gem merges work from [Sprockets Traceur](https://github.com/gunpowderlabs/s
|
|
4
4
|
and [Sprockets ES6](https://github.com/josh/sprockets-es6) to create a version that uses Babel as
|
5
5
|
the ES6 transpiler in a Sprockets 2.x environment.
|
6
6
|
|
7
|
-
This is intended as a temporary solution until Sprockets 2.x can be upgraded.
|
7
|
+
This is intended as a temporary solution until Sprockets 2.x can be upgraded.
|
8
|
+
|
9
|
+
## Background
|
10
|
+
|
11
|
+
I created this gem for the following reasons:
|
12
|
+
|
13
|
+
* I need Babel for IE 8 support, but
|
14
|
+
[Middleman](https://github.com/middleman/middleman-sprockets/issues/77)
|
15
|
+
does not yet support Sprockets 3, which [sprockets-es6](https://github.com/josh/sprockets-es6)
|
16
|
+
uses.
|
17
|
+
* Babel does not support [inline module formatting](https://github.com/babel/babel/issues/495),
|
18
|
+
which I need and therefore implement myself in this gem using some string replacement hacks.
|
19
|
+
|
20
|
+
## Use
|
21
|
+
|
22
|
+
This gem is meant to be a drop-in replacement for
|
23
|
+
[Sprockets Traceur](https://github.com/gunpowderlabs/sprockets-traceur). So, just include it in your
|
24
|
+
`Gemfile` and profit.
|
25
|
+
|
26
|
+
## Configuration
|
27
|
+
|
28
|
+
```
|
29
|
+
require 'babel'
|
30
|
+
::Babel.options do |o|
|
31
|
+
o[:optionKey] = 'option value'
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
See [Babel Options](https://babeljs.io/docs/usage/options/) for a list of available options.
|
36
|
+
|
37
|
+
## Demonstration
|
38
|
+
|
39
|
+
See the [sprockets-babel-demo](https://github.com/70mainstreet/sprockets-babel-demo) project for a
|
40
|
+
demonstration of how this gem can be used in a Middleman project.
|
41
|
+
|
42
|
+
## Caveats
|
43
|
+
|
44
|
+
Because the inline module formatter is done using some string replacement hacks, generated source
|
45
|
+
maps do not appear to work.
|
data/lib/sprockets/babel.rb
CHANGED
@@ -4,79 +4,6 @@ require 'sprockets'
|
|
4
4
|
require 'pathname'
|
5
5
|
require 'erb'
|
6
6
|
|
7
|
-
# Monkey patch babel-transpiler to support inline module formatting
|
8
|
-
module ::Babel::Transpiler
|
9
|
-
def self.transform(code, options = {})
|
10
|
-
modules = options[:modules] || 'inline'
|
11
|
-
result = context.call('babel.transform', code, options.merge('ast' => false,
|
12
|
-
'modules' => modules == 'inline' ? 'amd' : modules))
|
13
|
-
if modules == 'inline'
|
14
|
-
result['code'] = transform_inline(result['code'], options)
|
15
|
-
end
|
16
|
-
result
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.transform_inline(code, options)
|
20
|
-
result = remove_use_strict(code).gsub(/\Adefine\((.+?), function \(([^)]+)\) \{\n/m) do
|
21
|
-
raw_import_names = Regexp.last_match[1]
|
22
|
-
raw_import_vars = Regexp.last_match[2]
|
23
|
-
import_names = raw_import_names.gsub(/['"\[\]]/, '').split(', ')
|
24
|
-
import_vars = raw_import_vars.split(', ')
|
25
|
-
import_statements = ''
|
26
|
-
import_names.each_with_index { |item, i|
|
27
|
-
next if i == 0
|
28
|
-
next if item == 'exports' || item == 'module'
|
29
|
-
import_module_id = escape_module_id(resolve_relative_module_id(options[:moduleId], item))
|
30
|
-
import_statements += 'var ' + import_vars[i - 1] + ' = ' + import_module_id + ';'
|
31
|
-
}
|
32
|
-
"var exports = {}, module = {};\n" + import_statements
|
33
|
-
end
|
34
|
-
|
35
|
-
# deal with trailing stuff (such as source maps)
|
36
|
-
index = result.rindex(/\}\);/)
|
37
|
-
stripped = result
|
38
|
-
trailing_text = ''
|
39
|
-
unless index.nil?
|
40
|
-
stripped = result[0..index - 1]
|
41
|
-
trailing_text = result[(index + 3)..-1]
|
42
|
-
end
|
43
|
-
|
44
|
-
'var ' + escape_module_id(options[:moduleId]) + " = (function() {\n" +
|
45
|
-
"'use strict';\n" +
|
46
|
-
stripped + "\n" +
|
47
|
-
'return (typeof module.exports === \'undefined\') ? exports : module.exports;' +
|
48
|
-
"})();\n" + trailing_text
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.resolve_relative_module_id(source_module_id, target_module_id)
|
52
|
-
target_path_parts = target_module_id.split(/\//)
|
53
|
-
basename = target_path_parts.pop
|
54
|
-
return target_module_id if target_path_parts.length == 0
|
55
|
-
return target_module_id if target_path_parts[0] != '.' && target_path_parts[0] != '..'
|
56
|
-
target_path_parts.shift if target_path_parts[0] == '.'
|
57
|
-
|
58
|
-
source_path_parts = source_module_id.split(/\//)
|
59
|
-
source_path_parts.pop
|
60
|
-
while target_path_parts.length > 0 && target_path_parts[0] == '..'
|
61
|
-
target_path_parts.pop
|
62
|
-
source_path_parts.pop if source_path_parts.length > 0
|
63
|
-
end
|
64
|
-
parts = source_path_parts.concat(target_path_parts)
|
65
|
-
if parts.length == 0
|
66
|
-
return basename
|
67
|
-
end
|
68
|
-
parts.join('/') + '/' + basename
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.remove_use_strict(code)
|
72
|
-
code.gsub(/['"]use strict['"];\n/m, '')
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.escape_module_id(module_id)
|
76
|
-
'$__' + ERB::Util.url_encode(module_id.gsub(/^\.\//, '')).gsub(/%/, '') + '__'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
7
|
module Sprockets
|
81
8
|
module Babel
|
82
9
|
class Template < Tilt::Template
|
@@ -94,11 +21,10 @@ module Sprockets
|
|
94
21
|
|
95
22
|
def evaluate(scope, locals, &block)
|
96
23
|
source_root = get_source_root(scope)
|
97
|
-
|
24
|
+
Babel.transform(data, {
|
98
25
|
modules: 'inline',
|
99
26
|
moduleIds: true,
|
100
27
|
moduleId: get_module_name(source_root),
|
101
|
-
sourceMaps: false, # due to slowness :(
|
102
28
|
filename: file,
|
103
29
|
filenameRelative: get_filename_relative(source_root)
|
104
30
|
}.merge(options.merge(::Babel.options)))['code']
|
@@ -130,6 +56,80 @@ module Sprockets
|
|
130
56
|
file.gsub(/\.[^\/]+$/, '').gsub(/^\//, '')
|
131
57
|
end
|
132
58
|
end
|
59
|
+
|
60
|
+
def self.transform(code, options = {})
|
61
|
+
modules = options[:modules] || 'inline'
|
62
|
+
result = ::Babel::Transpiler.context.call('babel.transform', code, options.merge(
|
63
|
+
'ast' => false,
|
64
|
+
'modules' => modules == 'inline' ? 'amd' : modules
|
65
|
+
))
|
66
|
+
if modules == 'inline'
|
67
|
+
result['code'] = transform_inline(result['code'], options)
|
68
|
+
end
|
69
|
+
result
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def self.transform_inline(code, options)
|
75
|
+
result = remove_use_strict(code).gsub(/\Adefine\((.+?), function \(([^)]+)\) \{\n/m) do
|
76
|
+
raw_import_names = Regexp.last_match[1]
|
77
|
+
raw_import_vars = Regexp.last_match[2]
|
78
|
+
import_names = raw_import_names.gsub(/['"\[\]]/, '').split(', ')
|
79
|
+
import_vars = raw_import_vars.split(', ')
|
80
|
+
import_statements = ''
|
81
|
+
import_names.each_with_index { |item, i|
|
82
|
+
next if i == 0
|
83
|
+
next if item == 'exports' || item == 'module'
|
84
|
+
import_module_id = escape_module_id(resolve_relative_module_id(options[:moduleId], item))
|
85
|
+
import_statements += 'var ' + import_vars[i - 1] + ' = ' + import_module_id + ';'
|
86
|
+
}
|
87
|
+
"var exports = {}, module = {};\n" + import_statements
|
88
|
+
end
|
89
|
+
|
90
|
+
# deal with trailing stuff (such as source maps)
|
91
|
+
index = result.rindex(/\}\);/)
|
92
|
+
stripped = result
|
93
|
+
trailing_text = ''
|
94
|
+
unless index.nil?
|
95
|
+
stripped = result[0..index - 1]
|
96
|
+
trailing_text = result[(index + 3)..-1]
|
97
|
+
end
|
98
|
+
|
99
|
+
'var ' + escape_module_id(options[:moduleId]) + " = (function() {\n" +
|
100
|
+
"'use strict';\n" +
|
101
|
+
stripped + "\n" +
|
102
|
+
'return (typeof module.exports === \'undefined\') ? exports : module.exports;' +
|
103
|
+
"})();\n" + trailing_text
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.resolve_relative_module_id(source_module_id, target_module_id)
|
107
|
+
target_path_parts = target_module_id.split(/\//)
|
108
|
+
basename = target_path_parts.pop
|
109
|
+
return target_module_id if target_path_parts.length == 0
|
110
|
+
return target_module_id if target_path_parts[0] != '.' && target_path_parts[0] != '..'
|
111
|
+
target_path_parts.shift if target_path_parts[0] == '.'
|
112
|
+
|
113
|
+
source_path_parts = source_module_id.split(/\//)
|
114
|
+
source_path_parts.pop
|
115
|
+
while target_path_parts.length > 0 && target_path_parts[0] == '..'
|
116
|
+
target_path_parts.pop
|
117
|
+
source_path_parts.pop if source_path_parts.length > 0
|
118
|
+
end
|
119
|
+
parts = source_path_parts.concat(target_path_parts)
|
120
|
+
if parts.length == 0
|
121
|
+
return basename
|
122
|
+
end
|
123
|
+
parts.join('/') + '/' + basename
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.remove_use_strict(code)
|
127
|
+
code.gsub(/['"]use strict['"];\n/m, '')
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.escape_module_id(module_id)
|
131
|
+
'$__' + ERB::Util.url_encode(module_id.gsub(/^\.\//, '')).gsub(/%/, '') + '__'
|
132
|
+
end
|
133
133
|
end
|
134
134
|
|
135
135
|
register_engine '.es6', Babel::Template
|
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.
|
4
|
+
version: 0.0.2
|
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-04-
|
11
|
+
date: 2015-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sprockets
|