Class: Sprockets::BumbleD::Transformer

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

Defined Under Namespace

Classes: BabelBridge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Transformer

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sprockets/bumble_d/transformer.rb', line 21

def initialize(options)
  @options = options.dup
  @root_dir = @options.delete(:root_dir)
  babel_config_version = @options.delete(:babel_config_version)

  unless @root_dir && File.directory?(@root_dir)
    error_message =
      'You must provide the `root_dir` directory from which ' \
      'node modules are to be resolved'
    raise RootDirectoryDoesNotExistError, error_message
  end

  @resolution_complete = false
  @cache_key = [
    self.class.name,
    VERSION,
    babel_config_version,
    @options
  ].freeze
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



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

def cache_key
  @cache_key
end

Instance Method Details

#babelObject



78
79
80
# File 'lib/sprockets/bumble_d/transformer.rb', line 78

def babel
  @babel ||= BabelBridge.new(@root_dir)
end

#cache_key_from_input(input) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



74
75
76
# File 'lib/sprockets/bumble_d/transformer.rb', line 74

def cache_key_from_input(input)
  @cache_key + [input[:filename]] + [input[:data]]
end

#call(input) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sprockets/bumble_d/transformer.rb', line 44

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

  result = input[:cache].fetch(cache_key_from_input(input)) do
    filename_relative = Sprockets::PathUtils.split_subpath(
      input[:load_path],
      input[:filename]
    )

    options = {
      moduleIds: true,
      sourceRoot: input[:load_path],
      filename: input[:filename],
      filenameRelative: filename_relative,
      ast: false
    }.merge(options_with_resolved_plugins_and_presets)

    if options[:moduleIds] && options[:moduleRoot]
      options[:moduleId] ||= File.join(options[:moduleRoot], input[:name])
    elsif options[:moduleIds] && input[:name] && !input[:name].empty?
      options[:moduleId] ||= input[:name]
    end

    babel.transform(data, options)
  end

  { data: result['code'] }
end