From 429e7d64bd306c5139748ad578c47616bc4dfef2 Mon Sep 17 00:00:00 2001 From: Andrew Kvalheim Date: Fri, 2 May 2014 11:41:59 -0700 Subject: [PATCH] Accept list of paths to exclude from gzipping. --- middleman-core/features/gzip.feature | 16 ++++++++++++++++ .../lib/middleman-more/extensions/gzip.rb | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/middleman-core/features/gzip.feature b/middleman-core/features/gzip.feature index 9240b05b..5a5a4265 100644 --- a/middleman-core/features/gzip.feature +++ b/middleman-core/features/gzip.feature @@ -34,3 +34,19 @@ Feature: GZIP assets during build | 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 | diff --git a/middleman-core/lib/middleman-more/extensions/gzip.rb b/middleman-core/lib/middleman-more/extensions/gzip.rb index 21b40f06..ed875d19 100644 --- a/middleman-core/lib/middleman-more/extensions/gzip.rb +++ b/middleman-core/lib/middleman-more/extensions/gzip.rb @@ -11,6 +11,7 @@ # class Middleman::Extensions::Gzip < ::Middleman::Extension option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.' + option :ignore, [], 'Patterns to avoid gzipping' def initialize(app, options_hash={}) super @@ -29,7 +30,7 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension # Fill a queue with inputs in_queue = Queue.new paths.each do |path| - in_queue << path if options.exts.include?(path.extname) + in_queue << path if should_gzip?(path) end num_paths = in_queue.size @@ -93,4 +94,13 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension [output_filename, old_size, new_size] 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