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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment