1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
# frozen_string_literal: true
require 'sprockets/utils'
module Sprockets
# `Compressing` is an internal mixin whose public methods are exposed on
# the `Environment` and `CachedEnvironment` classes.
module Compressing
include Utils
def compressors
config[:compressors]
end
# 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.
def register_compressor(mime_type, sym, klass)
self.config = hash_reassoc(config, :compressors, mime_type) do |compressors|
compressors[sym] = klass
compressors
end
end
# Return CSS compressor or nil if none is set
def css_compressor
if defined? @css_compressor
@css_compressor
end
end
# Assign a compressor to run on `text/css` assets.
#
# The compressor object must respond to `compress`.
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
# Return JS compressor or nil if none is set
def js_compressor
if defined? @js_compressor
@js_compressor
end
end
# Assign a compressor to run on `application/javascript` assets.
#
# The compressor object must respond to `compress`.
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
# Public: Checks if Gzip is enabled.
def gzip?
config[:gzip_enabled]
end
# Public: Checks if Gzip is disabled.
def skip_gzip?
!gzip?
end
# 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
#
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
end
end
|