2013-04-20 23:27:25 +02:00
|
|
|
# This extension Gzips assets and pages when building.
|
|
|
|
# Gzipped assets and pages can be served directly by Apache or
|
|
|
|
# Nginx with the proper configuration, and pre-zipping means that we
|
|
|
|
# can use a more agressive compression level at no CPU cost per request.
|
|
|
|
#
|
|
|
|
# Use Nginx's gzip_static directive, or AddEncoding and mod_rewrite in Apache
|
|
|
|
# to serve your Gzipped files whenever the normal (non-.gz) filename is requested.
|
|
|
|
#
|
|
|
|
# Pass the :exts options to customize which file extensions get zipped (defaults
|
|
|
|
# to .html, .htm, .js and .css.
|
|
|
|
#
|
|
|
|
class Middleman::Extensions::Gzip < ::Middleman::Extension
|
|
|
|
option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-01-02 06:19:05 +01:00
|
|
|
class NumberHelpers
|
|
|
|
include ::Padrino::Helpers::NumberHelpers
|
|
|
|
end
|
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def initialize(app, options_hash={})
|
|
|
|
super
|
2013-05-25 02:11:46 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
require 'zlib'
|
|
|
|
require 'stringio'
|
|
|
|
require 'find'
|
2013-12-11 07:06:41 +01:00
|
|
|
require 'thread'
|
2013-05-25 00:49:15 +02:00
|
|
|
end
|
2012-02-11 05:09:39 +01:00
|
|
|
|
2013-05-25 00:49:15 +02:00
|
|
|
def after_build(builder)
|
2013-12-11 07:06:41 +01:00
|
|
|
num_threads = 4
|
2014-01-01 03:21:30 +01:00
|
|
|
paths = ::Middleman::Util.all_files_under(app.config[:build_dir])
|
2013-05-25 00:49:15 +02:00
|
|
|
total_savings = 0
|
2013-05-12 19:09:45 +02:00
|
|
|
|
2013-12-11 07:06:41 +01:00
|
|
|
# Fill a queue with inputs
|
|
|
|
in_queue = Queue.new
|
|
|
|
paths.each do |path|
|
|
|
|
in_queue << path if options.exts.include?(path.extname)
|
|
|
|
end
|
|
|
|
num_paths = in_queue.size
|
|
|
|
|
|
|
|
# Farm out gzip tasks to threads and put the results in in_queue
|
|
|
|
out_queue = Queue.new
|
|
|
|
threads = num_threads.times.map do
|
|
|
|
Thread.new do
|
|
|
|
while path = in_queue.pop
|
|
|
|
out_queue << gzip_file(path.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Insert a nil for each thread to stop it
|
|
|
|
num_threads.times do
|
|
|
|
in_queue << nil
|
|
|
|
end
|
|
|
|
|
2013-12-01 07:59:14 +01:00
|
|
|
old_locale = I18n.locale
|
|
|
|
I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
|
|
|
|
|
2013-12-11 07:06:41 +01:00
|
|
|
num_paths.times do
|
|
|
|
output_filename, old_size, new_size = out_queue.pop
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-05-25 00:49:15 +02:00
|
|
|
if output_filename
|
|
|
|
total_savings += (old_size - new_size)
|
|
|
|
size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
|
2014-01-02 06:19:05 +01:00
|
|
|
builder.say_status :gzip, "#{output_filename} (#{NumberHelpers.new.number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
|
2012-02-11 05:09:39 +01:00
|
|
|
end
|
|
|
|
end
|
2013-05-25 00:49:15 +02:00
|
|
|
|
2014-01-02 06:19:05 +01:00
|
|
|
builder.say_status :gzip, "Total gzip savings: #{NumberHelpers.new.number_to_human_size(total_savings)}", :blue
|
2013-12-01 07:59:14 +01:00
|
|
|
I18n.locale = old_locale
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-02-11 05:09:39 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def gzip_file(path)
|
|
|
|
input_file = File.open(path, 'rb').read
|
|
|
|
output_filename = path + '.gz'
|
|
|
|
input_file_time = File.mtime(path)
|
2012-02-11 05:09:39 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Check if the right file's already there
|
|
|
|
if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time
|
2013-12-11 07:06:41 +01:00
|
|
|
return [nil, nil, nil]
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-04-28 07:25:45 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
File.open(output_filename, 'wb') do |f|
|
|
|
|
gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
|
|
|
|
gz.mtime = input_file_time.to_i
|
|
|
|
gz.write input_file
|
|
|
|
gz.close
|
|
|
|
end
|
2012-03-05 01:42:14 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Make the file times match, both for Nginx's gzip_static extension
|
|
|
|
# and so we can ID existing files. Also, so even if the GZ files are
|
|
|
|
# wiped out by build --clean and recreated, we won't rsync them over
|
|
|
|
# again because they'll end up with the same mtime.
|
|
|
|
File.utime(File.atime(output_filename), input_file_time, output_filename)
|
2012-03-11 19:06:48 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
old_size = File.size(path)
|
|
|
|
new_size = File.size(output_filename)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
[output_filename, old_size, new_size]
|
2012-02-11 05:09:39 +01:00
|
|
|
end
|
|
|
|
end
|