Implement and test asset_hash ignore option

This commit is contained in:
Thomas Reynolds 2012-05-26 15:08:59 -07:00
parent 0788ebce15
commit 98fc81beef
5 changed files with 45 additions and 31 deletions

View file

@ -4,11 +4,12 @@ Feature: Assets get a file hash appended to their and references to them are upd
When I cd to "build"
Then the following files should exist:
| index.html |
| apple-touch-icon.png |
| images/100px-1242c368.png |
| images/100px-5fd6fb90.jpg |
| images/100px-5fd6fb90.gif |
| javascripts/application-1d8d5276.js |
| stylesheets/site-ad4a5abd.css |
| stylesheets/site-50eaa978.css |
| index.html |
| subdir/index.html |
| other/index.html |
@ -20,46 +21,48 @@ Feature: Assets get a file hash appended to their and references to them are upd
| stylesheets/site.css |
And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px-5fd6fb90.jpg'"
And the file "stylesheets/site-ad4a5abd.css" should contain "background-image: url('../images/100px-5fd6fb90.jpg')"
And the file "index.html" should contain 'href="stylesheets/site-ad4a5abd.css"'
And the file "stylesheets/site-50eaa978.css" should contain "background-image: url('../images/100px-5fd6fb90.jpg')"
And the file "index.html" should contain 'href="apple-touch-icon.png"'
And the file "index.html" should contain 'href="stylesheets/site-50eaa978.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 'href="../stylesheets/site-ad4a5abd.css"'
And the file "subdir/index.html" should contain 'href="../stylesheets/site-50eaa978.css"'
And the file "subdir/index.html" should contain 'src="../javascripts/application-1d8d5276.js"'
And the file "subdir/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"'
And the file "other/index.html" should contain 'href="../stylesheets/site-ad4a5abd.css"'
And the file "other/index.html" should contain 'href="../stylesheets/site-50eaa978.css"'
And the file "other/index.html" should contain 'src="../javascripts/application-1d8d5276.js"'
And the file "other/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"'
Scenario: Hashed assets work in preview server
Given the Server is running at "asset-hash-app"
When I go to "/"
Then I should see 'href="stylesheets/site-ad4a5abd.css"'
Then I should see 'href="apple-touch-icon.png"'
And I should see 'href="stylesheets/site-50eaa978.css"'
And I should see 'src="javascripts/application-1d8d5276.js"'
And I should see 'src="images/100px-5fd6fb90.jpg"'
When I go to "/subdir/"
Then I should see 'href="../stylesheets/site-ad4a5abd.css"'
Then I should see 'href="../stylesheets/site-50eaa978.css"'
And I should see 'src="../javascripts/application-1d8d5276.js"'
And I should see 'src="../images/100px-5fd6fb90.jpg"'
When I go to "/other/"
Then I should see 'href="../stylesheets/site-ad4a5abd.css"'
Then I should see 'href="../stylesheets/site-50eaa978.css"'
And I should see 'src="../javascripts/application-1d8d5276.js"'
And I should see 'src="../images/100px-5fd6fb90.jpg"'
When I go to "/javascripts/application-1d8d5276.js"
Then I should see "img.src = '/images/100px-5fd6fb90.jpg'"
When I go to "/stylesheets/site-ad4a5abd.css"
When I go to "/stylesheets/site-50eaa978.css"
Then I should see "background-image: url('../images/100px-5fd6fb90.jpg')"
Scenario: Enabling an asset host still produces hashed files and references
Given the Server is running at "asset-hash-host-app"
When I go to "/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-1bac75d8.css"'
Then I should see 'href="http://middlemanapp.com/stylesheets/site-171eb3c0.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/subdir/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-1bac75d8.css"'
Then I should see 'href="http://middlemanapp.com/stylesheets/site-171eb3c0.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/other/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-1bac75d8.css"'
Then I should see 'href="http://middlemanapp.com/stylesheets/site-171eb3c0.css"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
# Asset helpers don't appear to work from Compass right now
# When I go to "/stylesheets/site-e5a31a3e.css"
@ -73,12 +76,11 @@ Feature: Assets get a file hash appended to their and references to them are upd
font-size: 14px
"""
When I go to "/partials/"
Then I should see 'href="../stylesheets/uses_partials-b1ef0501.css'
Then I should see 'href="../stylesheets/uses_partials-423a00f7.css'
And the file "source/stylesheets/_partial.sass" has the contents
"""
body
font-size: 18px !important
"""
When I go to "/partials/"
Then I should see 'href="../stylesheets/uses_partials-05453ae6.css'
Then I should see 'href="../stylesheets/uses_partials-e8c3d4eb.css'

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

View file

@ -5,6 +5,9 @@
<h2>Image url:</h2>
<img src="<%= image_path('100px.jpg') %>">
<h2>Ignored path:</h2>
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<h2>Autogenerated tests:</h2>
<p>images/100px.jpg</p>
<p>/images/100px.jpg</p>

View file

@ -43,6 +43,9 @@ module Middleman
# Default output style
config.output_style = :nested
# No line-comments in test mode (changing paths mess with sha1)
config.line_comments = false if ENV["TEST"]
if respond_to?(:asset_host) && asset_host.is_a?(Proc)
config.asset_host(&asset_host)
end

View file

@ -14,7 +14,7 @@ module Middleman
:asset_hash,
AssetHashManager.new(self, exts, ignore)
)
use Middleware, :exts => exts, :middleman_app => self
use Middleware, :exts => exts, :middleman_app => self, :ignore => ignore
end
end
alias :included :registered
@ -31,15 +31,16 @@ module Middleman
# @return [void]
def manipulate_resource_list(resources)
resources.each do |resource|
if @exts.include? resource.ext
if resource.template? # if it's a template, render it out
digest = Digest::SHA1.hexdigest(resource.render)[0..7]
else # if it's a static file, just hash it
digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7]
end
next unless @exts.include? resource.ext
next if @ignore.any? { |r| resource.destination_path.match(r) }
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
if resource.template? # if it's a template, render it out
digest = Digest::SHA1.hexdigest(resource.render)[0..7]
else # if it's a static file, just hash it
digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7]
end
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end
end
end
@ -48,10 +49,11 @@ module Middleman
# assets to include their new, hashed name.
class Middleware
def initialize(app, options={})
@rack_app = app
@exts = options[:exts]
@rack_app = app
@exts = options[:exts]
@ignore = options[:ignore]
@exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|')
@middleman_app = options[:middleman_app]
@middleman_app = options[:middleman_app]
end
def call(env)
@ -66,16 +68,20 @@ module Middleman
if body
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK?
body.gsub! /([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/ do |match|
opening_character = $1
asset_path = $2
relative_path = Pathname.new(asset_path).relative?
asset_path = dirpath.join(asset_path).to_s if relative_path
if asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path)
if @ignore.any? { |r| asset_path.match(r) }
match
elsif asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path)
replacement_path = "/#{asset_page.destination_path}"
replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path
"#{$1}#{replacement_path}"
"#{opening_character}#{replacement_path}"
else
match
end