Sha256: 3ae8aba8111446e2d98d26558bbbbe3a11d5015382fad12db3f1408b40ac23d1

Contents?: true

Size: 1.67 KB

Versions: 28

Compression:

Stored size: 1.67 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 = {
          '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
  end
end

Version data entries

28 entries across 27 versions & 10 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/sprockets-4.2.1/lib/sprockets/babel_processor.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.1/lib/sprockets/babel_processor.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/sprockets-4.2.1/lib/sprockets/babel_processor.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/sprockets-4.2.1/lib/sprockets/babel_processor.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/sprockets-4.1.1/lib/sprockets/babel_processor.rb
sprockets-4.2.1 lib/sprockets/babel_processor.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/sprockets-4.1.1/lib/sprockets/babel_processor.rb
sprockets-4.2.0 lib/sprockets/babel_processor.rb
sprockets-4.1.1 lib/sprockets/babel_processor.rb
sprockets-4.1.0 lib/sprockets/babel_processor.rb
sprockets-4.0.3 lib/sprockets/babel_processor.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/sprockets-4.0.2/lib/sprockets/babel_processor.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/sprockets-4.0.2/lib/sprockets/babel_processor.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/sprockets-4.0.2/lib/sprockets/babel_processor.rb
sprockets-4.0.2 lib/sprockets/babel_processor.rb
sprockets-4.0.1 lib/sprockets/babel_processor.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/sprockets-4.0.0/lib/sprockets/babel_processor.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/sprockets-4.0.0/lib/sprockets/babel_processor.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/sprockets-4.0.0/lib/sprockets/babel_processor.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/sprockets-4.0.0/lib/sprockets/babel_processor.rb