sprockets-babel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5caa9d6558bf0b00b4b8be9e8c6687bcf117d7b1
4
+ data.tar.gz: cbbf5453302305e55053e7c706e35268ecd6f5d1
5
+ SHA512:
6
+ metadata.gz: a9ef0382b54be4dcd2311e018ced832fe34ee28a7f94aacb69506229d68c46ba8cf2a3f03dde34cb7bfd4fa195f4717bcbf2514e95b36d27b4a0cc263f671f70
7
+ data.tar.gz: a5f96f1348d3ddda40ae48792aa35a9fb64ff4e75caf8fe2310118d491d40e4898490b1e14a24468e14c6d2f4f510f52536cc605e31799ccef3bae48f6af84de
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ spec/rails4/log
24
+ .idea
25
+ *.iml
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brad Chen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Sprockets::Babel
2
+
3
+ This gem merges work from [Sprockets Traceur](https://github.com/gunpowderlabs/sprockets-traceur)
4
+ and [Sprockets ES6](https://github.com/josh/sprockets-es6) to create a version that uses Babel as
5
+ the ES6 transpiler in a Sprockets 2.x environment.
6
+
7
+ This is intended as a temporary solution until Sprockets 2.x can be upgraded.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.warning = true
8
+ end
data/lib/babel.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Babel
2
+ def self.options
3
+ @options ||= {}
4
+ yield @options if block_given?
5
+ @options
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Sprockets
2
+ module Babel
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,136 @@
1
+ require 'babel/transpiler'
2
+ require 'babel'
3
+ require 'sprockets'
4
+ require 'pathname'
5
+ require 'erb'
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
+ module Sprockets
81
+ module Babel
82
+ class Template < Tilt::Template
83
+ self.default_mime_type = 'application/javascript'
84
+
85
+ def self.engine_initialized?
86
+ true
87
+ end
88
+
89
+ def initialize_engine
90
+ end
91
+
92
+ def prepare
93
+ end
94
+
95
+ def evaluate(scope, locals, &block)
96
+ source_root = get_source_root(scope)
97
+ ::Babel::Transpiler.transform(data, {
98
+ modules: 'inline',
99
+ moduleIds: true,
100
+ moduleId: get_module_name(source_root),
101
+ sourceMaps: false, # due to slowness :(
102
+ filename: file,
103
+ filenameRelative: get_filename_relative(source_root)
104
+ }.merge(options.merge(::Babel.options)))['code']
105
+ end
106
+
107
+ private
108
+
109
+ def get_source_root(scope)
110
+ asset_paths = scope.environment.paths
111
+ asset_paths.each do |path|
112
+ if file.start_with?(path)
113
+ return path
114
+ end
115
+ end
116
+ ''
117
+ end
118
+
119
+ def get_filename_relative(source_root)
120
+ return '' unless source_root
121
+ Pathname.new(file).relative_path_from(Pathname.new(source_root))
122
+ end
123
+
124
+ def get_module_name(source_root)
125
+ return basename(file) unless source_root
126
+ basename(file.gsub(source_root, ''))
127
+ end
128
+
129
+ def basename(file)
130
+ file.gsub(/\.[^\/]+$/, '').gsub(/^\//, '')
131
+ end
132
+ end
133
+ end
134
+
135
+ register_engine '.es6', Babel::Template
136
+ end
@@ -0,0 +1 @@
1
+ require 'sprockets/babel'
@@ -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 'sprockets/babel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sprockets-babel'
8
+ spec.version = Sprockets::Babel::VERSION
9
+ spec.authors = ['Brad Chen']
10
+ spec.email = ['brad.chen@70ms.com']
11
+ spec.summary = %q{Sprockets 2.x plugin for Babel.}
12
+ spec.description = %q{Transpile ES6 files with Google Babel.}
13
+ #spec.homepage = "https://github.com/gunpowderlabs/sprockets-traceur"
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 'sprockets', '~> 2'
22
+ spec.add_dependency 'babel-transpiler'
23
+
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'minitest'
26
+ end
@@ -0,0 +1 @@
1
+ var square = (n) => n * n
data/test/test_es6.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'minitest/autorun'
2
+ require 'sprockets'
3
+ require 'sprockets/babel'
4
+ require 'babel'
5
+
6
+ class Environment
7
+ def paths
8
+ []
9
+ end
10
+ end
11
+
12
+ class Scope
13
+ def environment
14
+ Environment.new
15
+ end
16
+ end
17
+
18
+ class TestES6 < MiniTest::Test
19
+ def test_transform_arrow_function
20
+ es6_source = File.read('test/fixtures/math.js.es6')
21
+
22
+ Babel.options do |o|
23
+ o[:modules] = 'ignore'
24
+ o[:sourceMaps] = false
25
+ end
26
+
27
+ template = Sprockets::Babel::Template.new('math.js.es6', 1) { es6_source }
28
+ assert_equal <<-JS.chomp, template.render(Scope.new).strip
29
+ "use strict";
30
+
31
+ var square = function square(n) {
32
+ return n * n;
33
+ };
34
+ JS
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-babel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brad Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: babel-transpiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
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: minitest
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
+ description: Transpile ES6 files with Google Babel.
70
+ email:
71
+ - brad.chen@70ms.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/babel.rb
82
+ - lib/sprockets-babel.rb
83
+ - lib/sprockets/babel.rb
84
+ - lib/sprockets/babel/version.rb
85
+ - sprockets-babel.gemspec
86
+ - test/fixtures/math.js.es6
87
+ - test/test_es6.rb
88
+ homepage:
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.6
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Sprockets 2.x plugin for Babel.
112
+ test_files:
113
+ - test/fixtures/math.js.es6
114
+ - test/test_es6.rb