Class: Sprockets::ES6

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

Constant Summary collapse

VERSION =
'0.9.2'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ES6

Returns a new instance of ES6.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sprockets/es6.rb', line 49

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

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



11
12
13
# File 'lib/sprockets/es6.rb', line 11

def configuration
  @configuration
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



43
44
45
# File 'lib/sprockets/es6.rb', line 43

def cache_key
  @cache_key
end

Class Method Details

.cache_keyObject



37
38
39
# File 'lib/sprockets/es6.rb', line 37

def cache_key
  instance.cache_key
end

.call(input) ⇒ Object



33
34
35
# File 'lib/sprockets/es6.rb', line 33

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

.configuration_hashObject



13
14
15
16
17
18
# File 'lib/sprockets/es6.rb', line 13

def configuration_hash
  configuration.to_h.reduce({}) do |hash, (key, val)|
    hash[key.to_s] = val
    hash
  end
end

.configure {|configuration| ... } ⇒ Object

Yields:



24
25
26
27
# File 'lib/sprockets/es6.rb', line 24

def configure
  self.configuration ||= OpenStruct.new
  yield configuration
end

.instanceObject



20
21
22
# File 'lib/sprockets/es6.rb', line 20

def instance
  @instance ||= new
end

.reset_configurationObject



29
30
31
# File 'lib/sprockets/es6.rb', line 29

def reset_configuration
  self.configuration = OpenStruct.new
end

Instance Method Details

#call(input) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/sprockets/es6.rb', line 61

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

#configuration_hashObject



45
46
47
# File 'lib/sprockets/es6.rb', line 45

def configuration_hash
  self.class.configuration_hash
end

#transform(data, opts) ⇒ Object



69
70
71
# File 'lib/sprockets/es6.rb', line 69

def transform(data, opts)
  Babel::Transpiler.transform(data, opts)
end

#transformation_options(input) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sprockets/es6.rb', line 73

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