Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Created May 10, 2016 03:06
Show Gist options
  • Save lucasmazza/2126283b47c38812c259fcd0180ac405 to your computer and use it in GitHub Desktop.
Save lucasmazza/2126283b47c38812c259fcd0180ac405 to your computer and use it in GitHub Desktop.
`rake assets:stats`
namespace :assets do
# Display all precompiled assets and their included
# dependencies.
#
# Arguments:
# * '--sort' sort the included deps list by their file size.
#
# bin/rake assets:stats [-- --sort]
desc 'List file size for precompiled assets'
task stats: :environment do
sort = ARGV.include?('--sort')
include ActiveSupport::NumberHelper
environment = Rails.application.assets
shell = Thor::Base.shell.new
assets = Rails.application.assets_manifest.find(Rails.application.config.assets.precompile)
assets.each do |asset|
length = asset.length
puts "#{asset.filename.remove("#{Rails.root}/")}: #{number_to_human_size(length)} total."
# NOTE: `included` does not account for `.scss` files that were `@import`ed.
if asset.metadata[:included] && asset.metadata[:included].length > 1
deps = (asset.metadata[:included]).map do |uri|
included = environment.load(uri)
name = included.logical_path.remove('.self')
size = number_to_human_size(included.length)
[included.length, ['⎿', name, size]]
end
deps.sort_by!(&:first) if sort
puts "Included assets:"
shell.print_table(deps.map(&:last))
end
puts "\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment