Class: Terser::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/terser/compressor.rb

Overview

A wrapper for Sprockets

Constant Summary collapse

VERSION =
'1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compressor

Returns a new instance of Compressor.



11
12
13
14
15
16
# File 'lib/terser/compressor.rb', line 11

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

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



30
31
32
# File 'lib/terser/compressor.rb', line 30

def cache_key
  @cache_key
end

Class Method Details

.cache_keyObject



26
27
28
# File 'lib/terser/compressor.rb', line 26

def self.cache_key
  instance.cache_key
end

.call(input) ⇒ Object



22
23
24
# File 'lib/terser/compressor.rb', line 22

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

.instanceObject



18
19
20
# File 'lib/terser/compressor.rb', line 18

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/terser/compressor.rb', line 33

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