Last active
August 29, 2015 13:56
-
-
Save SamSaffron/8838957 to your computer and use it in GitHub Desktop.
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
BEFORE: | |
sam@ubuntu discourse % rm -fr tmp/cache | |
sam@ubuntu discourse % rm -fr public/assets | |
sam@ubuntu discourse % time RAILS_ENV=production bin/rake assets:precompile | |
58.55s user 1.79s system 100% cpu 1:00.02 total | |
AFTER: | |
sam@ubuntu discourse % rm -fr tmp/cache | |
sam@ubuntu discourse % rm -fr public/assets | |
sam@ubuntu discourse % time RAILS_ENV=production bin/rake assets:precompile | |
7.29s user 0.73s system 100% cpu 8.005 total | |
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
task 'assets:precompile:before' do | |
unless %w{profile production}.include? Rails.env | |
raise "rake assets:precompile should only be run in RAILS_ENV=production, you are risking unminified assets" | |
end | |
# in the past we applied a patch that removed asset postfixes, but it is terrible practice | |
# leaving very complicated build issues | |
# https://github.com/rails/sprockets-rails/issues/49 | |
# let's make precompile faster using redis magic | |
require 'sprockets' | |
require 'digest/sha1' | |
module ::Sprockets | |
class UglifierCompressor | |
def evaluate(context, locals, &block) | |
expire_after = 1.week | |
digest = Digest::SHA1.hexdigest(data) | |
key = "SPROCKETS_#{digest}" | |
if compiled = $redis.get(key) | |
$redis.expire(key, expire_after) | |
else | |
compiled = Uglifier.new(:comments => :none).compile(data) | |
$redis.setex(key, expire_after, compiled) | |
end | |
compiled | |
end | |
end | |
end | |
end | |
task 'assets:precompile' => 'assets:precompile:before' |
This should work for Rails 4 only, correct?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very, very nice! I just tried it on one my Rails projects:
Before:
RAILS_ENV=production bundle exec rake assets:precompile 47.87s user 6.51s system 96% cpu 56.607 total
After:
RAILS_ENV=production bundle exec rake assets:precompile 16.12s user 4.67s system 99% cpu 20.941 total
👍