Skip to content

Commit

Permalink
PathUtils: *always* prefer longer to shorter extension match
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
David Judd authored and schneems committed Jul 1, 2016
1 parent dd4191a commit 697269c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/sprockets/path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/test_path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 697269c

Please sign in to comment.