2013-11-04 06:58:34 +08:00
|
|
|
|
2013-12-19 09:33:17 +08:00
|
|
|
task 'assets:precompile:before' do
|
2013-11-04 06:58:34 +08:00
|
|
|
|
2013-12-19 09:33:17 +08:00
|
|
|
unless %w{profile production}.include? Rails.env
|
|
|
|
raise "rake assets:precompile should only be run in RAILS_ENV=production, you are risking unminified assets"
|
2013-11-04 06:58:34 +08:00
|
|
|
end
|
2013-12-19 09:33:17 +08:00
|
|
|
|
2014-02-07 08:36:44 +08:00
|
|
|
# Ensure we ALWAYS do a clean build
|
|
|
|
# We use many .erbs that get out of date quickly, especially with plugins
|
|
|
|
puts "Purging old assets"
|
|
|
|
`rm -fr #{Rails.root}/tmp/cache`
|
|
|
|
`rm -fr #{Rails.root}/public/assets`
|
|
|
|
|
2013-12-19 09:33:17 +08:00
|
|
|
# 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
|
2014-02-06 13:55:53 +08:00
|
|
|
|
|
|
|
# let's make precompile faster using redis magic
|
|
|
|
require 'sprockets'
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
module ::Sprockets
|
|
|
|
class UglifierCompressor
|
|
|
|
def evaluate(context, locals, &block)
|
|
|
|
|
|
|
|
digest = Digest::SHA1.hexdigest(data)
|
|
|
|
key = "SPROCKETS_#{digest}"
|
|
|
|
|
2014-02-06 14:07:15 +08:00
|
|
|
if compiled = $redis.get(key)
|
|
|
|
$redis.expire(key, 1.week)
|
|
|
|
else
|
2014-02-06 13:55:53 +08:00
|
|
|
compiled = Uglifier.new(:comments => :none).compile(data)
|
|
|
|
$redis.setex(key, 1.week, compiled)
|
|
|
|
end
|
|
|
|
compiled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-04 06:58:34 +08:00
|
|
|
end
|
2013-12-19 09:33:17 +08:00
|
|
|
|
|
|
|
task 'assets:precompile' => 'assets:precompile:before'
|
|
|
|
|