Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate paths #71

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/propshaft/load_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Propshaft::LoadPath
attr_reader :paths, :version

def initialize(paths = [], version: nil)
@paths = Array(paths).collect { |path| Pathname.new(path) }
@paths = dedup(paths)
@version = version
end

Expand Down Expand Up @@ -65,4 +65,12 @@ def without_dotfiles(files)
def clear_cache
@cached_assets_by_path = nil
end

def dedup(paths)
[].tap do |deduped|
Array(paths).sort.each do |path|
deduped << Pathname.new(path) if deduped.blank? || !path.to_s.start_with?(deduped.last.to_s)
end
end
end
end
15 changes: 15 additions & 0 deletions test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ class Propshaft::LoadPathTest < ActiveSupport::TestCase
assert_nil Propshaft::LoadPath.new(Pathname.new("#{__dir__}/../fixtures/assets/nowhere")).find("missing")
end

test "deduplicate paths" do
load_path = Propshaft::LoadPath.new [
"app/assets/stylesheets",
"app/assets/images",
"app/assets",
"app/javascript",
"app/javascript/packs"
]

paths = load_path.paths
assert_equal 2, paths.count
assert_equal Pathname.new("app/assets"), paths.first
assert_equal Pathname.new("app/javascript"), paths.last
end

private
def find_asset(logical_path)
Propshaft::Asset.new(
Expand Down