Sha256: 49b07a17e03a4f01d27062b6022ed4cc80010bb0b253bc1f5c256078838b78ab
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
require 'zlib' require 'stringio' require 'find' module Middleman::Extensions # 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. # module Gzip class << self def registered(app, options={}) exts = options[:exts] || %w(.js .css .html .htm) app.send :include, InstanceMethods app.after_build do |builder| Find.find(self.class.inst.build_dir) do |path| next if File.directory? path if exts.include? File.extname(path) new_size = gzip_file(path, builder) end end end end alias :included :registered end module InstanceMethods def gzip_file(path, builder) input_file = File.open(path, 'r').read output_filename = path + '.gz' File.open(output_filename, 'w') do |f| gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) gz.write input_file gz.close end old_size = File.size(path) new_size = File.size(output_filename) size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger' builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
middleman-more-3.0.0.beta.2 | lib/middleman-more/extensions/gzip.rb |