Sha256: 997db13e59b2033bc327484515abb32183edf1a49b24030dc5b1b422b46ec46b

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Tags
    # A helper class to help find the path to assets inside of an esbuild
    # manifest file.
    class AssetPath < Liquid::Tag
      # @param tag_name [String] Name of the tag
      # @param asset_type [String] The type of asset to parse (js, css)
      # @param options [Hash] An options hash
      # @return [void]
      # @see {https://www.rdoc.info/github/Shopify/liquid/Liquid/Tag#initialize-instance_method}
      def initialize(tag_name, asset_type, options)
        super

        # js or css
        @asset_type = asset_type.strip
      end

      # Render an asset path based on the frontend manifest file
      # @param context [Liquid::Context] Context passed to the tag
      #
      # @return [String] Returns "MISSING_ESBUILD_MANIFEST" if the manifest
      #   file isn't found
      # @return [String] Returns a blank string if the asset isn't found
      # @return [String] Returns the path to the asset if no issues parsing
      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
    end
  end
end

Liquid::Template.register_tag("asset_path", Bridgetown::Tags::AssetPath)
Liquid::Template.register_tag("webpack_path", Bridgetown::Tags::AssetPath)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta3 lib/bridgetown-core/tags/asset_path.rb
bridgetown-core-2.0.0.beta2 lib/bridgetown-core/tags/asset_path.rb
bridgetown-core-2.0.0.beta1 lib/bridgetown-core/tags/asset_path.rb