class Webpacker::Manifest

Singleton registry for accessing the packs path using a generated manifest. This allows javascript_pack_tag, stylesheet_pack_tag, asset_pack_path to take a reference to, say, “calendar.js” or “calendar.css” and turn it into “/packs/calendar-1016838bab065ae1e314.js” or “/packs/calendar-1016838bab065ae1e314.css”.

When the configuration is set to on-demand compilation, with the ‘compile: true` option in the webpacker.yml file, any lookups will be preceded by a compilation if one is needed.

Public Class Methods

new(webpacker) click to toggle source
# File lib/webpacker/manifest.rb, line 13
def initialize(webpacker)
  @webpacker = webpacker
end

Public Instance Methods

lookup(name, pack_type = {}) click to toggle source

Computes the relative path for a given Webpacker asset using manifest.json. If no asset is found, returns nil.

Example:

Webpacker.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
# File lib/webpacker/manifest.rb, line 41
def lookup(name, pack_type = {})
  compile if compiling?

  find(full_pack_name(name, pack_type[:type]))
end
lookup!(name, pack_type = {}) click to toggle source

Like lookup, except that if no asset is found, raises a Webpacker::Manifest::MissingEntryError.

# File lib/webpacker/manifest.rb, line 48
def lookup!(name, pack_type = {})
  lookup(name, pack_type) || handle_missing_entry(name, pack_type)
end
lookup_pack_with_chunks(name, pack_type = {}) click to toggle source
# File lib/webpacker/manifest.rb, line 21
def lookup_pack_with_chunks(name, pack_type = {})
  compile if compiling?

  manifest_pack_type = manifest_type(pack_type[:type])
  manifest_pack_name = manifest_name(name, manifest_pack_type)
  find("entrypoints")[manifest_pack_name]["assets"][manifest_pack_type]
rescue NoMethodError
  nil
end
lookup_pack_with_chunks!(name, pack_type = {}) click to toggle source
# File lib/webpacker/manifest.rb, line 31
def lookup_pack_with_chunks!(name, pack_type = {})
  lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name, pack_type)
end
refresh() click to toggle source
# File lib/webpacker/manifest.rb, line 17
def refresh
  @data = load
end

Private Instance Methods

compile() click to toggle source
# File lib/webpacker/manifest.rb, line 57
def compile
  Webpacker.logger.tagged("Webpacker") { compiler.compile }
end
compiling?() click to toggle source
# File lib/webpacker/manifest.rb, line 53
def compiling?
  config.compile? && !dev_server.running?
end
data() click to toggle source
# File lib/webpacker/manifest.rb, line 61
def data
  if config.cache_manifest?
    @data ||= load
  else
    refresh
  end
end
find(name) click to toggle source
# File lib/webpacker/manifest.rb, line 69
def find(name)
  data[name.to_s].presence
end
full_pack_name(name, pack_type) click to toggle source
# File lib/webpacker/manifest.rb, line 73
def full_pack_name(name, pack_type)
  return name unless File.extname(name.to_s).empty?
  "#{name}.#{manifest_type(pack_type)}"
end
handle_missing_entry(name, pack_type) click to toggle source
# File lib/webpacker/manifest.rb, line 78
def handle_missing_entry(name, pack_type)
  raise Webpacker::Manifest::MissingEntryError, missing_file_from_manifest_error(full_pack_name(name, pack_type[:type]))
end
load() click to toggle source
# File lib/webpacker/manifest.rb, line 82
def load
  if config.manifest_path.exist?
    JSON.parse config.manifest_path.read
  else
    {}
  end
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/webpacker/manifest.rb, line 93
def manifest_name(name, pack_type)
  name.chomp(".#{pack_type}")
end
manifest_type(pack_type) click to toggle source
# File lib/webpacker/manifest.rb, line 97
def manifest_type(pack_type)
  case pack_type
  when :javascript then "js"
  when :stylesheet then "css"
  else pack_type.to_s
  end
end
missing_file_from_manifest_error(bundle_name) click to toggle source
# File lib/webpacker/manifest.rb, line 105
    def missing_file_from_manifest_error(bundle_name)
      <<-MSG
Webpacker can't find #{bundle_name} in #{config.manifest_path}. Possible causes:
1. You forgot to install node packages (try `yarn install`) or are running an incompatible version of Node
2. Your app has code with a non-standard extension (like a `.jsx` file) but the extension is not in the `extensions` config in `config/webpacker.yml`
3. You have set compile: false (see `config/webpacker.yml`) for this environment
   (unless you are using the `bin/webpacker -w` or the `bin/webpacker-dev-server`, in which case maybe you aren't running the dev server in the background?)
4. webpack has not yet re-run to reflect updates.
5. You have misconfigured Webpacker's `config/webpacker.yml` file.
6. Your webpack configuration is not creating a manifest.

Your manifest contains:
#{JSON.pretty_generate(@data)}
      MSG
    end