Class: Sprockets::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/bundle.rb

Overview

Internal: Bundle processor takes a single file asset and prepends all the ‘:required` URIs to the contents.

Uses pipeline metadata:

:required - Ordered Set of asset URIs to prepend
:stubbed  - Set of asset URIs to substract from the required set.

Also see DirectiveProcessor.

Class Method Summary collapse

Class Method Details

.call(input) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sprockets/bundle.rb', line 15

def self.call(input)
  env  = input[:environment]
  type = input[:content_type]
  dependencies = Set.new(input[:metadata][:dependencies])

  processed_uri, deps = env.resolve(input[:filename], accept: type, pipeline: :self, compat: false)
  dependencies.merge(deps)

  find_required = proc { |uri| env.load(uri).[:required] }
  required = Utils.dfs(processed_uri, &find_required)
  stubbed  = Utils.dfs(env.load(processed_uri).[:stubbed], &find_required)
  required.subtract(stubbed)
  assets = required.map { |uri| env.load(uri) }

  (required + stubbed).each do |uri|
    dependencies.merge(env.load(uri).[:dependencies])
  end

  reducers = Hash[env.match_mime_type_keys(env.config[:bundle_reducers], type).flat_map(&:to_a)]
  process_bundle_reducers(assets, reducers).merge(dependencies: dependencies, included: assets.map(&:uri))
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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sprockets/bundle.rb', line 44

def self.process_bundle_reducers(assets, reducers)
  initial = {}
  reducers.each do |k, (v, _)|
    if v.respond_to?(:call)
      initial[k] = v.call
    elsif !v.nil?
      initial[k] = v
    end
  end

  assets.reduce(initial) do |h, asset|
    reducers.each do |k, (_, block)|
      value = k == :data ? asset.source : asset.[k]
      if h.key?(k)
        if !value.nil?
          h[k] = block.call(h[k], value)
        end
      else
        h[k] = value
      end
    end
    h
  end
end