Sha256: 9240a83fd66ec8ded84e3db338bdb05a61a4c939c6efd73cb31dad6d8df0aefc

Contents?: true

Size: 1.17 KB

Versions: 15

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Tags
    class WebpackPath < Liquid::Tag
      include Bridgetown::Filters::URLFilters

      def initialize(tag_name, asset_type, tokens)
        super

        # js or css
        @asset_type = asset_type.strip
      end

      def render(context)
        @context = context
        site = context.registers[:site]

        frontend_path = relative_url("_bridgetown/static")

        manifest_file = site.in_root_dir(".bridgetown-webpack", "manifest.json")
        if File.exist?(manifest_file)
          manifest = JSON.parse(File.read(manifest_file))
          if @asset_type == "js"
            js_path = manifest["main.js"].split("/").last
            [frontend_path, "js", js_path].join("/")
          elsif @asset_type == "css"
            css_path = manifest["main.css"].split("/").last
            [frontend_path, "css", css_path].join("/")
          else
            Bridgetown.logger.error("Unknown Webpack asset type", @asset_type)
            nil
          end
        else
          "MISSING_WEBPACK_MANIFEST"
        end
      end
    end
  end
end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
bridgetown-core-0.15.0 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.15.0.beta4 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.15.0.beta3 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.15.0.beta2 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.15.0.beta1 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.14.1 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.14.0 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.13.0 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.12.1 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.12.0 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.11.2 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.11.1 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.11.0 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.10.2 lib/bridgetown-core/tags/webpack_path.rb
bridgetown-core-0.10.1 lib/bridgetown-core/tags/webpack_path.rb