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: 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
42 43 44 |
# File 'lib/shakapacker/manifest.rb', line 42 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.
104 105 106 107 108 |
# File 'lib/shakapacker/manifest.rb', line 104 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
122 123 124 |
# File 'lib/shakapacker/manifest.rb', line 122 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.
68 69 70 71 72 73 74 75 76 |
# File 'lib/shakapacker/manifest.rb', line 68 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
84 85 86 |
# File 'lib/shakapacker/manifest.rb', line 84 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.
52 53 54 |
# File 'lib/shakapacker/manifest.rb', line 52 def refresh @data = load end |