Skip to content

Commit

Permalink
Deduplicate paths (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenogazzola authored Feb 19, 2022
1 parent 4433b73 commit d61834e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit d61834e

Please sign in to comment.