Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Last active February 16, 2023 10:37
Show Gist options
  • Select an option

  • Save olivierlacan/92d4706d97ff35176b8982b3aaa034c3 to your computer and use it in GitHub Desktop.

Select an option

Save olivierlacan/92d4706d97ff35176b8982b3aaa034c3 to your computer and use it in GitHub Desktop.

Revisions

  1. olivierlacan revised this gist Jul 10, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions assets.rake
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@
    # `stylesheet_link_tag`. Next are examples of custom methods we use at
    # Code School with the content_for method in order to include JS/CSS in
    # specific location such as the <head> tag in this instance.
    JS_TAGS = ["javascript_include_tag", "included_javascript_for_head"]
    CSS_TAGS = ["stylesheet_link_tag", "linked_stylesheet_for_head"]
    JS_TAGS = %w[ javascript_include_tag included_javascript_for_head ]
    CSS_TAGS = %w[ stylesheet_link_tag linked_stylesheet_for_head ]

    # This task will first run `git grep` to find any instance in your git repo
    # of the JS_TAGS and CSS_TAGS. It will then consult the Sprockets manifest and
  2. olivierlacan revised this gist Jul 10, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions assets.rake
    Original file line number Diff line number Diff line change
    @@ -32,11 +32,15 @@ namespace :assets do
    if missing_js_precompiles.present?
    puts "Missing JavaScript precompiles: "
    puts missing_js_precompiles
    else
    puts "No missing JavaScript precompiles found."
    end

    if missing_css_precompiles.present?
    puts "Missing CSS precompiles: "
    puts missing_css_precompiles
    else
    puts "No missing CSS precompiles found."
    end
    end

  3. olivierlacan revised this gist Jul 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion assets.rake
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ namespace :assets do
    end

    if missing_css_precompiles.present?
    puts "Missing JavaScript precompiles: "
    puts "Missing CSS precompiles: "
    puts missing_css_precompiles
    end
    end
  4. olivierlacan revised this gist Jul 9, 2016. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions assets.rake
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,11 @@ namespace :assets do
    end

    def manifest
    JSON.parse File.read(sprockets_manifest_path)
    if sprockets_manifest_path.present?
    JSON.parse File.read(sprockets_manifest_path)
    else
    raise "No Sprockets Manifest Found: you need to run `bundle exec rake assets:precompile` first."
    end
    end

    def logical_paths
    @@ -77,7 +81,7 @@ namespace :assets do
    script = script.scan(/'(.*)'/).flatten.first

    next if script.nil?

    results[file] = script
    end

  5. olivierlacan revised this gist Jul 9, 2016. 1 changed file with 7 additions and 13 deletions.
    20 changes: 7 additions & 13 deletions assets.rake
    Original file line number Diff line number Diff line change
    @@ -48,12 +48,8 @@ namespace :assets do
    JSON.parse File.read(sprockets_manifest_path)
    end

    def manifest_files
    manifest["files"]
    end

    def logical_paths
    manifest_files.map { |script| script.last["logical_path"] }
    manifest["files"].map { |script| script.last["logical_path"] }
    end

    def check_for_precompiled(paths)
    @@ -64,9 +60,8 @@ namespace :assets do
    end

    def exclusion_prefixes
    [
    "//" # protocol-less path means non-local
    ]
    # // means protocol-less file which means non-local
    %w[ // ]
    end

    def grep(method)
    @@ -76,18 +71,17 @@ namespace :assets do
    def fetch_references(type)
    method_names = (type == :js ? JS_TAGS : CSS_TAGS)

    results = {}

    method_names.each do |method_name|
    method_names.inject({}) do |results, method_name|
    grep(method_name).split("\n").each do |result|
    file, script = result.split(":")
    script = script.scan(/'(.*)'/).flatten.first

    next if script.nil?

    results[file] = script
    end
    end

    results
    results
    end
    end
    end
  6. olivierlacan created this gist Jul 9, 2016.
    93 changes: 93 additions & 0 deletions assets.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    # These instance variables allow you to define which tags we should check
    # to find references to JavaScript and CSS files.
    #
    # Included first are the Rails defaults `javascript_include_tag` and
    # `stylesheet_link_tag`. Next are examples of custom methods we use at
    # Code School with the content_for method in order to include JS/CSS in
    # specific location such as the <head> tag in this instance.
    JS_TAGS = ["javascript_include_tag", "included_javascript_for_head"]
    CSS_TAGS = ["stylesheet_link_tag", "linked_stylesheet_for_head"]

    # This task will first run `git grep` to find any instance in your git repo
    # of the JS_TAGS and CSS_TAGS. It will then consult the Sprockets manifest and
    # check for matching logical paths for the files references by your JavaScript
    # and CSS tags. If the task can't find a mention of the assets you reference
    # in your tags within the Sprockets manifest, you will see them listed as
    # output. You should double-check that these references correspond to files
    # that are added to config.assets.precompile.
    #
    # It's likely that these files are not being properly precompiled which means
    # they're either going to 404 in production (if you have config.assets.compile
    # set to false) or cause a slow on-demand compilation (if you have
    # config.assets.compile set to true)
    namespace :assets do
    desc 'Check for non-precompiled assets'
    task :missing_precompile => :environment do
    javascripts = fetch_references(:js)
    stylesheets = fetch_references(:css)

    missing_js_precompiles = check_for_precompiled(javascripts)
    missing_css_precompiles = check_for_precompiled(stylesheets)

    if missing_js_precompiles.present?
    puts "Missing JavaScript precompiles: "
    puts missing_js_precompiles
    end

    if missing_css_precompiles.present?
    puts "Missing JavaScript precompiles: "
    puts missing_css_precompiles
    end
    end

    def sprockets_manifest_path
    Dir.glob("public/assets/.sprockets-manifest-*.json").first
    end

    def manifest
    JSON.parse File.read(sprockets_manifest_path)
    end

    def manifest_files
    manifest["files"]
    end

    def logical_paths
    manifest_files.map { |script| script.last["logical_path"] }
    end

    def check_for_precompiled(paths)
    paths.values.uniq.reject do |script_path|
    script_path.start_with?(*exclusion_prefixes) or
    logical_paths.any? { |path| path.include?(script_path) }
    end
    end

    def exclusion_prefixes
    [
    "//" # protocol-less path means non-local
    ]
    end

    def grep(method)
    %x{ git grep #{method} -- `git ls-files | grep app/views` }
    end

    def fetch_references(type)
    method_names = (type == :js ? JS_TAGS : CSS_TAGS)

    results = {}

    method_names.each do |method_name|
    grep(method_name).split("\n").each do |result|
    file, script = result.split(":")
    script = script.scan(/'(.*)'/).flatten.first

    next if script.nil?
    results[file] = script
    end
    end

    results
    end
    end