mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Feature: Add an option to create asset hashes without using the original filenames as prefix (#2249)
* Add option to remove filename to asset_hash extension * Add feature definition for remove_filename option in asset_hash * Yes, sir rubocop sir... * Replace remove_filename feature by rename_proc * Add tests for rename proc feature * Fix Rubocop issues
This commit is contained in:
parent
2ef303294d
commit
b975956e99
7 changed files with 63 additions and 1 deletions
|
@ -306,3 +306,17 @@ Feature: Assets get file hashes appended to them and references to them are upda
|
|||
| javascripts/application.js.map |
|
||||
|
||||
And the file "javascripts/application-myprefix-4553338c.js" should contain "//# sourceMappingURL=application.js-myprefix-22cc2b5f.map"
|
||||
|
||||
Scenario: Filenames can be removed by option
|
||||
Given a successfully built app at "asset-hash-remove-filename"
|
||||
When I cd to "build"
|
||||
Then the following files should exist:
|
||||
| index.html |
|
||||
| javascripts/4553338c.js |
|
||||
| javascripts/22cc2b5f.map |
|
||||
| index.html |
|
||||
And the following files should not exist:
|
||||
| javascripts/application.js |
|
||||
| javascripts/application.js.map |
|
||||
|
||||
And the file "javascripts/4553338c.js" should contain "//# sourceMappingURL=22cc2b5f.map"
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
activate :asset_hash,
|
||||
rename_proc: lambda do |path, _basename, digest, extension, _options|
|
||||
"#{path}#{digest}#{extension}"
|
||||
end
|
||||
|
||||
activate :relative_assets
|
||||
|
||||
activate :directory_indexes
|
|
@ -0,0 +1,6 @@
|
|||
<% content_for :head do %>
|
||||
<title>The Middleman!</title>
|
||||
<% end %>
|
||||
|
||||
<h1>Testing the sitemap hashing</h1>
|
||||
<br /><br /><br />
|
|
@ -0,0 +1,2 @@
|
|||
function foo(){var message="HEY THERE FRIEND!";var para=document.createElement("p");para.innerHTML=message;var body=document.getElementsByTagName("body")[0];body.insertBefore(para,body.firstChild)}window.onload=foo;
|
||||
//# sourceMappingURL=application.js.map
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"sources":["source/javascripts/application.js"],"names":["foo","message","para","document","createElement","innerHTML","body","getElementsByTagName","insertBefore","firstChild","window","onload"],"mappings":"AAAA,QAASA,OACP,GAAIC,SAAU,mBACd,IAAIC,MAAOC,SAASC,cAAc,IAClCF,MAAKG,UAAYJ,OACb,IAAIK,MAAOH,SAASI,qBAAqB,QAAQ,EAC/CD,MAAKE,aAAaN,KAAMI,KAAKG,YAGpCC,OAAOC,OAASX"}
|
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
||||
<%= javascript_include_tag "application" %>
|
||||
<%= yield_content :head %>
|
||||
</head>
|
||||
|
||||
<body class="<%= page_classes %>">
|
||||
|
||||
<div id="main" role="main">
|
||||
<%= yield %>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -6,6 +6,9 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
option :ignore, [], 'Regexes of filenames to skip adding asset hashes to'
|
||||
option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites'
|
||||
option :prefix, '', 'Prefix for hash'
|
||||
option :rename_proc, proc { |path, basename, digest, extension, options|
|
||||
"#{path}#{basename}-#{options.prefix}#{digest}#{extension}"
|
||||
}, 'Accepts path parameters and returns path name'
|
||||
|
||||
def initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block)
|
||||
super
|
||||
|
@ -90,7 +93,8 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
end
|
||||
|
||||
resource_list.update!(resource, :destination_path) do
|
||||
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{options.prefix}#{digest}#{ext}" }
|
||||
path, basename, extension = split_path(resource.destination_path)
|
||||
resource.destination_path = options.rename_proc.call(path, basename, digest, extension, options)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -100,4 +104,14 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
Middleman::Util.path_match(ignore, resource.destination_path)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Splits resource path into path, basename and extension
|
||||
# (e.g. "/images/landscape.png" => ["/images/", "landscape", ".png]
|
||||
def split_path(filepath)
|
||||
basename = File.basename(filepath, extension = File.extname(filepath))
|
||||
path = filepath.chomp(basename + extension)
|
||||
[path, basename, extension]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue