From 697269cf81e5261fdd7072e32bd489403027fd7e Mon Sep 17 00:00:00 2001 From: David Judd Date: Wed, 29 Jun 2016 13:35:36 -0700 Subject: [PATCH] PathUtils: *always* prefer longer to shorter extension match The older logic would *usually* prefer a longer to a shorter extension match, but not in the case with a three-or-more-component extension when one of the intermediate extensions wasn't recognized, e.g. ".coffee" would be preferred over ".js.jsx.coffee" unless ".jsx.coffee" was also a recognized extension. (".js.jsx.coffee" is the preferred Coffee+React extension with the react-rails gem) --- lib/sprockets/path_utils.rb | 19 +++++++++++-------- test/test_path_utils.rb | 3 +++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/sprockets/path_utils.rb b/lib/sprockets/path_utils.rb index 53d19c6e6..f6427fac3 100644 --- a/lib/sprockets/path_utils.rb +++ b/lib/sprockets/path_utils.rb @@ -148,16 +148,19 @@ def path_extnames(path) # # Returns [String extname, Object value] or nil nothing matched. def match_path_extname(path, extensions) - match, key = nil, "" - path_extnames(path).reverse_each do |extname| - key.prepend(extname) - if value = extensions[key] - match = [key.dup, value] - elsif match - break + basename = File.basename(path) + + i = basename.index('.'.freeze) + while i && i < basename.length - 1 + extname = basename[i..-1] + if value = extensions[extname] + return extname, value end + + i = basename.index('.'.freeze, i+1) end - match + + nil end # Internal: Returns all parents for path diff --git a/test/test_path_utils.rb b/test/test_path_utils.rb index 59f3b5154..e6362d92d 100644 --- a/test/test_path_utils.rb +++ b/test/test_path_utils.rb @@ -170,6 +170,9 @@ def test_match_path_extname refute match_path_extname("jquery.map", extensions) refute match_path_extname("jquery.map.js", extensions) refute match_path_extname("jquery.map.css", extensions) + + extensions = { ".coffee" => "application/coffeescript", ".js" => "application/javascript", ".js.jsx.coffee" => "application/jsx+coffee" } + assert_equal [".js.jsx.coffee", "application/jsx+coffee"], match_path_extname("component.js.jsx.coffee", extensions) end def test_path_parents