class RailsVite::Manifest

Attributes

public_manifest_path[R]
rails_vite[R]

Public Class Methods

new(rails_vite) click to toggle source
# File lib/rails_vite/manifest.rb, line 15
def initialize(rails_vite)
  @rails_vite = rails_vite
  @public_manifest_path = rails_vite.config.public_manifest_path
end

Public Instance Methods

compile() click to toggle source
# File lib/rails_vite/manifest.rb, line 70
def compile
  RailsVite.logger.tagged('RailsVite') { compiler.compile }
end
compiling?() click to toggle source
# File lib/rails_vite/manifest.rb, line 66
def compiling?
  config.compile? && !dev_server.running?
end
data() click to toggle source
# File lib/rails_vite/manifest.rb, line 74
def data
  if config.cache_manifest?
    @data ||= load
  else
    refresh
  end
end
exist?() click to toggle source
# File lib/rails_vite/manifest.rb, line 108
def exist?
  public_manifest_path.exist?
end
find(name) click to toggle source
# File lib/rails_vite/manifest.rb, line 82
def find(name)
  data[name.to_s].presence
end
find_css(name) click to toggle source
# File lib/rails_vite/manifest.rb, line 86
def find_css(name)
  r = find(name)
  Array(r['css']).map(&->(i){ "/#{i}" })
end
full_pack_name(name, pack_type) click to toggle source
# File lib/rails_vite/manifest.rb, line 91
def full_pack_name(name, pack_type)
  return name unless File.extname(name.to_s).empty?
  "#{name}.#{manifest_type(pack_type)}"
end
handle_missing_entry(name, pack_type) click to toggle source
# File lib/rails_vite/manifest.rb, line 96
def handle_missing_entry(name, pack_type)
  raise Manifest::MissingEntryError, missing_file_from_manifest_error(full_pack_name(name, pack_type[:type]))
end
load() click to toggle source
# File lib/rails_vite/manifest.rb, line 100
def load
  if exist?
    JSON.parse public_manifest_path.read
  else
    {}
  end
end
lookup(name, pack_type = {}) click to toggle source

Computes the relative path for a given RailsVite asset using manifest.json. If no asset is found, returns nil.

Example:

RailsVite.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
# File lib/rails_vite/manifest.rb, line 44
def lookup(name, pack_type = {})
  compile if compiling?

  find(full_pack_name(name, pack_type[:type]))
end
lookup!(name, pack_type = {}) click to toggle source

Like lookup, except that if no asset is found, raises a RailsVite::Manifest::MissingEntryError.

# File lib/rails_vite/manifest.rb, line 51
def lookup!(name, pack_type = {})
  lookup(name, pack_type) || handle_missing_entry(name, pack_type)
end
lookup_by_path(path) click to toggle source
# File lib/rails_vite/manifest.rb, line 55
def lookup_by_path(path)
  relative_path = path.relative_path_from config.source_path
  r = find(relative_path)

  if r.is_a? Hash
    r['file']
  elsif path.exist?
    "@fs#{path.to_s}"
  end
end
lookup_pack_with_chunks(name, pack_type = {}) click to toggle source
# File lib/rails_vite/manifest.rb, line 24
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 = {}) click to toggle source
# File lib/rails_vite/manifest.rb, line 34
def lookup_pack_with_chunks!(name, pack_type = {})
  lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name, pack_type)
end
manifest_name(name, pack_type) click to toggle source

The ‘manifest_name` method strips of the file extension of the name, because in the manifest hash the entrypoints are defined by their pack name without the extension. When the user provides a name with a file extension, we want to try to strip it off.

# File lib/rails_vite/manifest.rb, line 115
def manifest_name(name, pack_type)
  return name if File.extname(name.to_s).empty?
  File.basename(name, ".#{pack_type}")
end
manifest_type(pack_type) click to toggle source
# File lib/rails_vite/manifest.rb, line 120
def manifest_type(pack_type)
  case pack_type
  when :javascript then 'js'
  when :stylesheet then 'css'
  else pack_type.to_s
  end
end
refresh() click to toggle source
# File lib/rails_vite/manifest.rb, line 20
def refresh
  @data = load
end