class WebpackManifest::Manifest

Attributes

cache[W]
path[R]

Public Class Methods

new(path, cache: false) click to toggle source
# File lib/webpack_manifest/manifest.rb, line 15
def initialize(path, cache: false)
  @path = path.to_s
  @cache = cache
end

Public Instance Methods

assets() click to toggle source
# File lib/webpack_manifest/manifest.rb, line 34
def assets
  data.values
end
cache_enabled?() click to toggle source
# File lib/webpack_manifest/manifest.rb, line 38
def cache_enabled?
  @cache
end
find(name) click to toggle source
# File lib/webpack_manifest/manifest.rb, line 30
def find(name)
  data[name.to_s]
end
lookup!(name) click to toggle source
# File lib/webpack_manifest/manifest.rb, line 26
def lookup!(name)
  find(name) || handle_missing_entry(name)
end
lookup_pack_with_chunks!(name, type: nil) click to toggle source
# File lib/webpack_manifest/manifest.rb, line 20
def lookup_pack_with_chunks!(name, type: nil)
  manifest_pack_type = manifest_type(name, type)
  manifest_pack_name = manifest_name(name, manifest_pack_type)
  find('entrypoints')&.dig(manifest_pack_name, manifest_pack_type) || handle_missing_entry(name)
end

Private Instance Methods

data() click to toggle source
# File lib/webpack_manifest/manifest.rb, line 44
def data
  if cache_enabled?
    @data ||= load_data
  else
    load_data
  end
end
handle_missing_entry(name) click to toggle source
# File lib/webpack_manifest/manifest.rb, line 78
    def handle_missing_entry(name)
      raise MissingEntryError, <<~MSG
        Can not find #{name} in #{@path}.
        Your manifest contains:
        #{JSON.pretty_generate(@data)}
      MSG
    end
load_data() click to toggle source
# File lib/webpack_manifest/manifest.rb, line 52
def load_data
  u = URI.parse(@path)
  data = nil
  if u.scheme == 'file' || u.path == @path  # file path
    raise(FileNotFoundError, "#{@path}: no such manifest found") unless File.exist?(@path)
    data = File.read(@path)
  else
    # http url
    data = u.read
  end
  JSON.parse(data)
end
manifest_name(name, pack_type) click to toggle source

The `manifest_name` method strips of the file extension of the name, because in the manifest hash the entrypoints are defined by their pack name without the extension. When the user provides a name with a file extension, we want to try to strip it off.

# File lib/webpack_manifest/manifest.rb, line 68
def manifest_name(name, pack_type)
  return name if File.extname(name.to_s).empty?
  File.basename(name, '.' + pack_type)
end
manifest_type(name, pack_type) click to toggle source
# File lib/webpack_manifest/manifest.rb, line 73
def manifest_type(name, pack_type)
  return File.extname(name)[1..-1] if pack_type.nil?
  pack_type.to_s
end