class BabelSchmoozeSprockets::BabelProcessor

Constants

DEFAULT_BABEL_OPTIONS
DEFAULT_PLUGINS
DEFAULT_PRESETS

Public Class Methods

call(input) click to toggle source
# File lib/babel-schmooze-sprockets/babel_processor.rb, line 28
def self.call(input)
  instance.call(input)
end
instance() click to toggle source
# File lib/babel-schmooze-sprockets/babel_processor.rb, line 21
def self.instance
  @instance ||= BabelProcessor.new(
    root_dir: File.expand_path("#{__dir__}/../.."),
    options: BabelProcessor::DEFAULT_BABEL_OPTIONS
  )
end
new(options) click to toggle source
# File lib/babel-schmooze-sprockets/babel_processor.rb, line 32
def initialize(options)
  @options = options

  @cache_key = [
    self.class.name,
    babel.version,
    VERSION,
    @options
  ].freeze
end

Public Instance Methods

babel() click to toggle source
# File lib/babel-schmooze-sprockets/babel_processor.rb, line 73
def babel
  @babel ||= Babel.new(@options.fetch(:root_dir))
end
call(input) click to toggle source
# File lib/babel-schmooze-sprockets/babel_processor.rb, line 43
def call(input)
  data = input[:data]

  result = input[:cache].fetch(@cache_key + [input[:filename]] + [data]) do
    options = {
      moduleIds: true,
      sourceRoot: input[:load_path],
      moduleRoot: nil,
      filename: input[:filename],
      filenameRelative: Sprockets::PathUtils.split_subpath(input[:load_path], input[:filename]),
      sourceFileName: input[:source_path],
      sourceMaps: true,
      ast: false
    }.merge(@options.fetch(:options))

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

    babel.transform(data, options)
  end

  map = Sprockets::SourceMapUtils.decode_json_source_map(JSON.generate(result["map"]))
  map = Sprockets::SourceMapUtils.combine_source_maps(input[:metadata][:map], map["mappings"])

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