Class: ViteRuby::Manifest

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vite_ruby/manifest.rb

Overview

Public: Registry for accessing resources managed by Vite, using a generated manifest file which maps entrypoint names to file paths.

Example:

lookup_entrypoint('calendar', type: :javascript)
=> { "file" => "/vite/assets/calendar-1016838bab065ae1e314.js", "imports" => [] }

NOTE: Using ‘“autoBuild”: true` in `config/vite.json` file will trigger a build on demand as needed, before performing any lookup.

Instance Method Summary collapse

Constructor Details

#initialize(vite_ruby) ⇒ Manifest

Returns a new instance of Manifest.



13
14
15
16
# File 'lib/vite_ruby/manifest.rb', line 13

def initialize(vite_ruby)
  @vite_ruby = vite_ruby
  @build_mutex = Mutex.new if config.auto_build
end

Instance Method Details

#path_for(name, **options) ⇒ Object

Public: Returns the path for the specified Vite entrypoint file.

Raises an error if the resource could not be found in the manifest.



21
22
23
# File 'lib/vite_ruby/manifest.rb', line 21

def path_for(name, **options)
  lookup!(name, **options).fetch("file")
end

#react_preamble_codeObject

Public: Source script for the React Refresh plugin.



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vite_ruby/manifest.rb', line 61

def react_preamble_code
  if dev_server_running?
    <<~REACT_PREAMBLE_CODE
      import RefreshRuntime from '#{prefix_asset_with_host("@react-refresh")}'
      RefreshRuntime.injectIntoGlobalHook(window)
      window.$RefreshReg$ = () => {}
      window.$RefreshSig$ = () => (type) => type
      window.__vite_plugin_react_preamble_installed__ = true
    REACT_PREAMBLE_CODE
  end
end

#react_refresh_preambleObject

Public: The content of the preamble needed by the React Refresh plugin.



50
51
52
53
54
55
56
57
58
# File 'lib/vite_ruby/manifest.rb', line 50

def react_refresh_preamble
  if dev_server_running?
    <<~REACT_REFRESH
      <script type="module">
        #{react_preamble_code}
      </script>
    REACT_REFRESH
  end
end

#refreshObject

Public: Refreshes the cached mappings by reading the updated manifest files.



40
41
42
# File 'lib/vite_ruby/manifest.rb', line 40

def refresh
  @manifest = load_manifest
end

#resolve_entries(*names, **options) ⇒ Object

Public: Returns scripts, imported modules, and stylesheets for the specified entrypoint files.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vite_ruby/manifest.rb', line 27

def resolve_entries(*names, **options)
  entries = names.map { |name| lookup!(name, **options) }
  script_paths = entries.map { |entry| entry.fetch("file") }

  imports = dev_server_running? ? [] : entries.flat_map { |entry| entry["imports"] }.compact
  {
    scripts: script_paths,
    imports: imports.filter_map { |entry| entry.fetch("file") }.uniq,
    stylesheets: dev_server_running? ? [] : (entries + imports).flat_map { |entry| entry["css"] }.compact.uniq,
  }
end

#vite_client_srcObject

Public: The path from where the browser can download the Vite HMR client.



45
46
47
# File 'lib/vite_ruby/manifest.rb', line 45

def vite_client_src
  prefix_asset_with_host("@vite/client") if dev_server_running?
end