Module: Sprockets::Compressing

Includes:
Utils
Included in:
Configuration
Defined in:
lib/sprockets/compressing.rb

Overview

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

Constant Summary

Constants included from Utils

Utils::UNBOUND_METHODS_BIND_TO_ANY_OBJECT

Instance Method Summary collapse

Methods included from Utils

#concat_javascript_sources, #dfs, #duplicable?, #hash_reassoc, #hash_reassoc1, #module_include, #normalize_extension, #string_end_with_semicolon?

Instance Method Details

#compressorsObject



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

def compressors
  config[:compressors]
end

#css_compressorObject

Return CSS compressor or nil if none is set



21
22
23
24
25
# File 'lib/sprockets/compressing.rb', line 21

def css_compressor
  if defined? @css_compressor
    unwrap_processor(@css_compressor)
  end
end

#css_compressor=(compressor) ⇒ Object

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

The compressor object must respond to `compress`.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sprockets/compressing.rb', line 30

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

  if compressor.is_a?(Symbol)
    @css_compressor = klass = compressors['text/css'][compressor] || raise(Error, "unknown compressor: #{compressor}")
  elsif compressor.respond_to?(:compress)
    klass = LegacyProcProcessor.new(:css_compressor, proc { |context, data| compressor.compress(data) })
    @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



48
49
50
51
52
# File 'lib/sprockets/compressing.rb', line 48

def js_compressor
  if defined? @js_compressor
    unwrap_processor(@js_compressor)
  end
end

#js_compressor=(compressor) ⇒ Object

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

The compressor object must respond to `compress`.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sprockets/compressing.rb', line 57

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

  if compressor.is_a?(Symbol)
    @js_compressor = klass = compressors['application/javascript'][compressor] || raise(Error, "unknown compressor: #{compressor}")
  elsif compressor.respond_to?(:compress)
    klass = LegacyProcProcessor.new(:js_compressor, proc { |context, data| compressor.compress(data) })
    @js_compressor = :js_compressor
  else
    @js_compressor = klass = compressor
  end

  register_bundle_processor 'application/javascript', klass
end

#register_compressor(mime_type, sym, klass) ⇒ Object



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

def register_compressor(mime_type, sym, klass)
  self.config = hash_reassoc(config, :compressors, mime_type) do |compressors|
    compressors[sym] = klass
    compressors
  end
end