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
# 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)}"
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key.



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

def cache_key
  @cache_key
end

Class Method Details

.cache_keyObject



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

def self.cache_key
  instance.cache_key
end

.call(input) ⇒ Object



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

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

.instanceObject



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

def self.instance
  @instance ||= new
end

Instance Method Details

#call(input) ⇒ Object



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

def call(input)
  input_options = { :source_map => { :filename => input[:filename] } }
  terser = ::Terser.new(@options.merge(input_options))

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

  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