123456789_123456789_123456789_123456789_123456789_

Module: Sprockets::Compressing

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Utils
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

Utils - Included

MODULE_INCLUDE_MUTEX, WHITESPACE_ORDINALS

Instance Attribute Summary

Instance Method Summary

Utils - Included

#concat_javascript_sources

Internal: Accumulate asset source to buffer and append a trailing semicolon if necessary.

#dfs

Internal: Post-order Depth-First search algorithm.

#dfs_paths

Internal: Post-order Depth-First search algorithm that gathers all paths along the way.

#duplicable?

Internal: Check if object can safely be .dup’d.

#hash_reassoc

Internal: Duplicate and store key/value on new frozen hash.

#hash_reassoc1

Internal: Duplicate and store key/value on new frozen hash.

#module_include

Internal: Inject into target module for the duration of the block.

#string_end_with_semicolon?

Internal: Check if string has a trailing semicolon.

Instance Attribute Details

#css_compressor (rw)

Return CSS compressor or nil if none is set

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 40

def css_compressor
  if defined? @css_compressor
    @css_compressor
  end
end

#css_compressor=(compressor) (rw)

Assign a compressor to run on text/css assets.

The compressor object must respond to compress.

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 49

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 = config[:compressors]['text/css'][compressor] || raise(Error, "unknown compressor: #{compressor}")
  elsif compressor.respond_to?(:compress)
    klass = proc { |input| compressor.compress(input[:data]) }
    @css_compressor = :css_compressor
  else
    @css_compressor = klass = compressor
  end

  register_bundle_processor 'text/css', klass
end

#gzip=(gzip) (rw)

Public: Enable or disable the creation of Gzip files.

To disable gzip generation set to a falsey value:

environment.gzip = false

To enable set to a truthy value. By default zlib wil be used to gzip assets. If you have the Zopfli gem installed you can specify the zopfli algorithm to be used instead:

environment.gzip = :zopfli
[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 116

def gzip=(gzip)
  self.config = config.merge(gzip_enabled: gzip).freeze

  case gzip
  when false, nil
    self.unregister_exporter Exporters::ZlibExporter
    self.unregister_exporter Exporters::ZopfliExporter
  when :zopfli
    self.unregister_exporter Exporters::ZlibExporter
    self.register_exporter '*/*', Exporters::ZopfliExporter
  else
    self.unregister_exporter Exporters::ZopfliExporter
    self.register_exporter '*/*', Exporters::ZlibExporter
  end

  gzip
end

#gzip?Boolean (rw)

Public: Checks if Gzip is enabled.

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 94

def gzip?
  config[:gzip_enabled]
end

#js_compressor (rw)

Return JS compressor or nil if none is set

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 67

def js_compressor
  if defined? @js_compressor
    @js_compressor
  end
end

#js_compressor=(compressor) (rw)

Assign a compressor to run on application/javascript assets.

The compressor object must respond to compress.

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 76

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 = config[:compressors]['application/javascript'][compressor] || raise(Error, "unknown compressor: #{compressor}")
  elsif compressor.respond_to?(:compress)
    klass = proc { |input| compressor.compress(input[:data]) }
    @js_compressor = :js_compressor
  else
    @js_compressor = klass = compressor
  end

  register_bundle_processor 'application/javascript', klass
end

#skip_gzip?Boolean (readonly)

Public: Checks if Gzip is disabled.

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 99

def skip_gzip?
  !gzip?
end

Instance Method Details

#compressors

[ GitHub ]

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

def compressors
  config[:compressors]
end

#register_compressor(mime_type, sym, klass)

Public: Register a new compressor klass at sym for mime_type.

Registering a processor allows it to be looked up by sym later when assigning a JavaScript or CSS compressor.

Compressors only operate on JavaScript and CSS. If you want to compress a different type of asset, use a processor instead.

Examples

register_compressor 'text/css', :my_sass, MySassCompressor
css_compressor = :my_sass

mime_type - String MIME Type (one of: ‘test/css’ or ‘application/javascript’). sym - Symbol registration address. klass - The compressor class.

Returns nothing.

[ GitHub ]

  
# File 'lib/sprockets/compressing.rb', line 32

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