class Webpacked::Manifest

Webpack manifest loading, caching and entry point retrieval

Constants

ASSET_KINDS

Public Class Methods

asset_paths(entry, kind = nil) click to toggle source

Load manifest from file and cache it if Rails.configuration.webpacked.dev_server set to false. Return entry point asset path for :js or :css kind or both if kind skipped

# File lib/webpacked/manifest.rb, line 27
def asset_paths(entry, kind = nil)
  return unless Rails.configuration.webpacked.enabled

  validate_asset_kind(kind)
  if Rails.configuration.webpacked.dev_server
    @manifest = load_manifest!
  else
    @manifest ||= load_manifest!
  end
  validate_entry(entry)

  return @manifest[entry] unless kind
  return @manifest[entry][kind] if @manifest[entry]
end
load_manifest!() click to toggle source

Force to load manifest from file

# File lib/webpacked/manifest.rb, line 43
def load_manifest!
  manifest_path = Rails.configuration.webpacked.manifest_path
  manifest_path = Rails.root.join(manifest_path)
  raise LoadError, "File #{manifest_path} not found" unless File.exist?(manifest_path)

  manifest = JSON.parse(File.read manifest_path).with_indifferent_access
  clean_asset_paths(manifest)
  manifest
end

Private Class Methods

clean_asset_paths(manifest) click to toggle source
# File lib/webpacked/manifest.rb, line 66
def clean_asset_paths(manifest)
  manifest.each do |entry, assets|
    assets.each do |kind, asset_path|
      manifest[entry][kind] = if asset_path =~ %r{(http[s]?)://}i
        asset_path
      else
        Pathname.new(asset_path).cleanpath.to_s
      end
    end
  end
end
validate_asset_kind(kind) click to toggle source
# File lib/webpacked/manifest.rb, line 55
def validate_asset_kind(kind)
  return unless kind
  raise UnknownAssetKindError, kind unless ASSET_KINDS.include?(kind)
end
validate_entry(entry) click to toggle source
# File lib/webpacked/manifest.rb, line 60
def validate_entry(entry)
  unless entry == Rails.configuration.webpacked.common_entry_name
    raise EntryMissingError, entry unless @manifest[entry]
  end
end