Skip to content

Commit

Permalink
Revert "Revert "[close #331] Smarter entry lookup""
Browse files Browse the repository at this point in the history
This reverts commit e295278.
  • Loading branch information
schneems committed Aug 25, 2016
1 parent b729881 commit d30c2e4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/sprockets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ module Sprockets
require 'sprockets/eco_processor'
require 'sprockets/ejs_processor'
require 'sprockets/jst_processor'
register_mime_type 'application/javascript+function', extensions: []
register_mime_type 'text/eco', extensions: ['.eco', '.jst.eco']
register_mime_type 'text/ejs', extensions: ['.ejs', '.jst.ejs']
register_transformer 'text/eco', 'application/javascript+function', EcoProcessor
Expand Down
28 changes: 21 additions & 7 deletions lib/sprockets/path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,30 @@ def match_path_extname(path, extensions)
# Returns an Array of [String path, Object value] matches.
def find_matching_path_for_extensions(path, basename, extensions)
matches = []
entries(path).each do |entry|
next unless File.basename(entry).start_with?(basename)
extname, value = match_path_extname(entry, extensions)
if basename == entry.chomp(extname)
filename = File.join(path, entry)
if file?(filename)
matches << [filename, value]
# Requires a file touch for each extension
# In the case where lots of extensions are given, it may be faster to search the directory
if extensions.length < 3
extensions.each do |(extension, mime_type)|
file = File.join(path, basename + extension)
matches << [file, mime_type] if File.exist?(file)
end
end

# Used when files don't have registered mime types or
# when a large number of extensions would require too many file touches.
if matches.empty?
entries(path).each do |entry|
next unless File.basename(entry).start_with?(basename)
extname, value = match_path_extname(entry, extensions)
if basename == entry.chomp(extname)
filename = File.join(path, entry)
if file?(filename)
matches << [filename, value]
end
end
end
end

matches
end

Expand Down
15 changes: 14 additions & 1 deletion lib/sprockets/resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,20 @@ def resolve_under_paths(paths, logical_name, accepts)
method(:resolve_alts_under_path),
method(:resolve_index_under_path)
]
mime_exts = config[:mime_exts]

mime_exts = {}
accepts.each do |mime, version|
if '*/*'.freeze == mime
mime_exts = config[:mime_exts]
break
end

mime_hash = config[:mime_types][mime]
raise "No valid mime type found for #{ mime.inspect }. Valid types #{ config[:mime_types].keys.inspect }" unless mime_hash
mime_hash[:extensions].each do |extension|
mime_exts[extension] = mime
end
end

paths.each do |load_path|
candidates = []
Expand Down
4 changes: 2 additions & 2 deletions test/test_path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_entries
"source-maps",
"symlink"
], entries(File.expand_path("../fixtures", __FILE__))

[ ['a', 'b'], ['a', 'b', '.', '..'] ].each do |dir_contents|
Dir.stub :entries, dir_contents do
assert_equal ['a', 'b'], entries(Dir.tmpdir)
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_find_matching_path_for_extensions
assert_equal [
["#{dirname}/hello.jst.ejs", "application/ejs"],
["#{dirname}/hello.txt", "text/plain"]
], find_matching_path_for_extensions(dirname, "hello", extensions)
].sort, find_matching_path_for_extensions(dirname, "hello", extensions).sort

end

Expand Down

0 comments on commit d30c2e4

Please sign in to comment.