Module: Sprockets::Processing
Overview
‘Processing` is an internal mixin whose public methods are exposed on the `Environment` and `Index` classes.
Instance Method Summary collapse
-
#bundle_processors(mime_type = nil) ⇒ Object
Returns an ‘Array` of `Processor` classes.
-
#css_compressor ⇒ Object
Return CSS compressor or nil if none is set.
-
#css_compressor=(compressor) ⇒ Object
Assign a compressor to run on ‘text/css` assets.
-
#format_extensions ⇒ Object
Returns an ‘Array` of format extension `String`s.
-
#js_compressor ⇒ Object
Return JS compressor or nil if none is set.
-
#js_compressor=(compressor) ⇒ Object
Assign a compressor to run on ‘application/javascript` assets.
-
#postprocessors(mime_type = nil) ⇒ Object
Returns an ‘Array` of `Processor` classes.
-
#preprocessors(mime_type = nil) ⇒ Object
Returns an ‘Array` of `Processor` classes.
-
#processors(*args) ⇒ Object
Deprecated alias for ‘preprocessors`.
-
#register_bundle_processor(mime_type, klass, &block) ⇒ Object
Registers a new Bundle Processor ‘klass` for `mime_type`.
-
#register_postprocessor(mime_type, klass, &block) ⇒ Object
Registers a new Postprocessor ‘klass` for `mime_type`.
-
#register_preprocessor(mime_type, klass, &block) ⇒ Object
Registers a new Preprocessor ‘klass` for `mime_type`.
-
#register_processor(*args, &block) ⇒ Object
Deprecated alias for ‘register_preprocessor`.
-
#unregister_bundle_processor(mime_type, klass) ⇒ Object
Remove Bundle Processor ‘klass` for `mime_type`.
-
#unregister_postprocessor(mime_type, klass) ⇒ Object
Remove Postprocessor ‘klass` for `mime_type`.
-
#unregister_preprocessor(mime_type, klass) ⇒ Object
Remove Preprocessor ‘klass` for `mime_type`.
-
#unregister_processor(*args) ⇒ Object
Deprecated alias for ‘unregister_preprocessor`.
Instance Method Details
#bundle_processors(mime_type = nil) ⇒ Object
Returns an ‘Array` of `Processor` classes. If a `mime_type` argument is supplied, the processors registered under that extension will be returned.
Bundle Processors are ran on concatenated assets rather than individual files.
All ‘Processor`s must follow the `Tilt::Template` interface. It is recommended to subclass `Tilt::Template`.
150 151 152 153 154 155 156 |
# File 'lib/sprockets/processing.rb', line 150 def bundle_processors(mime_type = nil) if mime_type @bundle_processors[mime_type].dup else deep_copy_hash(@bundle_processors) end end |
#css_compressor ⇒ Object
Return CSS compressor or nil if none is set
196 197 198 199 200 201 |
# File 'lib/sprockets/processing.rb', line 196 def css_compressor bundle_processors('text/css').detect { |klass| klass.respond_to?(:name) && klass.name == 'Sprockets::Processor (css_compressor)' } end |
#css_compressor=(compressor) ⇒ Object
Assign a compressor to run on ‘text/css` assets.
The compressor object must respond to ‘compress` or `compile`.
206 207 208 209 210 211 212 213 |
# File 'lib/sprockets/processing.rb', line 206 def css_compressor=(compressor) unregister_bundle_processor 'text/css', :css_compressor return unless compressor register_bundle_processor 'text/css', :css_compressor do |context, data| compressor.compress(data) end end |
#format_extensions ⇒ Object
Returns an ‘Array` of format extension `String`s.
format_extensions
# => ['.js', '.css']
15 16 17 |
# File 'lib/sprockets/processing.rb', line 15 def format_extensions @trail.extensions - @engines.keys end |
#js_compressor ⇒ Object
Return JS compressor or nil if none is set
216 217 218 219 220 221 |
# File 'lib/sprockets/processing.rb', line 216 def js_compressor bundle_processors('application/javascript').detect { |klass| klass.respond_to?(:name) && klass.name == 'Sprockets::Processor (js_compressor)' } end |
#js_compressor=(compressor) ⇒ Object
Assign a compressor to run on ‘application/javascript` assets.
The compressor object must respond to ‘compress` or `compile`.
226 227 228 229 230 231 232 233 |
# File 'lib/sprockets/processing.rb', line 226 def js_compressor=(compressor) unregister_bundle_processor 'application/javascript', :js_compressor return unless compressor register_bundle_processor 'application/javascript', :js_compressor do |context, data| compressor.compress(data) end end |
#postprocessors(mime_type = nil) ⇒ Object
Returns an ‘Array` of `Processor` classes. If a `mime_type` argument is supplied, the processors registered under that extension will be returned.
Postprocessors are ran after Preprocessors and Engine processors.
All ‘Processor`s must follow the `Tilt::Template` interface. It is recommended to subclass `Tilt::Template`.
49 50 51 52 53 54 55 |
# File 'lib/sprockets/processing.rb', line 49 def postprocessors(mime_type = nil) if mime_type @postprocessors[mime_type].dup else deep_copy_hash(@postprocessors) end end |
#preprocessors(mime_type = nil) ⇒ Object
Returns an ‘Array` of `Processor` classes. If a `mime_type` argument is supplied, the processors registered under that extension will be returned.
Preprocessors are ran before Postprocessors and Engine processors.
All ‘Processor`s must follow the `Tilt::Template` interface. It is recommended to subclass `Tilt::Template`.
33 34 35 36 37 38 39 |
# File 'lib/sprockets/processing.rb', line 33 def preprocessors(mime_type = nil) if mime_type @preprocessors[mime_type].dup else deep_copy_hash(@preprocessors) end end |
#processors(*args) ⇒ Object
Deprecated alias for ‘preprocessors`.
20 21 22 |
# File 'lib/sprockets/processing.rb', line 20 def processors(*args) preprocessors(*args) end |
#register_bundle_processor(mime_type, klass, &block) ⇒ Object
Registers a new Bundle Processor ‘klass` for `mime_type`.
register_bundle_processor 'text/css', Sprockets::CharsetNormalizer
A block can be passed for to create a shorthand processor.
register_bundle_processor :my_processor do |context, data|
data.gsub(...)
end
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sprockets/processing.rb', line 168 def register_bundle_processor(mime_type, klass, &block) if block_given? name = klass.to_s klass = Class.new(Processor) do @name = name @processor = block end end @bundle_processors[mime_type].push(klass) end |
#register_postprocessor(mime_type, klass, &block) ⇒ Object
Registers a new Postprocessor ‘klass` for `mime_type`.
register_postprocessor 'text/css', Sprockets::CharsetNormalizer
A block can be passed for to create a shorthand processor.
register_postprocessor 'text/css', :my_processor do |context, data|
data.gsub(...)
end
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sprockets/processing.rb', line 94 def register_postprocessor(mime_type, klass, &block) if block_given? name = klass.to_s klass = Class.new(Processor) do @name = name @processor = block end end @postprocessors[mime_type].push(klass) end |
#register_preprocessor(mime_type, klass, &block) ⇒ Object
Registers a new Preprocessor ‘klass` for `mime_type`.
register_preprocessor 'text/css', Sprockets::DirectiveProcessor
A block can be passed for to create a shorthand processor.
register_preprocessor 'text/css', :my_processor do |context, data|
data.gsub(...)
end
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sprockets/processing.rb', line 72 def register_preprocessor(mime_type, klass, &block) if block_given? name = klass.to_s klass = Class.new(Processor) do @name = name @processor = block end end @preprocessors[mime_type].push(klass) end |
#register_processor(*args, &block) ⇒ Object
Deprecated alias for ‘register_preprocessor`.
58 59 60 |
# File 'lib/sprockets/processing.rb', line 58 def register_processor(*args, &block) register_preprocessor(*args, &block) end |
#unregister_bundle_processor(mime_type, klass) ⇒ Object
Remove Bundle Processor ‘klass` for `mime_type`.
unregister_bundle_processor 'text/css', Sprockets::CharsetNormalizer
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/sprockets/processing.rb', line 184 def unregister_bundle_processor(mime_type, klass) if klass.is_a?(String) || klass.is_a?(Symbol) klass = @bundle_processors[mime_type].detect { |cls| cls.respond_to?(:name) && cls.name == "Sprockets::Processor (#{klass})" } end @bundle_processors[mime_type].delete(klass) end |
#unregister_postprocessor(mime_type, klass) ⇒ Object
Remove Postprocessor ‘klass` for `mime_type`.
unregister_postprocessor 'text/css', Sprockets::DirectiveProcessor
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/sprockets/processing.rb', line 130 def unregister_postprocessor(mime_type, klass) if klass.is_a?(String) || klass.is_a?(Symbol) klass = @postprocessors[mime_type].detect { |cls| cls.respond_to?(:name) && cls.name == "Sprockets::Processor (#{klass})" } end @postprocessors[mime_type].delete(klass) end |
#unregister_preprocessor(mime_type, klass) ⇒ Object
Remove Preprocessor ‘klass` for `mime_type`.
unregister_preprocessor 'text/css', Sprockets::DirectiveProcessor
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sprockets/processing.rb', line 115 def unregister_preprocessor(mime_type, klass) if klass.is_a?(String) || klass.is_a?(Symbol) klass = @preprocessors[mime_type].detect { |cls| cls.respond_to?(:name) && cls.name == "Sprockets::Processor (#{klass})" } end @preprocessors[mime_type].delete(klass) end |
#unregister_processor(*args) ⇒ Object
Deprecated alias for ‘unregister_preprocessor`.
107 108 109 |
# File 'lib/sprockets/processing.rb', line 107 def unregister_processor(*args) unregister_preprocessor(*args) end |