class Middleman::Extensions::AssetHash
def after_configuration
def after_configuration # Allow specifying regexes to ignore, plus always ignore apple touch icons @ignore = Array(options.ignore) + [/^apple-touch-icon/] app.use Middleware, exts: options.exts, middleman_app: app, ignore: @ignore end
def ignored_resource?(resource)
def ignored_resource?(resource) @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } end
def initialize(app, options_hash={}, &block)
def initialize(app, options_hash={}, &block) super require 'digest/sha1' require 'rack/test' require 'uri' end
def manipulate_resource_list(resources)
-
(void)
-
def manipulate_resource_list(resources) @rack_client ||= ::Rack::Test::Session.new(app.class.to_rack_app) # Process resources in order: binary images and fonts, then SVG, then JS/CSS. # This is so by the time we get around to the text files (which may reference # images and fonts) the static assets' hashes are already calculated. resources.sort_by do |a| if %w(.svg).include? a.ext 0 elsif %w(.js .css).include? a.ext 1 else -1 end end.each(&method(:manipulate_single_resource)) end
def manipulate_single_resource(resource)
def manipulate_single_resource(resource) return unless options.exts.include?(resource.ext) return if ignored_resource?(resource) return if resource.ignored? # Render through the Rack interface so middleware and mounted apps get a shot response = @rack_client.get(URI.escape(resource.destination_path), {}, 'bypass_asset_hash' => 'true') raise "#{resource.path} should be in the sitemap!" unless response.status == 200 digest = Digest::SHA1.hexdigest(response.body)[0..7] resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } end