Class: Bridgetown::Tags::AssetPath

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/tags/asset_path.rb

Overview

A helper class to help find the path to assets inside of an esbuild manifest file.

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, asset_type, options) ⇒ void

Parameters:

  • tag_name (String)

    Name of the tag

  • asset_type (String)

    The type of asset to parse (js, css)

  • options (Hash)

    An options hash

See Also:



13
14
15
16
17
18
# File 'bridgetown-core/lib/bridgetown-core/tags/asset_path.rb', line 13

def initialize(tag_name, asset_type, options)
  super

  # js or css
  @asset_type = asset_type.strip
end

Instance Method Details

#render(context) ⇒ String

Render an asset path based on the frontend manifest file

Parameters:

  • context (Liquid::Context)

    Context passed to the tag

Returns:

  • (String)

    Returns “MISSING_ESBUILD_MANIFEST” if the manifest file isn’t found

  • (String)

    Returns a blank string if the asset isn’t found

  • (String)

    Returns the path to the asset if no issues parsing



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'bridgetown-core/lib/bridgetown-core/tags/asset_path.rb', line 27

def render(context)
  @context = context
  site = context.registers[:site]
  if tag_name == "webpack_path"
    source_file =
      "#{context.registers.static[:file_system].root.last}/#{context.template_name}.liquid"
    raise(
      Bridgetown::Errors::FatalException,
      "🚨 Oops, you'll need to change `webpack_path' to `asset_path' in:\n#{source_file}\n"
    )
  end

  Bridgetown::Utils.parse_frontend_manifest_file(site, @asset_type) || ""
end