Method: Condenser::BabelProcessor#initialize

Defined in:
lib/condenser/processors/babel_processor.rb

#initialize(dir = nil, **options) ⇒ BabelProcessor

Returns a new instance of BabelProcessor.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/condenser/processors/babel_processor.rb', line 7

def initialize(dir = nil, **options)
  super(dir)
  
  options[:plugins] ||= [
    ["@babel/plugin-transform-runtime", { corejs: 3, useESModules: true }]
  ]
  options[:presets] ||= [
    ['@babel/preset-env', {
      modules: false,
      targets: { browsers: '> 1% and not dead' }
    }]
  ]

  packages = options.slice(:plugins, :presets).values.reduce(&:+).map { |p| p.is_a?(Array) ? p[0] : p}
  packages.unshift('@babel/core')
  if packages.include?('@babel/plugin-transform-runtime')
    runtime = options[:plugins].find { |i| i.is_a?(Array) ? i[0] == '@babel/plugin-transform-runtime' : i == '@babel/plugin-transform-runtime' }
    packages << if runtime.is_a?(Array) && runtime[1][:corejs]
      if runtime[1][:corejs].is_a?(Hash)
        "@babel/runtime-corejs#{runtime[1][:corejs][:version]}"
      else
        "@babel/runtime-corejs#{runtime[1][:corejs]}"
      end
    else
      '@babel/runtime'
    end
  end
  
  npm_install(*packages)
  
  options[:plugins].map! do |plugin|
    if plugin.is_a?(Array)
      plugin[0] = npm_module_path(plugin[0])
      plugin
    else
      npm_module_path(plugin)
    end
  end
  
  options[:presets].map! do |plugin|
    if plugin.is_a?(Array)
      plugin[0] = npm_module_path(plugin[0])
      plugin
    else
      npm_module_path(plugin)
    end
  end

  @options = {
    ast:        false,
    compact:    false,
    sourceMap:  false
  }.merge(options)
end