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'
|
2017-11-24 23:40:49 +08:00
|
|
|
require 'open3'
|
2014-05-15 13:52:09 +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
|
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`
|
|
|
|
|
2017-07-05 16:36:06 +08:00
|
|
|
# Ensure we clear emoji cache before pretty-text/emoji/data.js.es6.erb
|
|
|
|
# is recompiled
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
2017-03-10 06:05:55 +08:00
|
|
|
if Rails.configuration.assets.js_compressor == :uglifier && !`which uglifyjs`.empty? && !ENV['SKIP_NODE_UGLIFY']
|
2014-12-18 01:14:12 +08:00
|
|
|
$node_uglify = true
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
2017-03-10 05:44:50 +08:00
|
|
|
unless ENV['USE_SPROCKETS_UGLIFY']
|
|
|
|
$bypass_sprockets_uglify = true
|
|
|
|
end
|
|
|
|
|
2014-12-12 15:53:26 +08:00
|
|
|
puts "Bundling 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
|
|
|
|
|
|
|
require 'sprockets'
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
2015-07-22 02:52:54 +08:00
|
|
|
# Needed for proper source maps with a CDN
|
|
|
|
load "#{Rails.root}/lib/global_path.rb"
|
|
|
|
include GlobalPath
|
2014-03-04 13:54:58 +08:00
|
|
|
|
2017-03-10 05:44:50 +08:00
|
|
|
if $bypass_sprockets_uglify
|
2016-04-19 14:28:10 +08:00
|
|
|
Rails.configuration.assets.js_compressor = nil
|
2016-08-23 12:04:53 +08:00
|
|
|
Rails.configuration.assets.gzip = false
|
2014-02-06 13:55:53 +08:00
|
|
|
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
|
2015-10-13 07:48:21 +08:00
|
|
|
if ENV["DONT_PRECOMPILE_CSS"] == "1"
|
|
|
|
STDERR.puts "Skipping CSS precompilation, ensure CSS lives in a shared directory across hosts"
|
|
|
|
else
|
|
|
|
STDERR.puts "Start compiling CSS: #{Time.zone.now}"
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
# Heroku precompiles assets before db migration, so tables may not exist.
|
|
|
|
# css will get precompiled during first request instead in that case.
|
|
|
|
|
2017-04-14 22:33:35 +08:00
|
|
|
if ActiveRecord::Base.connection.table_exists?(Theme.table_name)
|
2017-04-12 22:52:52 +08:00
|
|
|
STDERR.puts "Compiling css for #{db} #{Time.zone.now}"
|
2017-05-06 01:50:48 +08:00
|
|
|
begin
|
|
|
|
Stylesheet::Manager.precompile_css
|
|
|
|
rescue => PG::UndefinedColumn
|
|
|
|
STDERR.puts "Skipping precompilation of CSS cause schema is old, you are precompiling prior to running migrations."
|
|
|
|
end
|
2014-06-13 02:41:37 +08:00
|
|
|
end
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|
2015-10-12 14:31:37 +08:00
|
|
|
|
2015-10-13 07:48:21 +08:00
|
|
|
STDERR.puts "Done compiling CSS: #{Time.zone.now}"
|
|
|
|
end
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|
|
|
|
|
2014-12-12 15:53:26 +08:00
|
|
|
def assets_path
|
|
|
|
"#{Rails.root}/public/assets"
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def compress_node(from, to)
|
2014-12-12 15:53:26 +08:00
|
|
|
to_path = "#{assets_path}/#{to}"
|
2016-02-05 10:05:47 +08:00
|
|
|
assets = cdn_relative_path("/assets")
|
2017-07-28 09:20:09 +08:00
|
|
|
source_map_root = assets + ((d = File.dirname(from)) == "." ? "" : "/#{d}")
|
2015-07-22 02:52:54 +08:00
|
|
|
source_map_url = cdn_path "/assets/#{to}.map"
|
2014-12-12 15:53:26 +08:00
|
|
|
|
2015-07-22 02:52:54 +08: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 '#{source_map_url}'"
|
2014-12-18 01:14:12 +08:00
|
|
|
|
|
|
|
STDERR.puts cmd
|
2015-09-22 09:28:15 +08:00
|
|
|
result = `#{cmd} 2>&1`
|
|
|
|
unless $?.success?
|
|
|
|
STDERR.puts result
|
|
|
|
exit 1
|
|
|
|
end
|
2014-12-18 01:14:12 +08:00
|
|
|
|
2015-09-22 09:28:15 +08:00
|
|
|
result
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def compress_ruby(from, to)
|
2014-12-12 15:53:26 +08:00
|
|
|
data = File.read("#{assets_path}/#{from}")
|
|
|
|
|
|
|
|
uglified, map = Uglifier.new(comments: :none,
|
2016-01-11 19:13:37 +08:00
|
|
|
screw_ie8: true,
|
2017-03-10 05:44:50 +08:00
|
|
|
source_map: {
|
|
|
|
filename: File.basename(from),
|
|
|
|
output_filename: File.basename(to)
|
|
|
|
}
|
2014-12-12 15:53:26 +08:00
|
|
|
)
|
2017-07-28 09:20:09 +08:00
|
|
|
.compile_with_map(data)
|
2014-12-12 15:53:26 +08:00
|
|
|
dest = "#{assets_path}/#{to}"
|
|
|
|
|
2015-07-22 02:52:54 +08:00
|
|
|
File.write(dest, uglified << "\n//# sourceMappingURL=#{cdn_path "/assets/#{to}.map"}")
|
2014-12-12 15:53:26 +08:00
|
|
|
File.write(dest + ".map", map)
|
2017-03-11 00:35:54 +08:00
|
|
|
|
|
|
|
GC.start
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def gzip(path)
|
2017-11-24 22:52:08 +08:00
|
|
|
STDERR.puts "gzip -f -c -9 #{path} > #{path}.gz"
|
2016-06-07 14:55:57 +08:00
|
|
|
STDERR.puts `gzip -f -c -9 #{path} > #{path}.gz`
|
2017-11-24 22:52:08 +08:00
|
|
|
raise "gzip compression failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
|
2016-06-07 14:55:57 +08:00
|
|
|
end
|
|
|
|
|
2017-11-25 00:53:49 +08:00
|
|
|
if ENV['COMPRESS_BROTLI']&.to_i == 1
|
|
|
|
# different brotli versions use different parameters
|
|
|
|
ver_out, ver_err, ver_status = Open3.capture3('brotli --version')
|
|
|
|
if !ver_status.success?
|
|
|
|
# old versions of brotli don't respond to --version
|
|
|
|
def brotli_command(path)
|
|
|
|
"brotli --quality 11 --input #{path} --output #{path}.br"
|
|
|
|
end
|
|
|
|
elsif ver_out >= "brotli 1.0.0"
|
|
|
|
def brotli_command(path)
|
|
|
|
"brotli --quality=11 #{path} --output=#{path}.br"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# not sure what to do here, not expecting this
|
|
|
|
raise "cannot determine brotli version"
|
2017-11-24 23:40:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-07 14:55:57 +08:00
|
|
|
def brotli(path)
|
2017-03-27 11:55:43 +08:00
|
|
|
if ENV['COMPRESS_BROTLI']&.to_i == 1
|
2017-11-24 23:40:49 +08:00
|
|
|
STDERR.puts brotli_command(path)
|
|
|
|
STDERR.puts `#{brotli_command(path)}`
|
2017-11-24 22:52:08 +08:00
|
|
|
raise "brotli compression failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
|
2016-06-07 15:42:12 +08:00
|
|
|
STDERR.puts `chmod +r #{path}.br`
|
2017-11-24 22:52:08 +08:00
|
|
|
raise "chmod failed: exit code #{$?.exitstatus}" if $?.exitstatus != 0
|
2016-06-07 14:55:57 +08:00
|
|
|
end
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def compress(from, to)
|
2017-03-10 05:44:50 +08:00
|
|
|
if $node_uglify
|
2017-07-28 09:20:09 +08:00
|
|
|
compress_node(from, to)
|
2014-12-12 15:53:26 +08:00
|
|
|
else
|
2017-07-28 09:20:09 +08:00
|
|
|
compress_ruby(from, to)
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-20 09:34:30 +08:00
|
|
|
def concurrent?
|
2016-06-07 15:03:05 +08:00
|
|
|
if ENV["SPROCKETS_CONCURRENT"] == "1"
|
2016-04-20 09:34:30 +08:00
|
|
|
concurrent_compressors = []
|
|
|
|
yield(Proc.new { |&block| concurrent_compressors << Concurrent::Future.execute { block.call } })
|
|
|
|
concurrent_compressors.each(&:wait!)
|
|
|
|
else
|
|
|
|
yield(Proc.new { |&block| block.call })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-03 05:46:03 +08:00
|
|
|
task 'assets:precompile' => 'assets:precompile:before' do
|
2014-12-12 15:53:26 +08:00
|
|
|
|
2017-03-10 05:44:50 +08:00
|
|
|
if $bypass_sprockets_uglify
|
2014-12-12 15:53:26 +08:00
|
|
|
puts "Compressing Javascript and Generating Source Maps"
|
|
|
|
manifest = Sprockets::Manifest.new(assets_path)
|
2015-02-21 04:48:45 +08:00
|
|
|
|
2016-04-20 09:34:30 +08:00
|
|
|
concurrent? do |proc|
|
|
|
|
to_skip = Rails.configuration.assets.skip_minification || []
|
|
|
|
manifest.files
|
2017-07-28 09:20:09 +08:00
|
|
|
.select { |k, v| k =~ /\.js$/ }
|
|
|
|
.each do |file, info|
|
2016-04-20 09:34:30 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
path = "#{assets_path}/#{file}"
|
2016-04-20 09:34:30 +08:00
|
|
|
_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}"
|
|
|
|
|
|
|
|
proc.call do
|
|
|
|
# We can specify some files to never minify
|
|
|
|
unless (ENV["DONT_MINIFY"] == "1") || to_skip.include?(info['logical_path'])
|
|
|
|
FileUtils.mv(path, _path)
|
2017-07-28 09:20:09 +08:00
|
|
|
compress(_file, file)
|
2016-04-20 09:34:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
info["size"] = File.size(path)
|
|
|
|
info["mtime"] = File.mtime(path).iso8601
|
|
|
|
gzip(path)
|
2016-06-07 14:55:57 +08:00
|
|
|
brotli(path)
|
2016-04-20 09:34:30 +08:00
|
|
|
end
|
2015-02-21 04:48:45 +08:00
|
|
|
end
|
2016-04-20 09:34:30 +08:00
|
|
|
end
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# protected
|
|
|
|
manifest.send :save
|
2017-03-21 01:05:39 +08:00
|
|
|
|
2017-03-21 03:59:06 +08:00
|
|
|
if GlobalSetting.fallback_assets_path.present?
|
2017-03-21 01:43:59 +08:00
|
|
|
begin
|
2017-03-21 03:59:06 +08:00
|
|
|
FileUtils.cp_r("#{Rails.root}/public/assets/.", GlobalSetting.fallback_assets_path)
|
2017-03-21 01:43:59 +08:00
|
|
|
rescue => e
|
2017-03-21 03:59:06 +08:00
|
|
|
STDERR.puts "Failed to backup assets to #{GlobalSetting.fallback_assets_path}"
|
2017-03-21 01:43:59 +08:00
|
|
|
STDERR.puts e
|
|
|
|
STDERR.puts e.backtrace
|
|
|
|
end
|
2017-03-21 01:05:39 +08:00
|
|
|
end
|
2017-04-14 22:30:19 +08:00
|
|
|
end
|
2017-03-21 01:05:39 +08:00
|
|
|
|
2017-04-14 22:30:19 +08:00
|
|
|
end
|
2017-03-21 01:05:39 +08:00
|
|
|
|
2017-04-14 22:30:19 +08:00
|
|
|
Rake::Task["assets:precompile"].enhance do
|
|
|
|
class Sprockets::Manifest
|
|
|
|
def reload
|
2017-04-15 03:06:52 +08:00
|
|
|
@filename = find_directory_manifest(@directory)
|
2017-04-14 22:30:19 +08:00
|
|
|
@data = json_decode(File.read(@filename))
|
|
|
|
end
|
2014-12-12 15:53:26 +08:00
|
|
|
end
|
2017-04-15 03:06:52 +08:00
|
|
|
|
2017-04-14 22:30:19 +08:00
|
|
|
# cause on boot we loaded a blank manifest,
|
|
|
|
# we need to know where all the assets are to precompile CSS
|
|
|
|
# cause CSS uses asset_path
|
|
|
|
Rails.application.assets_manifest.reload
|
|
|
|
Rake::Task["assets:precompile:css"].invoke
|
2014-05-03 05:46:03 +08:00
|
|
|
end
|