class Sprockets::Commoner::Bundle

Constants

InaccessibleStubbedFileError
JS_PACKAGE_PATH
OUTRO
PRELUDE

Public Class Methods

call(input) click to toggle source
# File lib/sprockets/commoner/bundle.rb, line 51
def self.call(input)
  env = input[:environment]
  instance(env).call(input)
end
instance(env) click to toggle source
# File lib/sprockets/commoner/bundle.rb, line 47
def self.instance(env)
  @instance ||= new(env.root)
end
new(root) click to toggle source
Calls superclass method
# File lib/sprockets/commoner/bundle.rb, line 43
def initialize(root)
  super(root, 'NODE_PATH' => JS_PACKAGE_PATH)
end

Public Instance Methods

call(input) click to toggle source
# File lib/sprockets/commoner/bundle.rb, line 56
def call(input)
  return unless input[:metadata][:commoner_enabled]
  env = input[:environment]
  # Get the filenames of all the assets that are included in the bundle
  assets_in_bundle = Set.new(input[:metadata][:included]) { |uri| env.load(uri).filename }
  # Subtract the assets in the bundle from those that are required. The missing assets were excluded through stubbing.
  assets_missing = input[:metadata][:commoner_required] - assets_in_bundle

  global_identifiers = assets_missing.map do |filename|
    uri, _ = if Commoner.sprockets4?
      env.resolve(filename, accept: input[:content_type], pipeline: :self)
    else
      env.resolve(filename, accept: input[:content_type], pipeline: :self, compat: false)
    end
    asset = env.load(uri)
    # Retrieve the global variable the file is exposed through
    global = asset.metadata[:commoner_global_identifier]
    raise InaccessibleStubbedFileError, "#{filename} is stubbed in #{input[:filename]} but doesn't define a global. Add an 'expose' directive." if global.nil?
    [filename.slice(env.root.size + 1, filename.size), global]
  end

  used_helpers = input[:metadata][:commoner_used_helpers].to_a
  header_code = generate_header(used_helpers, global_identifiers)
  {
    data: "#{PRELUDE}#{header_code}\n#{input[:data]}#{OUTRO}",
    map:  shift_map(input[:metadata][:map], PRELUDE.lines.count + header_code.lines.count),
  }
end

Private Instance Methods

shift_map(map, offset) click to toggle source
# File lib/sprockets/commoner/bundle.rb, line 87
def shift_map(map, offset)
  return unless map && Commoner.sprockets4?
  index_map = Sprockets::SourceMapUtils.make_index_map(map)
  index_map.merge({
    "sections" => index_map["sections"].map { |section|
      section.merge({
        "offset" => section["offset"].merge({
          "line" => section["offset"]["line"] + offset
        })
      })
    }
  })
end