Class: Shakapacker::Manifest
- Inherits:
-
Object
- Object
- Shakapacker::Manifest
- 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.
Defined Under Namespace
Classes: LoadResult, MissingEntryError
Instance Method Summary collapse
-
#initialize(instance) ⇒ Shakapacker::Manifest
constructor
Creates a new manifest instance.
-
#lookup(name, pack_type = {}) ⇒ String?
Looks up the compiled path for a given asset.
-
#lookup!(name, pack_type = {}) ⇒ String
Like #lookup, but raises an error if the asset is not found.
-
#lookup_pack_with_chunks(name, pack_type = {}) ⇒ Array<String>?
Looks up an entry point with all its chunks (split code).
-
#lookup_pack_with_chunks!(name, pack_type = {}) ⇒ Array<String>
Like #lookup_pack_with_chunks, but raises an error if not found.
-
#refresh ⇒ Hash
Reloads the manifest data from disk.
Constructor Details
#initialize(instance) ⇒ Shakapacker::Manifest
Creates a new manifest instance
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.
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
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.
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
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 |
#refresh ⇒ Hash
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.
56 57 58 59 |
# File 'lib/shakapacker/manifest.rb', line 56 def refresh @load_result = load @load_result.data end |