Sha256: 2b4a4128db8e0df4616b55efeedd50a9c2bc09721c817070c34ba92d13d02413

Contents?: true

Size: 1.49 KB

Versions: 25

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true
module ThemeCheck
  # Reports errors when trying to use too much JavaScript on page load
  # Encourages the use of the Import on Interaction pattern [1].
  # [1]: https://addyosmani.com/blog/import-on-interaction/
  class AssetSizeJavaScript < HtmlCheck
    include RegexHelpers
    severity :error
    category :html, :performance
    doc docs_url(__FILE__)

    attr_reader :threshold_in_bytes

    def initialize(threshold_in_bytes: 10000)
      @threshold_in_bytes = threshold_in_bytes
    end

    def on_script(node)
      file_size = src_to_file_size(node.attributes['src'])
      return if file_size.nil?
      return if file_size <= threshold_in_bytes
      add_offense(
        "JavaScript on every page load exceeds compressed size threshold (#{threshold_in_bytes} Bytes), consider using the import on interaction pattern.",
        node: node
      )
    end

    def src_to_file_size(src)
      # We're kind of intentionally only looking at {{ 'asset' | asset_url }} or full urls in here.
      # More complicated liquid statements are not in scope.
      if src =~ /^#{LIQUID_VARIABLE}$/o && src =~ /asset_url/ && src =~ 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
      elsif src =~ %r{^(https?:)?//}
        asset = RemoteAssetFile.from_src(src)
        asset.gzipped_size
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.14.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.13.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.12.1 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.12.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.11.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.10.3 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.10.2 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.10.1 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.10.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.9.2 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.9.1 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.9.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.8.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.7.2 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.7.1 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.7.0 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.6.2 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.6.1 lib/theme_check/checks/asset_size_javascript.rb
theme-check-1.6.0 lib/theme_check/checks/asset_size_javascript.rb