2013-12-19 09:33:17 +08:00
|
|
|
task 'assets:precompile:before' do
|
2013-11-04 06:58:34 +08:00
|
|
|
|
2014-05-15 13:52:09 +08:00
|
|
|
require 'uglifier'
|
|
|
|
|
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
|
2014-02-07 18:43:05 +08:00
|
|
|
puts "Purging temp files"
|
2014-02-07 08:36:44 +08:00
|
|
|
`rm -fr #{Rails.root}/tmp/cache`
|
|
|
|
|
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
|
2014-03-04 13:54:58 +08:00
|
|
|
|
|
|
|
def self.cache_compiled(type, data)
|
|
|
|
digest = Digest::SHA1.hexdigest(data)
|
|
|
|
key = "SPROCKETS_#{type}_#{digest}"
|
|
|
|
if compiled = $redis.get(key)
|
|
|
|
$redis.expire(key, 1.week)
|
|
|
|
else
|
|
|
|
compiled = yield
|
|
|
|
$redis.setex(key, 1.week, compiled)
|
|
|
|
end
|
|
|
|
compiled
|
|
|
|
end
|
|
|
|
|
|
|
|
class UglifierCompressor
|
2014-02-06 13:55:53 +08:00
|
|
|
|
2014-03-04 13:54:58 +08:00
|
|
|
def evaluate(context, locals, &block)
|
|
|
|
::Sprockets.cache_compiled("uglifier", data) do
|
2014-03-04 14:16:53 +08:00
|
|
|
Uglifier.new(:comments => :none).compile(data)
|
2014-02-06 13:55:53 +08:00
|
|
|
end
|
|
|
|
end
|
2014-03-04 13:54:58 +08:00
|
|
|
|
2014-02-06 13:55:53 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-04 06:58:34 +08:00
|
|
|
end
|
2013-12-19 09:33:17 +08:00
|
|
|
|
2014-05-03 05:46:03 +08:00
|
|
|
task 'assets:precompile:css' => 'environment' do
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
2014-06-13 02:41:37 +08:00
|
|
|
# Heroku precompiles assets before db migration, so tables may not exist.
|
|
|
|
# css will get precompiled during first request instead in that case.
|
|
|
|
if ActiveRecord::Base.connection.table_exists?(ColorScheme.table_name)
|
|
|
|
puts "Compiling css for #{db}"
|
|
|
|
[:desktop, :mobile].each do |target|
|
|
|
|
puts DiscourseStylesheets.compile(target, force: true)
|
|
|
|
end
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
task 'assets:precompile' => 'assets:precompile:before' do
|
|
|
|
# Run after assets:precompile
|
|
|
|
Rake::Task["assets:precompile:css"].invoke
|
|
|
|
end
|