module Sprockets::Processing
def process(processors, uri, filename, load_path, name, content_type)
Internal: Run processors on filename and data.
def process(processors, uri, filename, load_path, name, content_type) data, metadata = nil, {} input = { environment: self, cache: cache, uri: uri, filename: filename, load_path: load_path, name: name, content_type: content_type, metadata: metadata } processors.each do |processor| begin result = processor.call(input.merge(data: data, metadata: metadata)) case result when NilClass # noop when Hash data = result[:data] metadata = metadata.merge(result) metadata.delete(:data) when String data = result else raise Error, "invalid processor return type: #{result.class}" end end end { source: data, charset: data.encoding.name.downcase, length: data.bytesize, digest: digest_class.hexdigest(data), metadata: metadata } end
def process_bundle_reducers(assets, reducers)
reducers - Array of [initial, reducer_proc] pairs
assets - Array of Assets
metadata Hash.
Internal: Run bundle reducers on set of Assets producing a reduced
def process_bundle_reducers(assets, reducers) initial = {} reducers.each do |k, (v, _)| initial[k] = v if v end assets.reduce(initial) do |h, asset| reducers.each do |k, (_, block)| value = k == :data ? asset.source : asset.metadata[k] h[k] = h.key?(k) ? block.call(h[k], value) : value end h end end
def register_bundle_processor(mime_type, klass, &block)
end
data.gsub(...)
register_bundle_processor 'application/javascript', :my_processor do |context, data|
A block can be passed for to create a shorthand processor.
register_bundle_processor 'application/javascript', Sprockets::DirectiveProcessor
Registers a new Bundle Processor `klass` for `mime_type`.
def register_bundle_processor(mime_type, klass, &block) mutate_hash_config(:bundle_processors, mime_type) do |processors| processors.push(wrap_processor(klass, block)) processors end end
def register_bundle_reducer(mime_type, key, *args, &block)
block - Proc accepting the memo accumulator and current value
initial - Initial memo to pass to the reduce funciton (default: nil)
key - Symbol metadata key
mime_type - String MIME Type. Use '*/*' applies to all types.
}
total + count
Sprockets.register_bundle_reducer 'text/css', :selector_count, 0 { |total, count|
Sprockets.register_bundle_reducer 'application/javascript', :jshint_errors, [], :+
Examples
Public: Register bundle reducer function.
def register_bundle_reducer(mime_type, key, *args, &block) case args.size when 0 reducer = block when 1 if block_given? initial = args[0] reducer = block else initial = nil reducer = args[0].to_proc end when 2 initial = args[0] reducer = args[1].to_proc else raise ArgumentError, "wrong number of arguments (#{args.size} for 0..2)" end mutate_hash_config(:bundle_reducers, mime_type) do |reducers| reducers.merge(key => [initial, reducer]) end end
def register_postprocessor(mime_type, klass, proc = nil, &block)
end
data.gsub(...)
register_postprocessor 'application/javascript', :my_processor do |context, data|
A block can be passed for to create a shorthand processor.
register_postprocessor 'application/javascript', Sprockets::DirectiveProcessor
Registers a new Postprocessor `klass` for `mime_type`.
def register_postprocessor(mime_type, klass, proc = nil, &block) proc ||= block mutate_hash_config(:postprocessors, mime_type) do |processors| processors.push(wrap_processor(klass, proc)) processors end end
def register_preprocessor(mime_type, klass, &block)
end
data.gsub(...)
register_preprocessor 'text/css', :my_processor do |context, data|
A block can be passed for to create a shorthand processor.
register_preprocessor 'text/css', Sprockets::DirectiveProcessor
Registers a new Preprocessor `klass` for `mime_type`.
def register_preprocessor(mime_type, klass, &block) mutate_hash_config(:preprocessors, mime_type) do |processors| processors.push(wrap_processor(klass, block)) processors end end
def unregister_bundle_processor(mime_type, klass)
unregister_bundle_processor 'application/javascript', Sprockets::DirectiveProcessor
Remove Bundle Processor `klass` for `mime_type`.
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::LegacyProcProcessor (#{klass})" } end mutate_hash_config(:bundle_processors, mime_type) do |processors| processors.delete(klass) processors end end
def unregister_postprocessor(mime_type, klass)
unregister_postprocessor 'text/css', Sprockets::DirectiveProcessor
Remove Postprocessor `klass` for `mime_type`.
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::LegacyProcProcessor (#{klass})" } end mutate_hash_config(:postprocessors, mime_type) do |processors| processors.delete(klass) processors end end
def unregister_preprocessor(mime_type, klass)
unregister_preprocessor 'text/css', Sprockets::DirectiveProcessor
Remove Preprocessor `klass` for `mime_type`.
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::LegacyProcProcessor (#{klass})" } end mutate_hash_config(:preprocessors, mime_type) do |processors| processors.delete(klass) processors end end
def unwrap_bundle_processors(mime_type)
mime_type - String MIME type.
Internal: Find and load bundle processors by mime type.
def unwrap_bundle_processors(mime_type) bundle_processors[mime_type].map do |processor| unwrap_processor(processor) end end
def unwrap_bundle_reducers(mime_type)
mime_type - String MIME type
Internal: Gather all bundle reducer functions for MIME type.
def unwrap_bundle_reducers(mime_type) self.bundle_reducers['*/*'].merge(self.bundle_reducers[mime_type]) end
def unwrap_postprocessors(mime_type)
mime_type - String MIME type.
Internal: Find and load postprocessors by mime type.
def unwrap_postprocessors(mime_type) postprocessors[mime_type].map do |processor| unwrap_processor(processor) end end
def unwrap_preprocessors(mime_type)
mime_type - String MIME type.
Internal: Find and load preprocessors by mime type.
def unwrap_preprocessors(mime_type) preprocessors[mime_type].map do |processor| unwrap_processor(processor) end end
def unwrap_processor(processor)
def unwrap_processor(processor) processor.respond_to?(:unwrap) ? processor.unwrap : processor end
def wrap_processor(klass, proc)
def wrap_processor(klass, proc) if !proc if klass.class == Sprockets::LazyProcessor || klass.respond_to?(:call) klass else LegacyTiltProcessor.new(klass) end elsif proc.respond_to?(:arity) && proc.arity == 2 LegacyProcProcessor.new(klass.to_s, proc) else proc end end