Module: Sprockets::Processing

Includes:
Utils
Included in:
Configuration, Loader
Defined in:
lib/sprockets/processing.rb

Overview

`Processing` 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

#bundle_processorsObject

Bundle Processors are ran on concatenated assets rather than individual files.



118
119
120
# File 'lib/sprockets/processing.rb', line 118

def bundle_processors
  config[:bundle_processors]
end

#bundle_reducersObject

Internal: Two dimensional Hash of reducer functions for a given mime type and asset metadata key.



169
170
171
# File 'lib/sprockets/processing.rb', line 169

def bundle_reducers
  config[:bundle_reducers]
end

#postprocessorsObject

Postprocessors are ran after Preprocessors and Engine processors.



32
33
34
# File 'lib/sprockets/processing.rb', line 32

def postprocessors
  config[:postprocessors]
end

#preprocessorsObject

Preprocessors are ran before Postprocessors and Engine processors.



16
17
18
# File 'lib/sprockets/processing.rb', line 16

def preprocessors
  config[:preprocessors]
end

#process_bundle_reducers(assets, reducers) ⇒ Object

Internal: Run bundle reducers on set of Assets producing a reduced metadata Hash.

assets - Array of Assets reducers - Array of [initial, reducer_proc] pairs

Returns reduced asset metadata Hash.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/sprockets/processing.rb', line 229

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.[k]
      h[k]  = h.key?(k) ? block.call(h[k], value) : value
    end
    h
  end
end

#register_bundle_metadata_reducer(mime_type, key, *args, &block) ⇒ Object

Public: Register bundle metadata reducer function.

Examples

Sprockets.register_bundle_metadata_reducer 'application/javascript', :jshint_errors, [], :+

Sprockets.register_bundle_metadata_reducer 'text/css', :selector_count, 0 { |total, count|
  total + count
}

mime_type - String MIME Type. Use '/' applies to all types. key - Symbol metadata key initial - Initial memo to pass to the reduce funciton (default: nil) block - Proc accepting the memo accumulator and current value

Returns nothing.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/sprockets/processing.rb', line 189

def (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

  self.config = hash_reassoc(config, :bundle_reducers, mime_type) do |reducers|
    reducers.merge(key => [initial, reducer])
  end
end

#register_bundle_processor(mime_type, klass, &block) ⇒ Object

Registers a new Bundle Processor `klass` for `mime_type`.

register_bundle_processor  'application/javascript', Sprockets::DirectiveProcessor

A block can be passed for to create a shorthand processor.

register_bundle_processor 'application/javascript', :my_processor do |context, data|
  data.gsub(...)
end


143
144
145
146
147
148
# File 'lib/sprockets/processing.rb', line 143

def register_bundle_processor(mime_type, klass, &block)
  self.config = hash_reassoc(config, :bundle_processors, mime_type) do |processors|
    processors.push(wrap_processor(klass, block))
    processors
  end
end

#register_postprocessor(mime_type, klass, proc = nil, &block) ⇒ Object

Registers a new Postprocessor `klass` for `mime_type`.

register_postprocessor 'application/javascript', Sprockets::DirectiveProcessor

A block can be passed for to create a shorthand processor.

register_postprocessor 'application/javascript', :my_processor do |context, data|
  data.gsub(...)
end


74
75
76
77
78
79
80
# File 'lib/sprockets/processing.rb', line 74

def register_postprocessor(mime_type, klass, proc = nil, &block)
  proc ||= block
  self.config = hash_reassoc(config, :postprocessors, mime_type) do |processors|
    processors.push(wrap_processor(klass, proc))
    processors
  end
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


57
58
59
60
61
62
# File 'lib/sprockets/processing.rb', line 57

def register_preprocessor(mime_type, klass, &block)
  self.config = hash_reassoc(config, :preprocessors, mime_type) do |processors|
    processors.push(wrap_processor(klass, block))
    processors
  end
end

#unregister_bundle_processor(mime_type, klass) ⇒ Object

Remove Bundle Processor `klass` for `mime_type`.

unregister_bundle_processor 'application/javascript', Sprockets::DirectiveProcessor


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sprockets/processing.rb', line 154

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

  self.config = hash_reassoc(config, :bundle_processors, mime_type) do |processors|
    processors.delete(klass)
    processors
  end
end

#unregister_postprocessor(mime_type, klass) ⇒ Object

Remove Postprocessor `klass` for `mime_type`.

unregister_postprocessor 'text/css', Sprockets::DirectiveProcessor


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sprockets/processing.rb', line 103

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

  self.config = hash_reassoc(config, :postprocessors, mime_type) do |processors|
    processors.delete(klass)
    processors
  end
end

#unregister_preprocessor(mime_type, klass) ⇒ Object

Remove Preprocessor `klass` for `mime_type`.

unregister_preprocessor 'text/css', Sprockets::DirectiveProcessor


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sprockets/processing.rb', line 86

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

  self.config = hash_reassoc(config, :preprocessors, mime_type) do |processors|
    processors.delete(klass)
    processors
  end
end

#unwrap_bundle_processors(mime_type) ⇒ Object

Internal: Find and load bundle processors by mime type.

mime_type - String MIME type.

Returns Array of Procs.



127
128
129
130
131
# File 'lib/sprockets/processing.rb', line 127

def unwrap_bundle_processors(mime_type)
  bundle_processors[mime_type].map do |processor|
    unwrap_processor(processor)
  end
end

#unwrap_bundle_reducers(mime_type) ⇒ Object

Internal: Gather all bundle reducer functions for MIME type.

mime_type - String MIME type

Returns an Array of [initial, reducer_proc] pairs.



218
219
220
# File 'lib/sprockets/processing.rb', line 218

def unwrap_bundle_reducers(mime_type)
  self.bundle_reducers['*/*'].merge(self.bundle_reducers[mime_type])
end

#unwrap_postprocessors(mime_type) ⇒ Object

Internal: Find and load postprocessors by mime type.

mime_type - String MIME type.

Returns Array of Procs.



41
42
43
44
45
# File 'lib/sprockets/processing.rb', line 41

def unwrap_postprocessors(mime_type)
  postprocessors[mime_type].map do |processor|
    unwrap_processor(processor)
  end
end

#unwrap_preprocessors(mime_type) ⇒ Object

Internal: Find and load preprocessors by mime type.

mime_type - String MIME type.

Returns Array of Procs.



25
26
27
28
29
# File 'lib/sprockets/processing.rb', line 25

def unwrap_preprocessors(mime_type)
  preprocessors[mime_type].map do |processor|
    unwrap_processor(processor)
  end
end