Module: Sprockets::Compressing

Included in:
Sprockets, Base
Defined in:
lib/sprockets/compressing.rb

Overview

`Compressing` is an internal mixin whose public methods are exposed on the `Environment` and `Index` classes.

Instance Method Summary collapse

Instance Method Details

#compressorsObject


5
6
7
# File 'lib/sprockets/compressing.rb', line 5

def compressors
  deep_copy_hash(@compressors)
end

#css_compressorObject

Return CSS compressor or nil if none is set


14
15
16
# File 'lib/sprockets/compressing.rb', line 14

def css_compressor
  @css_compressor if defined? @css_compressor
end

#css_compressor=(compressor) ⇒ Object

Assign a compressor to run on `text/css` assets.

The compressor object must respond to `compress`.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sprockets/compressing.rb', line 21

def css_compressor=(compressor)
  unregister_bundle_processor 'text/css', css_compressor if css_compressor
  @css_compressor = nil
  return unless compressor

  if compressor.is_a?(Symbol)
    compressor = compressors['text/css'][compressor] || raise(Error, "unknown compressor: #{compressor}")
  end

  if compressor.respond_to?(:compress)
    klass = Class.new(Processor) do
      @name = "css_compressor"
      @processor = proc { |context, data| compressor.compress(data) }
    end
    @css_compressor = :css_compressor
  else
    @css_compressor = klass = compressor
  end

  register_bundle_processor 'text/css', klass
end

#js_compressorObject

Return JS compressor or nil if none is set


44
45
46
# File 'lib/sprockets/compressing.rb', line 44

def js_compressor
  @js_compressor if defined? @js_compressor
end

#js_compressor=(compressor) ⇒ Object

Assign a compressor to run on `application/javascript` assets.

The compressor object must respond to `compress`.


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sprockets/compressing.rb', line 51

def js_compressor=(compressor)
  unregister_bundle_processor 'application/javascript', js_compressor if js_compressor
  @js_compressor = nil
  return unless compressor

  if compressor.is_a?(Symbol)
    compressor = compressors['application/javascript'][compressor] || raise(Error, "unknown compressor: #{compressor}")
  end

  if compressor.respond_to?(:compress)
    klass = Class.new(Processor) do
      @name = "js_compressor"
      @processor = proc { |context, data| compressor.compress(data) }
    end
    @js_compressor = :js_compressor
  else
    @js_compressor = klass = compressor
  end

  register_bundle_processor 'application/javascript', klass
end

#register_compressor(mime_type, sym, klass) ⇒ Object


9
10
11
# File 'lib/sprockets/compressing.rb', line 9

def register_compressor(mime_type, sym, klass)
  @compressors[mime_type][sym] = klass
end