From d30c2e490bd960e1f60cd044913a2781303c5e30 Mon Sep 17 00:00:00 2001 From: schneems Date: Thu, 25 Aug 2016 10:47:59 -0500 Subject: [PATCH] Revert "Revert "[close #331] Smarter entry lookup"" This reverts commit e2952781d383f286b55c532d2347f42fa6679cfd. --- lib/sprockets.rb | 1 + lib/sprockets/path_utils.rb | 28 +++++++++++++++++++++------- lib/sprockets/resolve.rb | 15 ++++++++++++++- test/test_path_utils.rb | 4 ++-- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/sprockets.rb b/lib/sprockets.rb index de506a4be..eba0158ac 100644 --- a/lib/sprockets.rb +++ b/lib/sprockets.rb @@ -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 diff --git a/lib/sprockets/path_utils.rb b/lib/sprockets/path_utils.rb index 467917de8..07448bbeb 100644 --- a/lib/sprockets/path_utils.rb +++ b/lib/sprockets/path_utils.rb @@ -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 diff --git a/lib/sprockets/resolve.rb b/lib/sprockets/resolve.rb index 95468c5d7..a1ec911ae 100644 --- a/lib/sprockets/resolve.rb +++ b/lib/sprockets/resolve.rb @@ -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 = [] diff --git a/test/test_path_utils.rb b/test/test_path_utils.rb index f1103f15a..629718c2d 100644 --- a/test/test_path_utils.rb +++ b/test/test_path_utils.rb @@ -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) @@ -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