class ViteRuby::Manifest

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/vite_ruby/manifest.rbs

class ViteRuby::Manifest
  def find_manifest_entry: (String name) -> untyped
end

on demand as needed, before performing any lookup.
NOTE: Using ‘“autoBuild”: true` in `config/vite.json` file will trigger a build
=> { “file” => “/vite/assets/calendar-1016838bab065ae1e314.js”, “imports” => [] }
lookup_entrypoint(’calendar’, type: :javascript)
Example:
manifest file which maps entrypoint names to file paths.
Public: Registry for accessing resources managed by Vite, using a generated

def extension_for_type(entry_type)

Internal: Allows to receive :javascript and :stylesheet as :type in helpers.
def extension_for_type(entry_type)
  case entry_type
  when :javascript then 'js'
  when :stylesheet then 'css'
  when :typescript then 'ts'
  else entry_type
  end
end

def find_manifest_entry(name)

Experimental RBS support (using type sampling data from the type_fusion project).

def find_manifest_entry: (String name) -> untyped

This signature was generated using 1 sample from 1 application.

Internal: Finds the specified entry in the manifest.
def find_manifest_entry(name)
  if dev_server_running?
    { 'file' => prefix_vite_asset(name) }
  else
    manifest[name]
  end
end

def initialize(vite_ruby)

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

def load_manifest

Internal: Loads and merges the manifest files, resolving the asset paths.
def load_manifest
  files = config.manifest_paths
  files.map { |path| JSON.parse(path.read) }.inject({}, &:merge).tap(&method(:resolve_references))
end

def lookup(name, **options)

=> { "file" => "/vite/assets/calendar-1016838bab065ae1e122.js", "imports" => [] }
manifest.lookup('calendar.js')
Example:

Returns a relative path, or nil if the asset is not found.

Internal: Computes the path for a given Vite asset using manifest.json.
def lookup(name, **options)
  @build_mutex.synchronize { builder.build || (return nil) } if should_build?
  find_manifest_entry resolve_entry_name(name, **options)
end

def lookup!(name, **options)

Returns a relative path for the asset, or raises an error if not found.

Internal: Strict version of lookup.
def lookup!(name, **options)
  lookup(name, **options) || missing_entry_error(name, **options)
end

def manifest

is reloaded automatically before each lookup, to ensure it's always fresh.
NOTE: When using build-on-demand in development and testing, the manifest

Internal: The parsed data from manifest.json.
def manifest
  return refresh if config.auto_build
  @manifest ||= load_manifest
end

def missing_entry_error(name, **options)

Internal: Raises a detailed message when an entry is missing in the manifest.
def missing_entry_error(name, **options)
  raise ViteRuby::MissingEntrypointError, OpenStruct.new(
    file_name: resolve_entry_name(name, **options),
    last_build: builder.last_build_metadata,
    manifest: @manifest,
    config: config,
  )
end

def path_for(name, **options)

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

Public: Returns the path for the specified Vite entrypoint file.
def path_for(name, **options)
  lookup!(name, **options).fetch('file')
end

def prefix_asset_with_host(path)

the framework tag helpers.
Internal: Prefixes an asset with the `asset_host` for tags that do not use
def prefix_asset_with_host(path)
  File.join(vite_asset_origin || config.asset_host || '/', config.public_output_dir, path)
end

def prefix_vite_asset(path)

Internal: Scopes an asset to the output folder in public, as a path.
def prefix_vite_asset(path)
  File.join(vite_asset_origin || '/', config.public_output_dir, path)
end

def react_preamble_code

Public: Source script for the React Refresh plugin.
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

def react_refresh_preamble

Public: The content of the preamble needed by the React Refresh plugin.
def react_refresh_preamble
  if dev_server_running?
    <<~REACT_REFRESH
      <script type="module">
        #{ react_preamble_code }
      </script>
    REACT_REFRESH
  end
end

def refresh

Public: Refreshes the cached mappings by reading the updated manifest files.
def refresh
  @manifest = load_manifest
end

def resolve_absolute_entry(name)

During develoment, files outside the root must be requested explicitly.
Internal: Entry names in the manifest are relative to the Vite.js.
def resolve_absolute_entry(name)
  if dev_server_running?
    File.join(FS_PREFIX, config.root, name)
  else
    config.root.join(name).relative_path_from(config.vite_root_dir).to_s
  end
end

def resolve_entries(*names, **options)

entrypoint files.
Public: Returns scripts, imported modules, and stylesheets for the specified
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.uniq
  {
    scripts: script_paths,
    imports: imports.map { |entry| entry.fetch('file') }.uniq,
    stylesheets: dev_server_running? ? [] : (entries + imports).flat_map { |entry| entry['css'] }.compact.uniq,
  }
end

def resolve_entry_name(name, type: nil)

Internal: Resolves the manifest entry name for the specified resource.
def resolve_entry_name(name, type: nil)
  return resolve_virtual_entry(name) if type == :virtual
  name = with_file_extension(name.to_s, type)
  raise ArgumentError, "Asset names can not be relative. Found: #{ name }" if name.start_with?('.')
  # Explicit path, relative to the source_code_dir.
  name.sub(%r{^~/(.+)$}) { return Regexp.last_match(1) }
  # Explicit path, relative to the project root.
  name.sub(%r{^/(.+)$}) { return resolve_absolute_entry(Regexp.last_match(1)) }
  # Sugar: Prefix with the entrypoints dir if the path is not nested.
  name.include?('/') ? name : File.join(config.entrypoints_dir, name)
end

def resolve_references(manifest)

Internal: Resolves the paths that reference a manifest entry.
def resolve_references(manifest)
  manifest.each_value do |entry|
    entry['file'] = prefix_vite_asset(entry['file'])
    %w[css assets].each do |key|
      entry[key] = entry[key].map { |path| prefix_vite_asset(path) } if entry[key]
    end
    entry['imports']&.map! { |name| manifest.fetch(name) }
  end
end

def resolve_virtual_entry(name)

Internal: Resolves a virtual entry by walking all the manifest keys.
def resolve_virtual_entry(name)
  manifest.keys.find { |file| file.include?(name) } || name
end

def should_build?

won't focus on the frontend, or when running the Vite server is not desired.
NOTE: Auto compilation is convenient when running tests, when the developer
def should_build?
  config.auto_build && !dev_server_running?
end

def vite_asset_origin

Internal: The origin of assets managed by Vite.
def vite_asset_origin
  config.origin if dev_server_running? && config.skip_proxy
end

def vite_client_src

Public: The path from where the browser can download the Vite HMR client.
def vite_client_src
  prefix_asset_with_host('@vite/client') if dev_server_running?
end

def with_file_extension(name, entry_type)

Internal: Adds a file extension to the file name, unless it already has one.
def with_file_extension(name, entry_type)
  if File.extname(name).empty? && (ext = extension_for_type(entry_type))
    "#{ name }.#{ ext }"
  else
    name
  end
end