Class: Middleman::Extensions::AssetHash

Inherits:
Middleman::Extension show all
Defined in:
middleman-core/lib/middleman-core/extensions/asset_hash.rb

Constant Summary

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary

Attributes inherited from Middleman::Extension

#app, #options

Instance Method Summary (collapse)

Methods inherited from Middleman::Extension

activated_extension, #after_build, #after_extension_activated, after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, helpers, option

Methods included from Contracts

#Contract

Constructor Details

- (AssetHash) initialize(app, options_hash = {}, &block)



7
8
9
10
11
12
13
14
# File 'middleman-core/lib/middleman-core/extensions/asset_hash.rb', line 7

def initialize(app, options_hash={}, &block)
  super

  require 'digest/sha1'
  require 'rack/mock'
  require 'uri'
  require 'middleman-core/middleware/inline_url_rewriter'
end

Instance Method Details

- (Object) after_configuration



16
17
18
19
20
21
22
23
24
25
26
27
# File 'middleman-core/lib/middleman-core/extensions/asset_hash.rb', line 16

def after_configuration
  # Allow specifying regexes to ignore, plus always ignore apple touch icons
  @ignore = Array(options.ignore) + [/^apple-touch-icon/]

  app.use ::Middleman::Middleware::InlineURLRewriter,
          id: :asset_hash,
          url_extensions: options.exts,
          source_extensions: %w(.htm .html .php .css .js),
          ignore: @ignore,
          middleman_app: app,
          proc: method(:rewrite_url)
end

- (Boolean) ignored_resource?(resource)



89
90
91
# File 'middleman-core/lib/middleman-core/extensions/asset_hash.rb', line 89

def ignored_resource?(resource)
  @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
end

- (Object) manipulate_resource_list(resources)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'middleman-core/lib/middleman-core/extensions/asset_hash.rb', line 49

def manipulate_resource_list(resources)
  @rack_client ||= begin
    rack_app = ::Middleman::Rack.new(app).to_app
    ::Rack::MockRequest.new(rack_app)
  end

  # 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

- (Object) manipulate_single_resource(resource)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'middleman-core/lib/middleman-core/extensions/asset_hash.rb', line 70

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_inline_url_rewriter_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}" }
  resource
end

- (Object) rewrite_url(asset_path, dirpath, _request_path)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'middleman-core/lib/middleman-core/extensions/asset_hash.rb', line 30

def rewrite_url(asset_path, dirpath, _request_path)
  relative_path = Pathname.new(asset_path).relative?

  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_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