Created
May 10, 2016 03:06
-
-
Save lucasmazza/2126283b47c38812c259fcd0180ac405 to your computer and use it in GitHub Desktop.
`rake assets:stats`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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