class Jekyll::Vite::Tag

Internal: Base class for all tags.

Public Instance Methods

render(context) { |: raise(NotImplementedError, "Implement render in #{ class.name }")| ... } click to toggle source

Override: Set the context to make the site available in the URLFilters.

# File lib/jekyll/vite/tags.rb, line 8
def render(context)
  @context = context
  @params = @params.is_a?(String) ? parse_params(context).transform_keys(&:to_sym) : @params || {}
  if @file = render_variable(@file)
    validate_file_name(@file)
    track_file_dependency(@file)
  end
  block_given? ? yield : raise(NotImplementedError, "Implement render in #{ self.class.name }")
end
render_variable(variable) click to toggle source

Override: Modified version that can resolve recursive references.

# File lib/jekyll/vite/tags.rb, line 19
def render_variable(variable)
  variable = Liquid::Template.parse(variable).render!(@context) while VARIABLE_SYNTAX =~ variable
  variable
end

Protected Instance Methods

last_build_metadata_path() click to toggle source

Internal: Adding the last build metadata file as a dependency ensures all pages using Vite assets are regenerated if a Vite build is triggered.

# File lib/jekyll/vite/tags.rb, line 75
def last_build_metadata_path
  ViteRuby.instance.builder.send(:last_build_path)
end
script_tags(sources, **attrs) click to toggle source

Internal: Renders HTML script tags.

# File lib/jekyll/vite/tags.rb, line 54
def script_tags(sources, **attrs)
  sources.map { |src| tag(:script, src: src, **attrs) }.join("\n")
end
stringify_attrs(**attrs) click to toggle source

Internal: Renders HTML attributes inside a tag.

# File lib/jekyll/vite/tags.rb, line 37
def stringify_attrs(**attrs)
  attrs.map { |key, value| %(#{ key }="#{ value }") }.join(' ')
end
tag(type, **attrs) click to toggle source

Internal: Renders an HTML tag of the specified type.

# File lib/jekyll/vite/tags.rb, line 42
def tag(type, **attrs)
  self_closing = type != :script
  %i[href src].each { |key| attrs[key] = relative_url(attrs[key]) if attrs.key?(key) }
  ["<#{ type } ", stringify_attrs(**attrs), self_closing ? '/>' : "></#{ type }>"].join
end
track_file_dependency(name) click to toggle source

Internal: Adds entrypoint files managed by Vite as a dependency in the renegerator in order to support –incremental mode.

# File lib/jekyll/vite/tags.rb, line 60
def track_file_dependency(name)
  site = @context.registers[:site]
  path = site.in_source_dir(File.join(ViteRuby.config.source_code_dir, ViteRuby.config.entrypoints_dir, name))

  ['', '.css', '.js', '.ts'].each do |ext|
    if File.file?(asset_path = "#{ path }#{ ext }")
      return [asset_path, last_build_metadata_path].each do |filename|
        add_include_to_dependency(site, filename.to_s, @context)
      end
    end
  end
end
vite_asset_path(name, **options) click to toggle source

Internal: Resolves the path for the specified Vite asset.

# File lib/jekyll/vite/tags.rb, line 27
def vite_asset_path(name, **options)
  vite_manifest.path_for(name, **options)
end
vite_manifest() click to toggle source

Internal: Returns the current manifest loaded by Vite Ruby.

# File lib/jekyll/vite/tags.rb, line 32
def vite_manifest
  ViteRuby.instance.manifest
end