Add rewrite_ignore option to asset_hash extension.
This allows for configuring the files that will be skipped when performing URL rewriting on the file content. This differs from the existing `ignore` option, which configures the files that won't have hashes added to the filename.
This commit is contained in:
parent
1e8080b44c
commit
0563523a81
|
@ -203,6 +203,45 @@ Feature: Assets get file hashes appended to them and references to them are upda
|
||||||
| javascripts/application-1d8d5276.js |
|
| javascripts/application-1d8d5276.js |
|
||||||
| stylesheets/site-7474cadd.css |
|
| stylesheets/site-7474cadd.css |
|
||||||
|
|
||||||
|
Scenario: Hashed-asset files are not replaced for rewrite ignored paths
|
||||||
|
Given a fixture app "asset-hash-app"
|
||||||
|
And a file named "config.rb" with:
|
||||||
|
"""
|
||||||
|
is_stylesheet = proc { |path| path.start_with? '/stylesheets' }
|
||||||
|
activate :asset_hash, rewrite_ignore: [
|
||||||
|
%r(javascripts/*),
|
||||||
|
'/subdir/*',
|
||||||
|
is_stylesheet
|
||||||
|
]
|
||||||
|
activate :relative_assets
|
||||||
|
activate :directory_indexes
|
||||||
|
"""
|
||||||
|
And a successfully built app at "asset-hash-app"
|
||||||
|
When I cd to "build"
|
||||||
|
Then the following files should exist:
|
||||||
|
| index.html |
|
||||||
|
| subdir/index.html |
|
||||||
|
| images/100px-5fd6fb90.jpg |
|
||||||
|
| javascripts/application-1d8d5276.js |
|
||||||
|
| stylesheets/site-8bc55985.css |
|
||||||
|
And the following files should not exist:
|
||||||
|
| images/100px.jpg |
|
||||||
|
| javascripts/application.js |
|
||||||
|
| stylesheets/site.css |
|
||||||
|
And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px.jpg'"
|
||||||
|
And the file "stylesheets/site-8bc55985.css" should contain:
|
||||||
|
"""
|
||||||
|
background-image: url("../images/100px.jpg")
|
||||||
|
"""
|
||||||
|
And the file "index.html" should contain 'href="stylesheets/site-8bc55985.css"'
|
||||||
|
And the file "index.html" should contain 'src="javascripts/application-1d8d5276.js"'
|
||||||
|
And the file "index.html" should contain 'src="images/100px-5fd6fb90.jpg"'
|
||||||
|
And the file "subdir/index.html" should contain:
|
||||||
|
"""
|
||||||
|
<h2>Image url3:</h2>
|
||||||
|
<p><img src="../images/100px.jpg"></p>
|
||||||
|
"""
|
||||||
|
|
||||||
Scenario: Already minified files should still be hashed
|
Scenario: Already minified files should still be hashed
|
||||||
Given a successfully built app at "asset-hash-minified-app"
|
Given a successfully built app at "asset-hash-minified-app"
|
||||||
When I cd to "build"
|
When I cd to "build"
|
||||||
|
|
|
@ -5,6 +5,7 @@ require 'middleman-core/rack'
|
||||||
class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
||||||
option :exts, %w(.jpg .jpeg .png .gif .webp .js .css .otf .woff .woff2 .eot .ttf .svg .svgz), 'List of extensions that get asset hashes appended to them.'
|
option :exts, %w(.jpg .jpeg .png .gif .webp .js .css .otf .woff .woff2 .eot .ttf .svg .svgz), 'List of extensions that get asset hashes appended to them.'
|
||||||
option :ignore, [], 'Regexes of filenames to skip adding asset hashes to'
|
option :ignore, [], 'Regexes of filenames to skip adding asset hashes to'
|
||||||
|
option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites'
|
||||||
|
|
||||||
def initialize(app, options_hash={}, &block)
|
def initialize(app, options_hash={}, &block)
|
||||||
super
|
super
|
||||||
|
@ -23,6 +24,7 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
||||||
url_extensions: options.exts.sort.reverse,
|
url_extensions: options.exts.sort.reverse,
|
||||||
source_extensions: %w(.htm .html .php .css .js),
|
source_extensions: %w(.htm .html .php .css .js),
|
||||||
ignore: @ignore,
|
ignore: @ignore,
|
||||||
|
rewrite_ignore: options.rewrite_ignore,
|
||||||
middleman_app: app,
|
middleman_app: app,
|
||||||
proc: method(:rewrite_url)
|
proc: method(:rewrite_url)
|
||||||
end
|
end
|
||||||
|
|
|
@ -34,13 +34,16 @@ module Middleman
|
||||||
@source_exts_regex_text = Regexp.union(@source_exts).to_s
|
@source_exts_regex_text = Regexp.union(@source_exts).to_s
|
||||||
|
|
||||||
@ignore = options.fetch(:ignore)
|
@ignore = options.fetch(:ignore)
|
||||||
|
@rewrite_ignore = Array(options.fetch(:rewrite_ignore, []))
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
status, headers, response = @rack_app.call(env)
|
status, headers, response = @rack_app.call(env)
|
||||||
|
|
||||||
# Allow upstream request to skip all rewriting
|
# Allow configuration or upstream request to skip all rewriting
|
||||||
return [status, headers, response] if env['bypass_inline_url_rewriter'] == 'true'
|
if rewrite_ignore?(env['PATH_INFO']) || env['bypass_inline_url_rewriter'] == 'true'
|
||||||
|
return [status, headers, response]
|
||||||
|
end
|
||||||
|
|
||||||
# Allow upstream request to skip this specific rewriting
|
# Allow upstream request to skip this specific rewriting
|
||||||
if @uid
|
if @uid
|
||||||
|
@ -96,6 +99,11 @@ module Middleman
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Contract String => Bool
|
||||||
|
def rewrite_ignore?(path)
|
||||||
|
@rewrite_ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue