Merge pull request #1268 from AndrewKvalheim/gzip-ignore

Excluding paths from gzipping
This commit is contained in:
Ben Hollis 2014-06-25 21:57:26 -07:00
commit 32bdc43ccb
3 changed files with 47 additions and 6 deletions

View file

@ -3,12 +3,12 @@ Feature: GZIP assets during build
Scenario: Built assets should be gzipped Scenario: Built assets should be gzipped
Given a successfully built app at "gzip-app" Given a successfully built app at "gzip-app"
Then the following files should exist: Then the following files should exist:
| build/javascripts/test.js.gz | | build/index.html |
| build/stylesheets/test.css.gz |
| build/index.html.gz | | build/index.html.gz |
| build/javascripts/test.js | | build/javascripts/test.js |
| build/javascripts/test.js.gz |
| build/stylesheets/test.css | | build/stylesheets/test.css |
| build/index.html | | build/stylesheets/test.css.gz |
When I run `file build/javascripts/test.js.gz` When I run `file build/javascripts/test.js.gz`
Then the output should contain "gzip" Then the output should contain "gzip"
@ -19,3 +19,34 @@ Feature: GZIP assets during build
When I go to "/stylesheets/test.css" When I go to "/stylesheets/test.css"
Then I should see "test_selector" Then I should see "test_selector"
Scenario: Only specified extensions should be gzipped
Given a fixture app "gzip-app"
And a file named "config.rb" with:
"""
activate :gzip, exts: %w(.js .html .htm)
"""
And a successfully built app at "gzip-app"
Then the following files should exist:
| build/index.html |
| build/index.html.gz |
| build/javascripts/test.js |
| build/javascripts/test.js.gz |
| build/stylesheets/test.css |
And the following files should not exist:
| build/stylesheets/test.css.gz |
Scenario: Gzipped files are not produced for ignored paths
Given a fixture app "gzip-app"
And a file named "config.rb" with:
"""
activate :gzip, ignore: ['index.html', %r(javascripts/.*)]
"""
And a successfully built app at "gzip-app"
Then the following files should exist:
| build/index.html |
| build/javascripts/test.js |
| build/stylesheets/test.css |
| build/stylesheets/test.css.gz |
And the following files should not exist:
| build/index.html.gz |
| build/javascripts/test.js.gz |

View file

@ -2,7 +2,7 @@ require 'middleman-core/util'
class Middleman::Extensions::AssetHash < ::Middleman::Extension class Middleman::Extensions::AssetHash < ::Middleman::Extension
option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), 'List of extensions that get asset hashes appended to them.' option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), 'List of extensions that get asset hashes appended to them.'
option :ignore, [], 'Regexes of filenames to skip adding asset hashes to' option :ignore, [], 'Patterns to avoid adding asset hashes to'
def initialize(app, options_hash={}, &block) def initialize(app, options_hash={}, &block)
super super

View file

@ -11,6 +11,7 @@
# #
class Middleman::Extensions::Gzip < ::Middleman::Extension class Middleman::Extensions::Gzip < ::Middleman::Extension
option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.' option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.'
option :ignore, [], 'Patterns to avoid gzipping'
def initialize(app, options_hash={}) def initialize(app, options_hash={})
super super
@ -29,7 +30,7 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension
# Fill a queue with inputs # Fill a queue with inputs
in_queue = Queue.new in_queue = Queue.new
paths.each do |path| paths.each do |path|
in_queue << path if options.exts.include?(path.extname) in_queue << path if should_gzip?(path)
end end
num_paths = in_queue.size num_paths = in_queue.size
@ -93,4 +94,13 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension
[output_filename, old_size, new_size] [output_filename, old_size, new_size]
end end
private
# Whether a path should be gzipped
# @param [Pathname] path A destination path
# @return [Boolean]
def should_gzip?(path)
options.exts.include?(path.extname) && options.ignore.none? { |ignore| Middleman::Util.path_match(ignore, path.to_s) }
end
end end