Class: Middleman::Extensions::AssetHash
Constant Summary
Constants included
from Contracts
Contracts::PATH_MATCHER
Instance Attribute Summary
#app, #options
Instance Method Summary
collapse
activated_extension, #add_exposed_to_context, #after_build, #after_configuration, #after_extension_activated, after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, option, #ready, resources
Methods included from Contracts
#Contract
Constructor Details
#initialize(app, options_hash = {}, &block) ⇒ AssetHash
Returns a new instance of AssetHash.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/middleman-core/extensions/asset_hash.rb', line 10
def initialize(app, options_hash={}, &block)
super
require 'addressable/uri'
require 'digest/sha1'
require 'rack/mock'
@ignore = Array(options.ignore) + [/^apple-touch-icon/]
@exts = options.exts || (app.config[:asset_extensions] - %w(.ico))
app.rewrite_inline_urls id: :asset_hash,
url_extensions: @exts.sort.reverse,
source_extensions: options.sources,
ignore: @ignore,
rewrite_ignore: options.rewrite_ignore,
proc: method(:rewrite_url),
after: :asset_host
end
|
Instance Method Details
#ignored_resource?(resource) ⇒ Boolean
100
101
102
103
104
|
# File 'lib/middleman-core/extensions/asset_hash.rb', line 100
def ignored_resource?(resource)
@ignore.any? do |ignore|
Middleman::Util.path_match(ignore, resource.destination_path)
end
end
|
#manipulate_resource_list(resources) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/middleman-core/extensions/asset_hash.rb', line 55
def manipulate_resource_list(resources)
@rack_client ||= begin
rack_app = ::Middleman::Rack.new(app).to_app
::Rack::MockRequest.new(rack_app)
end
resources.sort_by do |a|
if %w(.svg .svgz).include? a.ext
0
elsif %w(.js .css).include? a.ext
1
else
-1
end
end.each(&method(:manipulate_single_resource))
end
|
#manipulate_single_resource(resource) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/middleman-core/extensions/asset_hash.rb', line 76
def manipulate_single_resource(resource)
return unless @exts.include?(resource.ext)
return if ignored_resource?(resource)
return if resource.ignored?
digest = if resource.binary?
::Digest::SHA1.file(resource.source_file).hexdigest[0..7]
else
response = @rack_client.get(
::URI.escape(resource.destination_path),
'bypass_inline_url_rewriter_asset_hash' => 'true'
)
raise "#{resource.path} should be in the sitemap!" unless response.status == 200
::Digest::SHA1.hexdigest(response.body)[0..7]
end
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
resource
end
|
#rewrite_url(asset_path, dirpath, _request_path) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/middleman-core/extensions/asset_hash.rb', line 34
def rewrite_url(asset_path, dirpath, _request_path)
uri = ::Addressable::URI.parse(asset_path)
relative_path = !uri.path.start_with?('/')
full_asset_path = if relative_path
dirpath.join(asset_path).to_s
else
asset_path
end
return unless asset_page = app.sitemap.find_resource_by_destination_path(full_asset_path) || app.sitemap.find_resource_by_path(full_asset_path)
replacement_path = "/#{asset_page.destination_path}"
replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path
replacement_path
end
|