Class: Sprockets::BabelProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/babel_processor.rb

Constant Summary collapse

VERSION =
'1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BabelProcessor

Returns a new instance of BabelProcessor.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sprockets/babel_processor.rb', line 25

def initialize(options = {})
  @options = options.merge({
    'blacklist' => (options['blacklist'] || []) + ['useStrict'],
    'sourceMap' => true
  }).freeze

  @cache_key = [
    self.class.name,
    Autoload::Babel::Transpiler::VERSION,
    Autoload::Babel::Source::VERSION,
    VERSION,
    @options
  ].freeze
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



23
24
25
# File 'lib/sprockets/babel_processor.rb', line 23

def cache_key
  @cache_key
end

Class Method Details

.cache_keyObject



19
20
21
# File 'lib/sprockets/babel_processor.rb', line 19

def self.cache_key
  instance.cache_key
end

.call(input) ⇒ Object



15
16
17
# File 'lib/sprockets/babel_processor.rb', line 15

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

.instanceObject



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

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sprockets/babel_processor.rb', line 40

def call(input)
  data = input[:data]

  result = input[:cache].fetch(@cache_key + [input[:filename]] + [data]) do
    opts = {
      'moduleRoot' => nil,
      'filename' => input[:filename],
      'filenameRelative' => PathUtils.split_subpath(input[:load_path], input[:filename]),
      'sourceFileName' => File.basename(input[:filename]),
      'sourceMapTarget' => 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
    Autoload::Babel::Transpiler.transform(data, opts)
  end

  map = SourceMapUtils.format_source_map(result["map"], input)
  map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)

  { data: result['code'], map: map }
end