Full Code of TannerRogalsky/sprockets-es6 for AI

master d4b6d3d4ccee cached
15 files
10.6 KB
3.3k tokens
30 symbols
1 requests
Download .txt
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
Download .txt
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
Download .txt
SYMBOL INDEX (30 symbols across 3 files)

FILE: lib/sprockets/es6.rb
  type Sprockets (line 6) | module Sprockets
    class ES6 (line 7) | class ES6
      method configuration_hash (line 13) | def configuration_hash
      method instance (line 20) | def instance
      method configure (line 24) | def configure
      method reset_configuration (line 29) | def reset_configuration
      method call (line 33) | def call(input)
      method cache_key (line 37) | def cache_key
      method configuration_hash (line 45) | def configuration_hash
      method initialize (line 49) | def initialize(options = {})
      method call (line 61) | def call(input)
      method transform (line 69) | def transform(data, opts)
      method transformation_options (line 73) | def transformation_options(input)

FILE: lib/sprockets/es6/version.rb
  type Sprockets (line 1) | module Sprockets
    class ES6 (line 2) | class ES6

FILE: test/test_es6.rb
  class TestES6 (line 5) | class TestES6 < MiniTest::Test
    method setup (line 6) | def setup
    method test_require_asset (line 11) | def test_require_asset
    method test_transform_arrow_function (line 23) | def test_transform_arrow_function
    method test_common_modules (line 35) | def test_common_modules
    method test_amd_modules (line 46) | def test_amd_modules
    method test_amd_modules_with_ids (line 57) | def test_amd_modules_with_ids
    method test_amd_modules_with_ids_and_root (line 68) | def test_amd_modules_with_ids_and_root
    method test_system_modules (line 79) | def test_system_modules
    method test_system_modules_with_ids (line 95) | def test_system_modules_with_ids
    method test_system_modules_with_ids_and_root (line 111) | def test_system_modules_with_ids_and_root
    method test_caching_takes_filename_into_account (line 127) | def test_caching_takes_filename_into_account
    method test_class_level_transformation_configuration (line 153) | def test_class_level_transformation_configuration
    method test_cache_key (line 173) | def test_cache_key
    method register (line 184) | def register(processor)
Condensed preview — 15 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (12K chars).
[
  {
    "path": ".gitignore",
    "chars": 13,
    "preview": "Gemfile.lock\n"
  },
  {
    "path": ".travis.yml",
    "chars": 51,
    "preview": "language: ruby\nrvm:\n  - 2.3.1\ngemfile:\n  - Gemfile\n"
  },
  {
    "path": "Gemfile",
    "chars": 38,
    "preview": "source 'https://rubygems.org'\ngemspec\n"
  },
  {
    "path": "LICENSE",
    "chars": 1055,
    "preview": "Copyright (c) 2014 Joshua Peek\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this sof"
  },
  {
    "path": "README.md",
    "chars": 1109,
    "preview": "# Sprockets ES6\n\n**Experimental**\n\nA Sprockets transformer that converts ES6 code into vanilla ES5 with [Babel JS](https"
  },
  {
    "path": "Rakefile",
    "chars": 117,
    "preview": "require 'rake/testtask'\n\ntask :default => :test\n\nRake::TestTask.new do |t|\n  t.libs << 'test'\n  t.warning = true\nend\n"
  },
  {
    "path": "lib/sprockets/es6/version.rb",
    "chars": 61,
    "preview": "module Sprockets\n  class ES6\n    VERSION = '0.9.2'\n  end\nend\n"
  },
  {
    "path": "lib/sprockets/es6.rb",
    "chars": 2451,
    "preview": "require 'babel/transpiler'\nrequire 'sprockets'\nrequire 'sprockets/es6/version'\nrequire 'ostruct'\n\nmodule Sprockets\n  cla"
  },
  {
    "path": "sprockets-es6.gemspec",
    "chars": 820,
    "preview": "$:.unshift File.expand_path(\"../lib\", __FILE__)\nrequire 'sprockets/es6/version'\n\nGem::Specification.new do |s|\n  s.name "
  },
  {
    "path": "test/fixtures/file.js.es6",
    "chars": 26,
    "preview": "var square = (n) => n * n\n"
  },
  {
    "path": "test/fixtures/math.es6",
    "chars": 26,
    "preview": "var square = (n) => n * n\n"
  },
  {
    "path": "test/fixtures/mod.es6.erb",
    "chars": 14,
    "preview": "import \"foo\";\n"
  },
  {
    "path": "test/fixtures/mod2.es6",
    "chars": 14,
    "preview": "import \"foo\";\n"
  },
  {
    "path": "test/fixtures/require_asset.js.erb",
    "chars": 34,
    "preview": "<% require_asset \"file.js.es6\" %>\n"
  },
  {
    "path": "test/test_es6.rb",
    "chars": 4986,
    "preview": "require 'minitest/autorun'\nrequire 'sprockets'\nrequire 'sprockets/es6'\n\nclass TestES6 < MiniTest::Test\n  def setup\n    @"
  }
]

About this extraction

This page contains the full source code of the TannerRogalsky/sprockets-es6 GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 15 files (10.6 KB), approximately 3.3k tokens, and a symbol index with 30 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!