Class: MiniviteRails::Manifest
- Inherits:
-
Object
- Object
- MiniviteRails::Manifest
- Defined in:
- lib/minivite_rails/manifest.rb
Overview
Class to load and parse the manifest.json file generated by Vite.
Defined Under Namespace
Classes: FileNotFoundError, MissingEntryError
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#manifest_path ⇒ Object
readonly
Returns the value of attribute manifest_path.
Instance Method Summary collapse
- #data ⇒ Object
-
#initialize(config) ⇒ Manifest
constructor
A new instance of Manifest.
- #path_for(name, **options) ⇒ Object
- #public_path_for(name) ⇒ Object
-
#react_preamble_code ⇒ Object
Public: Source script for the React Refresh plugin.
-
#react_refresh_preamble ⇒ Object
Public: The content of the preamble needed by the React Refresh plugin.
-
#resolve_entries(*names, **options) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity.
- #update_config(config) ⇒ Object
- #vite_client_src ⇒ Object
Constructor Details
#initialize(config) ⇒ Manifest
Returns a new instance of Manifest.
18 19 20 |
# File 'lib/minivite_rails/manifest.rb', line 18 def initialize(config) update_config(config) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/minivite_rails/manifest.rb', line 16 def config @config end |
#manifest_path ⇒ Object (readonly)
Returns the value of attribute manifest_path.
16 17 18 |
# File 'lib/minivite_rails/manifest.rb', line 16 def manifest_path @manifest_path end |
Instance Method Details
#data ⇒ Object
28 29 30 31 32 |
# File 'lib/minivite_rails/manifest.rb', line 28 def data return load_manifest unless config.cache @data ||= load_manifest end |
#path_for(name, **options) ⇒ Object
34 35 36 |
# File 'lib/minivite_rails/manifest.rb', line 34 def path_for(name, **) lookup!(name, **).fetch('file') end |
#public_path_for(name) ⇒ Object
38 39 40 41 42 |
# File 'lib/minivite_rails/manifest.rb', line 38 def public_path_for(name) return prefix_vite_asset(name) if dev_server_available? File.join(config.public_base_path, name) end |
#react_preamble_code ⇒ Object
Public: Source script for the React Refresh plugin.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/minivite_rails/manifest.rb', line 72 def react_preamble_code return unless dev_server_available? <<~REACT_PREAMBLE_CODE import RefreshRuntime from '#{prefix_vite_asset('@react-refresh')}' RefreshRuntime.injectIntoGlobalHook(window) window.$RefreshReg$ = () => {} window.$RefreshSig$ = () => (type) => type window.__vite_plugin_react_preamble_installed__ = true REACT_PREAMBLE_CODE end |
#react_refresh_preamble ⇒ Object
Public: The content of the preamble needed by the React Refresh plugin.
61 62 63 64 65 66 67 68 69 |
# File 'lib/minivite_rails/manifest.rb', line 61 def react_refresh_preamble return unless dev_server_running? <<~REACT_REFRESH <script type="module"> #{react_preamble_code} </script> REACT_REFRESH end |
#resolve_entries(*names, **options) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/minivite_rails/manifest.rb', line 48 def resolve_entries(*names, **) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity entries = names.map { |name| lookup!(name, **) } script_paths = entries.map { |entry| entry.fetch('file') } imports = dev_server_available? ? [] : entries.flat_map { |entry| entry['imports'] }.compact.uniq { scripts: script_paths, imports: imports.map { |entry| entry.fetch('file') }.uniq, stylesheets: dev_server_available? ? [] : (entries + imports).flat_map { |entry| entry['css'] }.compact.uniq } end |
#update_config(config) ⇒ Object
22 23 24 25 26 |
# File 'lib/minivite_rails/manifest.rb', line 22 def update_config(config) @config = config @manifest_path = config.manifest_path || File.join(config.public_asset_dir, '.vite', 'manifest.json') @data = nil end |
#vite_client_src ⇒ Object
44 45 46 |
# File 'lib/minivite_rails/manifest.rb', line 44 def vite_client_src prefix_vite_asset('@vite/client') if dev_server_available? end |