Sha256: 3c2d897d7074ae8be095071e36d7fdffc246f265bdad3c3a047ebc5cd3b0c2c2

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 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.value.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.9.2 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.9.1 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.9.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.8.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.7.2 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.7.1 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.7.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.6.2 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.6.1 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
theme-check-1.6.0 lib/theme_check/checks/asset_size_css_stylesheet_tag.rb