require '6to5'
require 'sprockets'
require 'sprockets/es6/version'

module Sprockets
  class ES6
    def self.instance
      @instance ||= new
    end

    def self.call(input)
      instance.call(input)
    end

    def initialize(options = {})
      @options = options.dup.freeze

      @cache_key = [
        self.class.name,
        ES6to5.version,
        VERSION,
        @options
      ].freeze
    end

    def call(input)
      data = input[:data]
      result = input[:cache].fetch(@cache_key + [data]) do
        ES6to5.transform(data, @options)
      end
      result['code']
    end
  end

  append_path ES6to5::Source.root
  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

require '6to5'
require 'sprockets'
require 'sprockets/es6/version'

module Sprockets
  class ES6
    def self.instance
      @instance ||= new
    end

    def self.call(input)
      instance.call(input)
    end

    def initialize(options = {})
      @options = options.dup.freeze

      @cache_key = [
        self.class.name,
        ES6to5.version,
        VERSION,
        @options
      ].freeze
    end

    def call(input)
      data = input[:data]
      result = input[:cache].fetch(@cache_key + [data]) do
        ES6to5.transform(data, @options.merge(
          'filename' => input[:filename],
          'filenameRelative' => input[:name] + '.js',
        ))
      end
      result['code']
    end
  end

  append_path ES6to5::Source.root
  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

Set filename options for ES6to5.transform
Set filename options for ES6to5.transform

* `filename` is required to show the proper filename on syntax errors
* `filenameRelative` is used to build module ids (names)

Without these options all files / modules will be named "unknown".

Example:
filename: '/app/assets/foo/bar/baz.es6.erb'
filenameRelative: 'foo/bar/baz.js'
moduleId: 'foo/bar/baz'
