Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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'
@joerichsen
Copy link

joerichsen commented Feb 6, 2014

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

👍

@AvnerCohen
Copy link

AvnerCohen commented Feb 7, 2014

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