Class: Shakapacker::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/shakapacker/manifest.rb

Overview

Manifest for looking up compiled asset paths

The manifest reads the manifest.json file produced by webpack/rspack during compilation and provides methods to look up the compiled (digested) paths for source files.

This allows view helpers like javascript_pack_tag, stylesheet_pack_tag, and asset_pack_path to take a reference to a source file (e.g., “calendar.js”) and turn it into the compiled path with digest (e.g., “/packs/calendar-1016838bab065ae1e314.js”).

Automatic Compilation

When the configuration has compile: true in shakapacker.yml, any lookups will automatically trigger compilation if the assets are stale. This is typically enabled in development and disabled in production.

Caching

The manifest can cache the loaded data in memory when cache_manifest: true is set in the configuration. This improves performance in production by avoiding repeated file reads.

Examples:

Looking up assets

manifest = Shakapacker.manifest
manifest.lookup("application.js")
#=> "/packs/application-abc123.js"

manifest.lookup!("missing.js")
#=> raises Shakapacker::Manifest::MissingEntryError

See Also:

Defined Under Namespace

Classes: LoadResult, MissingEntryError

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Shakapacker::Manifest

Creates a new manifest instance

Parameters:



46
47
48
# File 'lib/shakapacker/manifest.rb', line 46

def initialize(instance)
  @instance = instance
end

Instance Method Details

#lookup(name, pack_type = {}) ⇒ String?

Looks up the compiled path for a given asset

Computes the relative path for a Shakapacker asset using the manifest.json file. If automatic compilation is enabled and the assets are stale, triggers a compilation before looking up the path.

Examples:

Shakapacker.manifest.lookup('calendar.js')
#=> "/packs/calendar-1016838bab065ae1e122.js"

Shakapacker.manifest.lookup('calendar', type: :javascript)
#=> "/packs/calendar-1016838bab065ae1e122.js"

Parameters:

  • name (String)

    the source file name (e.g., “calendar.js” or “calendar”)

  • pack_type (Hash) (defaults to: {})

    options hash with :type key (:javascript, :stylesheet, etc.). If not specified, the extension from the name is used.

Returns:

  • (String, nil)

    the compiled asset path, or nil if not found



109
110
111
112
113
# File 'lib/shakapacker/manifest.rb', line 109

def lookup(name, pack_type = {})
  compile if compiling?

  find(full_pack_name(name, pack_type[:type]))
end

#lookup!(name, pack_type = {}) ⇒ String

Like #lookup, but raises an error if the asset is not found

Examples:

Shakapacker.manifest.lookup!('calendar.js')
#=> "/packs/calendar-1016838bab065ae1e122.js"

Shakapacker.manifest.lookup!('missing.js')
#=> raises MissingEntryError

Parameters:

  • name (String)

    the source file name

  • pack_type (Hash) (defaults to: {})

    options hash with :type key

Returns:

  • (String)

    the compiled asset path

Raises:



127
128
129
# File 'lib/shakapacker/manifest.rb', line 127

def lookup!(name, pack_type = {})
  lookup(name, pack_type) || handle_missing_entry(name, pack_type)
end

#lookup_pack_with_chunks(name, pack_type = {}) ⇒ Array<String>?

Looks up an entry point with all its chunks (split code)

This method is used when you need to load all chunks for a pack that has been split via code splitting. It returns an array of asset paths for the main entry and all its dynamic imports.

Examples:

manifest.lookup_pack_with_chunks("application", type: :javascript)
#=> ["/packs/runtime-abc123.js", "/packs/application-def456.js"]

Parameters:

  • name (String)

    the entry point name (e.g., “application”)

  • pack_type (Hash) (defaults to: {})

    options hash with :type key (:javascript, :stylesheet, etc.)

Returns:

  • (Array<String>, nil)

    array of asset paths, or nil if not found



73
74
75
76
77
78
79
80
81
# File 'lib/shakapacker/manifest.rb', line 73

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 = {}) ⇒ Array<String>

Like #lookup_pack_with_chunks, but raises an error if not found

Parameters:

  • name (String)

    the entry point name

  • pack_type (Hash) (defaults to: {})

    options hash with :type key

Returns:

  • (Array<String>)

    array of asset paths

Raises:



89
90
91
# File 'lib/shakapacker/manifest.rb', line 89

def lookup_pack_with_chunks!(name, pack_type = {})
  lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name, pack_type)
end

#refreshHash

Reloads the manifest data from disk

Forces a fresh read of the manifest.json file, bypassing any cache. This is useful when you know the manifest has been updated.

Returns:

  • (Hash)

    the loaded manifest data



56
57
58
59
# File 'lib/shakapacker/manifest.rb', line 56

def refresh
  @load_result = load
  @load_result.data
end