Skip to content

Commit

Permalink
Preserve asset paths order when deduping (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenogazzola authored Feb 22, 2022
1 parent fb414e8 commit eb8b1e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 6 additions & 5 deletions 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 = dedup(paths)
@paths = dedup(paths)
@version = version
end

Expand Down Expand Up @@ -67,10 +67,11 @@ def clear_cache
end

def dedup(paths)
[].tap do |deduped|
Array(paths).map(&:to_s).sort.each do |path|
deduped << Pathname.new(path) if deduped.blank? || !path.start_with?(deduped.last.to_s)
end
paths = Array(paths).map { |path| Pathname.new(path) }
deduped = [].tap do |deduped|
paths.sort.each { |path| deduped << path if deduped.blank? || !path.to_s.start_with?(deduped.last.to_s) }
end

paths & deduped
end
end
10 changes: 5 additions & 5 deletions test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ class Propshaft::LoadPathTest < ActiveSupport::TestCase

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

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
assert_equal Pathname.new("app/javascript"), paths.first
assert_equal Pathname.new("app/assets"), paths.last
end

private
Expand Down

0 comments on commit eb8b1e2

Please sign in to comment.