2013-12-18 20:33:17 -05:00
|
|
|
task 'assets:precompile:before' do
|
2013-11-03 17:58:34 -05:00
|
|
|
|
2013-12-18 20:33:17 -05: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-03 17:58:34 -05:00
|
|
|
end
|
2013-12-18 20:33:17 -05:00
|
|
|
|
2014-02-06 19:36:44 -05:00
|
|
|
# Ensure we ALWAYS do a clean build
|
|
|
|
# We use many .erbs that get out of date quickly, especially with plugins
|
2014-02-07 05:43:05 -05:00
|
|
|
puts "Purging temp files"
|
2014-02-06 19:36:44 -05:00
|
|
|
`rm -fr #{Rails.root}/tmp/cache`
|
|
|
|
|
2013-12-18 20:33:17 -05: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 00:55:53 -05:00
|
|
|
|
|
|
|
# let's make precompile faster using redis magic
|
|
|
|
require 'sprockets'
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
module ::Sprockets
|
2014-03-04 00:54:58 -05: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 SassCompressor
|
2014-02-06 00:55:53 -05:00
|
|
|
def evaluate(context, locals, &block)
|
2014-03-04 00:54:58 -05:00
|
|
|
::Sprockets.cache_compiled("sass", data) do
|
2014-03-04 01:16:53 -05:00
|
|
|
# HACK, SASS compiler will degrade to aweful perf with huge files
|
2014-04-01 10:24:29 -04:00
|
|
|
# Bypass if larger than 200kb, ensure assets are minified prior
|
2014-03-04 01:16:53 -05:00
|
|
|
if context.pathname &&
|
|
|
|
context.pathname.to_s =~ /.css$/ &&
|
2014-04-01 10:24:29 -04:00
|
|
|
data.length > 200.kilobytes
|
2014-03-04 01:16:53 -05:00
|
|
|
puts "Skipped minifying #{context.pathname} cause it is larger than 200KB, minify in source control or avoid large CSS files"
|
|
|
|
data
|
|
|
|
else
|
|
|
|
::Sass::Engine.new(data, {
|
|
|
|
:syntax => :scss,
|
|
|
|
:cache => false,
|
|
|
|
:read_cache => false,
|
|
|
|
:style => :compressed
|
|
|
|
}).render
|
|
|
|
end
|
2014-03-04 00:54:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-02-06 00:55:53 -05:00
|
|
|
|
2014-03-04 00:54:58 -05:00
|
|
|
class UglifierCompressor
|
2014-02-06 00:55:53 -05:00
|
|
|
|
2014-03-04 00:54:58 -05:00
|
|
|
def evaluate(context, locals, &block)
|
|
|
|
::Sprockets.cache_compiled("uglifier", data) do
|
2014-03-04 01:16:53 -05:00
|
|
|
Uglifier.new(:comments => :none).compile(data)
|
2014-02-06 00:55:53 -05:00
|
|
|
end
|
|
|
|
end
|
2014-03-04 00:54:58 -05:00
|
|
|
|
2014-02-06 00:55:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-03 17:58:34 -05:00
|
|
|
end
|
2013-12-18 20:33:17 -05:00
|
|
|
|
|
|
|
task 'assets:precompile' => 'assets:precompile:before'
|
|
|
|
|