Sha256: 371b3294d85c718812aed1897b79c2548eb4906088728764a988624344e17078

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true
require 'sprockets/autoload'
require 'sprockets/path_utils'
require 'sprockets/source_map_utils'
require 'json'

module Sprockets
  class BabelProcessor
    VERSION = '1'

    def self.instance
      @instance ||= new
    end

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

    def self.cache_key
      instance.cache_key
    end

    attr_reader :cache_key

    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

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

      result = input[:cache].fetch(@cache_key + [input[:filename]] + [data]) do
        opts = {
          'sourceRoot' => input[:load_path],
          'moduleRoot' => nil,
          'filename' => input[:filename],
          'filenameRelative' => PathUtils.split_subpath(input[:load_path], input[:filename]),
          'sourceFileName' => input[:source_path]
        }.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.decode_json_source_map(JSON.generate(result['map']))
      map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map["mappings"])

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sprockets-4.0.0.beta4 lib/sprockets/babel_processor.rb