2013-04-20 17:27:25 -04: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-13 18:39:06 -04:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
def initialize(app, options_hash={})
|
|
|
|
super
|
|
|
|
|
|
|
|
require 'zlib'
|
|
|
|
require 'stringio'
|
|
|
|
require 'find'
|
2012-02-10 23:09:39 -05:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
gzip_ext = self
|
2012-08-13 18:39:06 -04:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
app.after_build do |builder|
|
|
|
|
paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir)
|
|
|
|
paths.each do |path|
|
|
|
|
next unless gzip_ext.options.exts.include? path.extname
|
2012-08-13 18:39:06 -04:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s)
|
2012-08-13 18:39:06 -04:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
if output_filename
|
|
|
|
size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
|
|
|
|
old_locale = I18n.locale
|
|
|
|
I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
|
|
|
|
builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
|
|
|
|
I18n.locale = old_locale
|
2012-02-10 23:09:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-20 17:27:25 -04:00
|
|
|
end
|
2012-02-10 23:09:39 -05:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
def gzip_file(path)
|
|
|
|
input_file = File.open(path, 'rb').read
|
|
|
|
output_filename = path + '.gz'
|
|
|
|
input_file_time = File.mtime(path)
|
2012-02-10 23:09:39 -05:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
# Check if the right file's already there
|
|
|
|
if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time
|
|
|
|
return
|
|
|
|
end
|
2012-04-28 01:25:45 -04:00
|
|
|
|
2013-04-20 17:27:25 -04: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-04 19:42:14 -05:00
|
|
|
|
2013-04-20 17:27:25 -04: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 14:06:48 -04:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
old_size = File.size(path)
|
|
|
|
new_size = File.size(output_filename)
|
2012-08-13 18:39:06 -04:00
|
|
|
|
2013-04-20 17:27:25 -04:00
|
|
|
[output_filename, old_size, new_size]
|
2012-02-10 23:09:39 -05:00
|
|
|
end
|
|
|
|
end
|