Sha256: 0de0980d44f445053b56ad6eeaff573aff6cf583b57d4f51f38e1cd40b28dbd1

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  class AssetSizeCSSStylesheetTag < LiquidCheck
    include RegexHelpers
    severity :error
    category :liquid, :performance
    doc docs_url(__FILE__)

    def initialize(threshold_in_bytes: 100_000)
      @threshold_in_bytes = threshold_in_bytes
    end

    def on_variable(node)
      used_filters = node.filters.map { |name, *_rest| name }
      return unless used_filters.include?("stylesheet_tag")
      file_size = stylesheet_tag_pipeline_to_file_size(node.markup)
      return if file_size.nil?
      return if file_size <= @threshold_in_bytes
      add_offense(
        "CSS on every page load exceeding compressed size threshold (#{@threshold_in_bytes} Bytes).",
        node: node
      )
    end

    def stylesheet_tag_pipeline_to_file_size(href)
      # asset_url
      if href =~ /asset_url/ && href =~ Liquid::QuotedString
        asset_id = Regexp.last_match(0).gsub(START_OR_END_QUOTE, "")
        asset = @theme.assets.find { |a| a.name.end_with?("/" + asset_id) }
        return if asset.nil?
        asset.gzipped_size

      # remote URLs
      elsif href =~ %r{(https?:)?//[^'"]+}
        url = Regexp.last_match(0)
        asset = RemoteAssetFile.from_src(url)
        asset.gzipped_size
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.14.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.13.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.12.1 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.12.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.11.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.10.3 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.10.2 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.10.1 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.10.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb