From eb8b1e219ff28b158d7e4e6ac8f12284e9087043 Mon Sep 17 00:00:00 2001 From: Breno Gazzola Date: Tue, 22 Feb 2022 08:20:42 -0300 Subject: [PATCH] Preserve asset paths order when deduping (#75) --- lib/propshaft/load_path.rb | 11 ++++++----- test/propshaft/load_path_test.rb | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/propshaft/load_path.rb b/lib/propshaft/load_path.rb index a71ef1c..4d89cb7 100644 --- a/lib/propshaft/load_path.rb +++ b/lib/propshaft/load_path.rb @@ -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 @@ -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 diff --git a/test/propshaft/load_path_test.rb b/test/propshaft/load_path_test.rb index 6bc1bf6..8ab8cf4 100644 --- a/test/propshaft/load_path_test.rb +++ b/test/propshaft/load_path_test.rb @@ -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