Skip to content

Commit

Permalink
PERF: Uglify and gzip assets concurrently.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgxworld committed Apr 21, 2016
1 parent 51b0b5f commit b744306
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ def compress(from,to)
end
end

def concurrent?
if ENV["CONCURRENT"] == "1"
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

task 'assets:precompile' => 'assets:precompile:before' do
# Run after assets:precompile
Rake::Task["assets:precompile:css"].invoke
Expand All @@ -124,30 +134,34 @@ task 'assets:precompile' => 'assets:precompile:before' do
puts "Compressing Javascript and Generating Source Maps"
manifest = Sprockets::Manifest.new(assets_path)

to_skip = Rails.configuration.assets.skip_minification || []
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}"

# We can specify some files to never minify
unless (ENV["DONT_MINIFY"] == "1") || to_skip.include?(info['logical_path'])
FileUtils.mv(path, _path)
compress(_file,file)
concurrent? do |proc|
to_skip = Rails.configuration.assets.skip_minification || []
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}"

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)
compress(_file,file)
end

info["size"] = File.size(path)
info["mtime"] = File.mtime(path).iso8601
gzip(path)
end
end

info["size"] = File.size(path)
info["mtime"] = File.mtime(path).iso8601
gzip(path)
end
end
end

# protected
Expand Down

0 comments on commit b744306

Please sign in to comment.