Class: Sprockets::ES6Module

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/es6module.rb,
lib/sprockets/es6module/version.rb

Constant Summary collapse

MODULE_METHODS =
{
  amd: 'to_amd',
  common: 'to_cjs',
  umd: 'to_umd'
}
VERSION =
'0.0.2'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :amd, options = {}) ⇒ ES6Module

Returns a new instance of ES6Module.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sprockets/es6module.rb', line 22

def initialize(type = :amd, options = {})
  @method = MODULE_METHODS[type]

  raise "Unsupported method `#{type}`. Available types are: #{MODULE_METHODS.keys.inspect}" unless @method

  @options = options.dup.merge(strict: false).freeze

  @cache_key = [
    self.class.name,
    Esperanto::VERSION,
    VERSION,
    @method
  ].freeze
end

Class Method Details

.call(input) ⇒ Object



18
19
20
# File 'lib/sprockets/es6module.rb', line 18

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

.instanceObject



14
15
16
# File 'lib/sprockets/es6module.rb', line 14

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/sprockets/es6module.rb', line 37

def call(input)
  data = Sprockets::ES6.new(blacklist: ['es6.modules', 'useStrict']).call(input)

  module_name = input[:name]
  options = @options.merge(amdName: module_name)
  result = input[:cache].fetch(@cache_key + [options, data]) do
    Esperanto.send(@method, data, options)
  end
  result['code']
end