Sha256: fe7f862e762928909d95e3350c3509e028c32fdb72830d95c11524d97569b1b3

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'sprockets/digest_utils'
require 'sprockets/source_map_utils' if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('4.x')

class Terser
  # A wrapper for Sprockets
  class Compressor
    VERSION = '1'

    def initialize(options = {})
      options[:comments] ||= :none
      @options = options.merge(Rails.application.config.assets.terser.to_h)
      @cache_key = -"Terser:#{::Terser::VERSION}:#{VERSION}:#{::Sprockets::DigestUtils.digest(options)}"
      @terser = ::Terser.new(@options)
    end

    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

    if Gem::Version.new(::Sprockets::VERSION) >= Gem::Version.new('4.x')
      def call(input)
        input_options = { :source_map => { :filename => input[:filename] } }

        js, map = @terser.compile_with_map(input[:data], input_options)

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

        { :data => js, :map => map }
      end
    else
      def call(input)
        @terser.compile(input[:data])
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/terser-1.2.4/lib/terser/compressor.rb
terser-1.2.5 lib/terser/compressor.rb
terser-1.2.4 lib/terser/compressor.rb
terser-1.2.3 lib/terser/compressor.rb
terser-1.2.2 lib/terser/compressor.rb
terser-1.2.1 lib/terser/compressor.rb
terser-1.2.0 lib/terser/compressor.rb
terser-1.1.20 lib/terser/compressor.rb
terser-1.1.19 lib/terser/compressor.rb