Sha256: abd9386c73b8ae47e52d0ac24d8a91a01d25624635f5ff675ec0078de3deed6e

Contents?: true

Size: 1.02 KB

Versions: 17

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true
require 'sprockets/source_map_utils'
require 'ruby2js'
require 'ruby2js/version'

module Ruby2JS
  module SprocketsTransformer
    include Sprockets
    VERSION = '1'

    @@options = {}

    def self.options=(options)
      @@options = options
    end

    def self.cache_key
      @cache_key ||= "#{name}:#{Ruby2JS::VERSION::STRING}:#{VERSION}".freeze
    end

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

      js, map = input[:cache].fetch([self.cache_key, data]) do
        result = Ruby2JS.convert(data, {**@@options, file: input[:filename]})
        [result.to_s, result.sourcemap.transform_keys {|key| key.to_s}]
      end

      map = SourceMapUtils.format_source_map(map, input)
      map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)

      { data: js, map: map }
    end
  end
end

Sprockets.register_mime_type 'application/ruby', extensions: ['.rb', '.js.rb']

Sprockets.register_transformer 'application/ruby', 'application/javascript',
  Ruby2JS::SprocketsTransformer

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ruby2js-5.0.1 lib/ruby2js/sprockets.rb
ruby2js-5.0.0 lib/ruby2js/sprockets.rb
ruby2js-4.2.2 lib/ruby2js/sprockets.rb
ruby2js-4.2.1 lib/ruby2js/sprockets.rb
ruby2js-4.2.0 lib/ruby2js/sprockets.rb
ruby2js-4.1.7 lib/ruby2js/sprockets.rb
ruby2js-4.1.6 lib/ruby2js/sprockets.rb
ruby2js-4.1.5 lib/ruby2js/sprockets.rb
ruby2js-4.1.4 lib/ruby2js/sprockets.rb
ruby2js-4.1.3 lib/ruby2js/sprockets.rb
ruby2js-4.1.2 lib/ruby2js/sprockets.rb
ruby2js-4.1.1 lib/ruby2js/sprockets.rb
ruby2js-4.1.0 lib/ruby2js/sprockets.rb
ruby2js-4.0.5 lib/ruby2js/sprockets.rb
ruby2js-4.0.4 lib/ruby2js/sprockets.rb
ruby2js-4.0.3 lib/ruby2js/sprockets.rb
ruby2js-4.0.2 lib/ruby2js/sprockets.rb