2013-12-18 20:33:17 -05:00
|
|
|
task 'assets:precompile:before' do
|
2013-11-03 17:58:34 -05:00
|
|
|
|
2014-05-15 01:52:09 -04:00
|
|
|
require 'uglifier'
|
|
|
|
|
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`
|
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
if Rails.configuration.assets.js_compressor == :uglifier && !`which uglifyjs`.empty? && !ENV['SKIP_NODE_UGLIFY']
|
|
|
|
$node_uglify = true
|
2014-12-12 02:53:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
puts "Bundling assets"
|
|
|
|
|
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
|
|
|
|
|
|
|
require 'sprockets'
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
2014-03-04 00:54:58 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
if $node_uglify
|
|
|
|
# monkey patch asset pipeline not to gzip, compress: false is broken
|
|
|
|
class ::Sprockets::Asset
|
|
|
|
# Save asset to disk.
|
|
|
|
def write_to(filename, options = {})
|
|
|
|
# Gzip contents if filename has '.gz'
|
|
|
|
return if File.extname(filename) == '.gz'
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
begin
|
|
|
|
FileUtils.mkdir_p File.dirname(filename)
|
2014-09-23 02:45:19 -04:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
File.open("#{filename}+", 'wb') do |f|
|
|
|
|
f.write to_s
|
|
|
|
end
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
# Atomic write
|
|
|
|
FileUtils.mv("#{filename}+", filename)
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
# Set mtime correctly
|
|
|
|
File.utime(mtime, mtime, filename)
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
nil
|
|
|
|
ensure
|
|
|
|
# Ensure tmp file gets cleaned up
|
|
|
|
FileUtils.rm("#{filename}+") if File.exist?("#{filename}+")
|
|
|
|
end
|
2014-03-04 00:54:58 -05:00
|
|
|
end
|
|
|
|
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
end
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
module ::Sprockets
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
class UglifierCompressor
|
2014-02-06 00:55:53 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
def evaluate(context, locals, &block)
|
|
|
|
# monkey patch cause we do this later, no idea how to cleanly disable
|
|
|
|
data
|
|
|
|
end
|
2014-03-04 00:54:58 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
end
|
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
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
task 'assets:precompile:css' => 'environment' do
|
2015-05-05 16:52:03 -04:00
|
|
|
puts "Start compiling CSS: #{Time.zone.now}"
|
2014-05-02 17:46:03 -04:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
2014-06-12 14:41:37 -04: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|
|
2015-05-05 16:52:03 -04:00
|
|
|
puts DiscourseStylesheets.compile(target)
|
2014-06-12 14:41:37 -04:00
|
|
|
end
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
end
|
2015-05-05 16:52:03 -04:00
|
|
|
puts "Done compiling CSS: #{Time.zone.now}"
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
|
2014-12-12 02:53:26 -05:00
|
|
|
def assets_path
|
|
|
|
"#{Rails.root}/public/assets"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compress_node(from,to)
|
|
|
|
to_path = "#{assets_path}/#{to}"
|
|
|
|
|
|
|
|
source_map_root = (d=File.dirname(from)) == "." ? "/assets" : "/assets/#{d}"
|
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
cmd = "uglifyjs '#{assets_path}/#{from}' -p relative -c -m -o '#{to_path}' --source-map-root '#{source_map_root}' --source-map '#{assets_path}/#{to}.map' --source-map-url '/assets/#{to}.map'"
|
|
|
|
|
|
|
|
|
|
|
|
STDERR.puts cmd
|
|
|
|
`#{cmd} 2>&1`
|
|
|
|
|
2014-12-12 02:53:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def compress_ruby(from,to)
|
|
|
|
data = File.read("#{assets_path}/#{from}")
|
|
|
|
|
|
|
|
uglified, map = Uglifier.new(comments: :none,
|
|
|
|
screw_ie8: false,
|
|
|
|
source_filename: File.basename(from),
|
|
|
|
output_filename: File.basename(to)
|
|
|
|
)
|
|
|
|
.compile_with_map(data)
|
|
|
|
dest = "#{assets_path}/#{to}"
|
|
|
|
|
|
|
|
File.write(dest, uglified << "\n//# sourceMappingURL=/assets/#{to}.map")
|
|
|
|
File.write(dest + ".map", map)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gzip(path)
|
|
|
|
STDERR.puts "gzip #{path}"
|
2015-02-24 10:07:54 -05:00
|
|
|
STDERR.puts `gzip -f -c -9 #{path} > #{path}.gz`
|
2014-12-12 02:53:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def compress(from,to)
|
|
|
|
if @has_uglifyjs ||= !`which uglifyjs`.empty?
|
|
|
|
compress_node(from,to)
|
|
|
|
else
|
|
|
|
compress_ruby(from,to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
task 'assets:precompile' => 'assets:precompile:before' do
|
|
|
|
# Run after assets:precompile
|
|
|
|
Rake::Task["assets:precompile:css"].invoke
|
2014-12-12 02:53:26 -05:00
|
|
|
|
2014-12-17 12:14:12 -05:00
|
|
|
if $node_uglify
|
2014-12-12 02:53:26 -05:00
|
|
|
puts "Compressing Javascript and Generating Source Maps"
|
|
|
|
manifest = Sprockets::Manifest.new(assets_path)
|
2015-02-20 15:48:45 -05:00
|
|
|
|
|
|
|
to_skip = Rails.configuration.assets.skip_minification || []
|
2014-12-12 02:53:26 -05:00
|
|
|
manifest.files
|
|
|
|
.select{|k,v| k =~ /\.js$/}
|
|
|
|
.each do |file, info|
|
|
|
|
|
|
|
|
path = "#{assets_path}/#{file}"
|
|
|
|
_file = (d = File.dirname(file)) == "." ? "_#{file}" : "#{d}/_#{File.basename(file)}"
|
|
|
|
_path = "#{assets_path}/#{_file}"
|
|
|
|
|
|
|
|
if File.exists?(_path)
|
|
|
|
STDERR.puts "Skipping: #{file} already compressed"
|
|
|
|
else
|
|
|
|
STDERR.puts "Compressing: #{file}"
|
2015-02-20 15:48:45 -05:00
|
|
|
|
|
|
|
# We can specify some files to never minify
|
|
|
|
unless to_skip.include?(info['logical_path'])
|
|
|
|
FileUtils.mv(path, _path)
|
|
|
|
compress(_file,file)
|
|
|
|
end
|
2014-12-12 02:53:26 -05:00
|
|
|
|
|
|
|
info["size"] = File.size(path)
|
|
|
|
info["mtime"] = File.mtime(path).iso8601
|
|
|
|
gzip(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# protected
|
|
|
|
manifest.send :save
|
|
|
|
end
|
2014-12-17 12:14:12 -05:00
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|