2013-12-31 23:41:17 +01:00
|
|
|
require 'middleman-core/util'
|
2015-08-17 19:48:56 +02:00
|
|
|
require 'middleman-core/rack'
|
2013-12-31 23:41:17 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
2016-01-27 10:48:36 +01:00
|
|
|
option :sources, %w(.css .htm .html .js .php .xhtml), 'List of extensions that are searched for hashable assets.'
|
2016-03-22 23:43:48 +01:00
|
|
|
option :exts, nil, 'List of extensions that get asset hashes appended to them.'
|
2013-12-28 01:26:31 +01:00
|
|
|
option :ignore, [], 'Regexes of filenames to skip adding asset hashes to'
|
2016-01-08 22:56:42 +01:00
|
|
|
option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites'
|
2012-05-25 01:14:17 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2012-04-04 09:00:33 +02:00
|
|
|
|
2016-01-14 02:16:36 +01:00
|
|
|
require 'addressable/uri'
|
2013-04-20 23:27:25 +02:00
|
|
|
require 'digest/sha1'
|
2014-01-03 02:25:31 +01:00
|
|
|
require 'rack/mock'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Allow specifying regexes to ignore, plus always ignore apple touch icons
|
|
|
|
@ignore = Array(options.ignore) + [/^apple-touch-icon/]
|
2015-09-17 18:41:17 +02:00
|
|
|
|
2016-03-22 23:43:48 +01:00
|
|
|
# Exclude .ico from the default list because browsers expect it
|
|
|
|
# to be named "favicon.ico"
|
|
|
|
@exts = options.exts || (app.config[:asset_extensions] - %w(.ico))
|
|
|
|
|
2016-01-14 02:16:36 +01:00
|
|
|
app.rewrite_inline_urls id: :asset_hash,
|
2016-03-22 23:43:48 +01:00
|
|
|
url_extensions: @exts.sort.reverse,
|
2016-01-14 02:16:36 +01:00
|
|
|
source_extensions: options.sources,
|
|
|
|
ignore: @ignore,
|
|
|
|
rewrite_ignore: options.rewrite_ignore,
|
2016-01-14 23:02:33 +01:00
|
|
|
proc: method(:rewrite_url),
|
|
|
|
after: :asset_host
|
2014-04-16 00:16:52 +02:00
|
|
|
end
|
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract String, Or[String, Pathname], Any => Maybe[String]
|
2014-06-16 18:05:24 +02:00
|
|
|
def rewrite_url(asset_path, dirpath, _request_path)
|
2015-04-26 22:01:19 +02:00
|
|
|
uri = ::Addressable::URI.parse(asset_path)
|
2016-01-14 02:16:36 +01:00
|
|
|
relative_path = !uri.path.start_with?('/')
|
2014-04-16 00:16:52 +02:00
|
|
|
|
|
|
|
full_asset_path = if relative_path
|
|
|
|
dirpath.join(asset_path).to_s
|
|
|
|
else
|
|
|
|
asset_path
|
|
|
|
end
|
|
|
|
|
2015-04-26 22:01:19 +02:00
|
|
|
return unless asset_page = app.sitemap.find_resource_by_destination_path(full_asset_path) || app.sitemap.find_resource_by_path(full_asset_path)
|
2014-04-16 00:16:52 +02:00
|
|
|
|
2014-07-02 20:05:57 +02:00
|
|
|
replacement_path = "/#{asset_page.destination_path}"
|
|
|
|
replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path
|
2015-04-26 22:01:19 +02:00
|
|
|
|
2014-07-02 20:05:57 +02:00
|
|
|
replacement_path
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-05-27 00:08:59 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Update the main sitemap resource list
|
2014-07-03 04:04:34 +02:00
|
|
|
# @return Array<Middleman::Sitemap::Resource>
|
|
|
|
Contract ResourceList => ResourceList
|
2013-04-20 23:27:25 +02:00
|
|
|
def manipulate_resource_list(resources)
|
2014-07-05 21:14:58 +02:00
|
|
|
@rack_client ||= begin
|
|
|
|
rack_app = ::Middleman::Rack.new(app).to_app
|
|
|
|
::Rack::MockRequest.new(rack_app)
|
|
|
|
end
|
2013-06-01 02:46:12 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# 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.
|
2014-05-12 09:05:22 +02:00
|
|
|
resources.sort_by do |a|
|
2015-04-26 22:01:19 +02:00
|
|
|
if %w(.svg .svgz).include? a.ext
|
2013-04-20 23:27:25 +02:00
|
|
|
0
|
|
|
|
elsif %w(.js .css).include? a.ext
|
|
|
|
1
|
|
|
|
else
|
|
|
|
-1
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
2014-05-12 09:05:22 +02:00
|
|
|
end.each(&method(:manipulate_single_resource))
|
2014-04-14 19:05:00 +02:00
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract IsA['Middleman::Sitemap::Resource'] => Maybe[IsA['Middleman::Sitemap::Resource']]
|
2014-05-12 09:05:22 +02:00
|
|
|
def manipulate_single_resource(resource)
|
2016-03-22 23:43:48 +01:00
|
|
|
return unless @exts.include?(resource.ext)
|
2014-05-12 09:05:22 +02:00
|
|
|
return if ignored_resource?(resource)
|
|
|
|
return if resource.ignored?
|
|
|
|
|
2016-01-14 02:16:36 +01:00
|
|
|
digest = if resource.binary?
|
|
|
|
::Digest::SHA1.file(resource.source_file).hexdigest[0..7]
|
|
|
|
else
|
|
|
|
# Render through the Rack interface so middleware and mounted apps get a shot
|
|
|
|
response = @rack_client.get(
|
|
|
|
::URI.escape(resource.destination_path),
|
|
|
|
'bypass_inline_url_rewriter_asset_hash' => 'true'
|
|
|
|
)
|
2014-04-16 00:16:52 +02:00
|
|
|
|
2016-01-14 02:16:36 +01:00
|
|
|
raise "#{resource.path} should be in the sitemap!" unless response.status == 200
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2016-01-14 02:16:36 +01:00
|
|
|
::Digest::SHA1.hexdigest(response.body)[0..7]
|
|
|
|
end
|
2013-06-01 02:46:12 +02:00
|
|
|
|
2014-05-12 09:05:22 +02:00
|
|
|
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
|
2014-07-03 04:04:34 +02:00
|
|
|
resource
|
2013-06-01 02:46:12 +02:00
|
|
|
end
|
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract IsA['Middleman::Sitemap::Resource'] => Bool
|
2013-06-01 02:46:12 +02:00
|
|
|
def ignored_resource?(resource)
|
2015-04-26 22:01:19 +02:00
|
|
|
@ignore.any? do |ignore|
|
|
|
|
Middleman::Util.path_match(ignore, resource.destination_path)
|
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
|
|
|
end
|