Repository: TannerRogalsky/sprockets-es6 Branch: master Commit: d4b6d3d4ccee Files: 15 Total size: 10.6 KB Directory structure: gitextract_p8_a_e52/ ├── .gitignore ├── .travis.yml ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── lib/ │ └── sprockets/ │ ├── es6/ │ │ └── version.rb │ └── es6.rb ├── sprockets-es6.gemspec └── test/ ├── fixtures/ │ ├── file.js.es6 │ ├── math.es6 │ ├── mod.es6.erb │ ├── mod2.es6 │ └── require_asset.js.erb └── test_es6.rb ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ Gemfile.lock ================================================ FILE: .travis.yml ================================================ language: ruby rvm: - 2.3.1 gemfile: - Gemfile ================================================ FILE: Gemfile ================================================ source 'https://rubygems.org' gemspec ================================================ FILE: LICENSE ================================================ Copyright (c) 2014 Joshua Peek Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Sprockets ES6 **Experimental** A Sprockets transformer that converts ES6 code into vanilla ES5 with [Babel JS](https://babeljs.io). ## Usage ``` ruby # Gemfile gem 'sprockets', '>= 3.0.0' gem 'sprockets-es6' ``` ``` ruby # application.rb # [...] require "action_view/railtie" require "sprockets/railtie" # require "rails/test_unit/railtie" require 'sprockets/es6' # [...] ``` ``` js // app.es6 let square = (x) => x * x class Animal { constructor(name) { this.name = name } } ``` ## Releases This plugin is primarily experimental and will never reach a stable 1.0. The purpose is to test out BabelJS features on Sprockets 3.x and include it by default in Sprockets 4.x. ## Asset manifests required for precompiling `.es6` won't work directly with `config.assets.precompile = %w( foo.es6 )` for annoying compatibility reasons with Sprockets 2.x. Besides, you should look into moving away from `config.assets.precompile` and using manifests instead. See [Sprockets 3.x UPGRADING guide](https://github.com/rails/sprockets/blob/master/UPGRADING.md#preference-for-asset-manifest-and-links). ================================================ FILE: Rakefile ================================================ require 'rake/testtask' task :default => :test Rake::TestTask.new do |t| t.libs << 'test' t.warning = true end ================================================ FILE: lib/sprockets/es6/version.rb ================================================ module Sprockets class ES6 VERSION = '0.9.2' end end ================================================ FILE: lib/sprockets/es6.rb ================================================ require 'babel/transpiler' require 'sprockets' require 'sprockets/es6/version' require 'ostruct' module Sprockets class ES6 class << self attr_accessor :configuration def configuration_hash configuration.to_h.reduce({}) do |hash, (key, val)| hash[key.to_s] = val hash end end def instance @instance ||= new end def configure self.configuration ||= OpenStruct.new yield configuration end def reset_configuration self.configuration = OpenStruct.new end def call(input) instance.call(input) end def cache_key instance.cache_key end end attr_reader :cache_key def configuration_hash self.class.configuration_hash end def initialize(options = {}) @options = configuration_hash.merge(options).freeze @cache_key = [ self.class.name, Babel::Transpiler.version, Babel::Transpiler.source_version, VERSION, @options ].freeze end def call(input) data = input[:data] result = input[:cache].fetch(@cache_key + [input[:filename]] + [data]) do transform(data, transformation_options(input)) end result['code'] end def transform(data, opts) Babel::Transpiler.transform(data, opts) end def transformation_options(input) opts = { 'sourceRoot' => input[:load_path], 'moduleRoot' => nil, 'filename' => input[:filename], 'filenameRelative' => input[:environment].split_subpath(input[:load_path], input[:filename]) }.merge(@options) if opts['moduleIds'] && opts['moduleRoot'] opts['moduleId'] ||= File.join(opts['moduleRoot'], input[:name]) elsif opts['moduleIds'] opts['moduleId'] ||= input[:name] end opts end end append_path Babel::Transpiler.source_path if respond_to?(:register_transformer) register_mime_type 'text/ecmascript-6', extensions: ['.es6'], charset: :unicode register_transformer 'text/ecmascript-6', 'application/javascript', ES6 register_preprocessor 'text/ecmascript-6', DirectiveProcessor end if respond_to?(:register_engine) args = ['.es6', ES6] args << { mime_type: 'text/ecmascript-6', silence_deprecation: true } if Sprockets::VERSION.start_with?("3") register_engine(*args) end end ================================================ FILE: sprockets-es6.gemspec ================================================ $:.unshift File.expand_path("../lib", __FILE__) require 'sprockets/es6/version' Gem::Specification.new do |s| s.name = 'sprockets-es6' s.version = Sprockets::ES6::VERSION s.homepage = "https://github.com/TannerRogalsky/sprockets-es6" s.summary = "Sprockets ES6 transformer" s.description = <<-EOS A Sprockets transformer that converts ES6 code into vanilla ES5 with Babel JS. EOS s.license = "MIT" s.files = [ 'lib/sprockets/es6.rb', 'lib/sprockets/es6/version.rb', 'LICENSE', 'README.md' ] s.add_dependency 'babel-transpiler' s.add_dependency 'babel-source', '>= 5.8.11' s.add_dependency 'sprockets', '>= 3.0.0' s.add_development_dependency 'rake' s.add_development_dependency 'minitest' s.authors = ['Joshua Peek'] s.email = 'josh@joshpeek.com' end ================================================ FILE: test/fixtures/file.js.es6 ================================================ var square = (n) => n * n ================================================ FILE: test/fixtures/math.es6 ================================================ var square = (n) => n * n ================================================ FILE: test/fixtures/mod.es6.erb ================================================ import "foo"; ================================================ FILE: test/fixtures/mod2.es6 ================================================ import "foo"; ================================================ FILE: test/fixtures/require_asset.js.erb ================================================ <% require_asset "file.js.es6" %> ================================================ FILE: test/test_es6.rb ================================================ require 'minitest/autorun' require 'sprockets' require 'sprockets/es6' class TestES6 < MiniTest::Test def setup @env = Sprockets::Environment.new @env.append_path File.expand_path("../fixtures", __FILE__) end def test_require_asset assert asset = @env["require_asset"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip "use strict"; var square = function square(n) { return n * n; }; JS end def test_transform_arrow_function assert asset = @env["math.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip "use strict"; var square = function square(n) { return n * n; }; JS end def test_common_modules register Sprockets::ES6.new('modules' => 'common') assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip "use strict"; require("foo"); JS end def test_amd_modules register Sprockets::ES6.new('modules' => 'amd') assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip define(["exports", "foo"], function (exports, _foo) { "use strict"; }); JS end def test_amd_modules_with_ids register Sprockets::ES6.new('modules' => 'amd', 'moduleIds' => true) assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip define("mod", ["exports", "foo"], function (exports, _foo) { "use strict"; }); JS end def test_amd_modules_with_ids_and_root register Sprockets::ES6.new('modules' => 'amd', 'moduleIds' => true, 'moduleRoot' => 'root') assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip define("root/mod", ["exports", "foo"], function (exports, _foo) { "use strict"; }); JS end def test_system_modules register Sprockets::ES6.new('modules' => 'system') assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip System.register(["foo"], function (_export) { "use strict"; return { setters: [function (_foo) {}], execute: function () {} }; }); JS end def test_system_modules_with_ids register Sprockets::ES6.new('modules' => 'system', 'moduleIds' => true) assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip System.register("mod", ["foo"], function (_export) { "use strict"; return { setters: [function (_foo) {}], execute: function () {} }; }); JS end def test_system_modules_with_ids_and_root register Sprockets::ES6.new('modules' => 'system', 'moduleIds' => true, 'moduleRoot' => 'root') assert asset = @env["mod.js"] assert_equal 'application/javascript', asset.content_type assert_equal <<-JS.chomp, asset.to_s.strip System.register("root/mod", ["foo"], function (_export) { "use strict"; return { setters: [function (_foo) {}], execute: function () {} }; }); JS end def test_caching_takes_filename_into_account register Sprockets::ES6.new('modules' => 'system', 'moduleIds' => true, 'moduleRoot' => 'root') mod1 = @env["mod.js"] mod2 = @env["mod2.js"] assert_equal <<-JS.chomp, mod1.to_s.strip System.register("root/mod", ["foo"], function (_export) { "use strict"; return { setters: [function (_foo) {}], execute: function () {} }; }); JS assert_equal <<-JS.chomp, mod2.to_s.strip System.register("root/mod2", ["foo"], function (_export) { "use strict"; return { setters: [function (_foo) {}], execute: function () {} }; }); JS end def test_class_level_transformation_configuration Sprockets::ES6.configure do |config| config.modules = 'amd' config.moduleIds = true end processor = Sprockets::ES6.new mock_env = OpenStruct.new def mock_env.split_subpath(*args) nil end transformation_options = processor.transformation_options( :environment => mock_env ) assert_equal transformation_options['modules'], 'amd' assert transformation_options['moduleIds'] Sprockets::ES6.reset_configuration end def test_cache_key assert Sprockets::ES6.cache_key amd_processor_1 = Sprockets::ES6.new('modules' => 'amd') amd_processor_2 = Sprockets::ES6.new('modules' => 'amd') assert_equal amd_processor_1.cache_key, amd_processor_2.cache_key system_processor = Sprockets::ES6.new('modules' => 'system') refute_equal amd_processor_1.cache_key, system_processor.cache_key end def register(processor) @env.register_transformer 'text/ecmascript-6', 'application/javascript', processor end end