class Viter::Manifest

Attributes

viter[R]

Public Class Methods

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

Public Instance Methods

compile() click to toggle source
# File lib/viter/manifest.rb, line 64
def compile
  Viter.logger.tagged('Viter') { compiler.compile }
end
compiling?() click to toggle source
# File lib/viter/manifest.rb, line 60
def compiling?
  config.compile? && !dev_server.running?
end
data() click to toggle source
# File lib/viter/manifest.rb, line 68
def data
  if config.cache_manifest?
    @data ||= load
  else
    refresh
  end
end
find(name) click to toggle source
# File lib/viter/manifest.rb, line 76
def find(name)
  data[name.to_s].presence
end
find_css(name) click to toggle source
# File lib/viter/manifest.rb, line 80
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/viter/manifest.rb, line 85
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/viter/manifest.rb, line 90
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/viter/manifest.rb, line 94
def load
  if config.public_manifest_path.exist?
    JSON.parse config.public_manifest_path.read
  else
    {}
  end
end
lookup(name, pack_type = {}) click to toggle source

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

Example:

Viter.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
# File lib/viter/manifest.rb, line 43
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 Webpacker::Manifest::MissingEntryError.

# File lib/viter/manifest.rb, line 50
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/viter/manifest.rb, line 54
def lookup_by_path(path)
  relative_path = path.relative_path_from config.source_path

  find(relative_path)
end
lookup_pack_with_chunks(name, pack_type = {}) click to toggle source
# File lib/viter/manifest.rb, line 23
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/viter/manifest.rb, line 33
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/viter/manifest.rb, line 105
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/viter/manifest.rb, line 110
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/viter/manifest.rb, line 19
def refresh
  @data = load
end