diff --git a/lib/propshaft/output_path.rb b/lib/propshaft/output_path.rb index f063134..6bb319b 100644 --- a/lib/propshaft/output_path.rb +++ b/lib/propshaft/output_path.rb @@ -25,11 +25,13 @@ def clean(count, age) def files Hash.new.tap do |files| all_files_from_tree(path).each do |file| - digested_path = file.relative_path_from(path) - logical_path, digest = Propshaft::Asset.extract_path_and_digest(digested_path.to_s) + digested_path = file.relative_path_from(path).to_s - files[digested_path.to_s] = { - logical_path: logical_path.to_s, + digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(digested\.)?([^.]|.map)+\z/, 1] + logical_path = digest ? digested_path.sub("-#{digest}", "") : digested_path + + files[digested_path] = { + logical_path: logical_path, digest: digest, mtime: File.mtime(file) } diff --git a/test/fixtures/output/file-already-abcdefVWXYZ0123456789_-.digested.css b/test/fixtures/output/file-already-abcdefVWXYZ0123456789_-.digested.css new file mode 100644 index 0000000..cb4cc48 --- /dev/null +++ b/test/fixtures/output/file-already-abcdefVWXYZ0123456789_-.digested.css @@ -0,0 +1,5 @@ +/* this is css */ + +.btn { + background-image: asset-path("archive.svg"); +} diff --git a/test/propshaft/output_path_test.rb b/test/propshaft/output_path_test.rb index 49f1c83..40057b0 100644 --- a/test/propshaft/output_path_test.rb +++ b/test/propshaft/output_path_test.rb @@ -9,7 +9,8 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase @manifest = { ".manifest.json": ".manifest.json", "one.txt": "one-f2e1ec14.txt", - "one.txt.map": "one-f2e1ec15.txt.map" + "one.txt.map": "one-f2e1ec15.txt.map", + "file-already-abcdefVWXYZ0123456789_-.digested.css": "file-already-abcdefVWXYZ0123456789_-.digested.css" }.stringify_keys @output_path = Propshaft::OutputPath.new(Pathname.new("#{__dir__}/../fixtures/output"), @manifest) end @@ -21,6 +22,8 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase assert_equal "one.txt", file[:logical_path] assert_equal "f2e1ec14", file[:digest] assert file[:mtime].is_a?(Time) + + assert files["file-already-abcdefVWXYZ0123456789_-.digested.css"] end test "clean always keeps most current versions" do @@ -79,6 +82,19 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase FileUtils.rm(current) if File.exist?(current) end + test "clean keeps the correct number of predigested versions" do + old = output_asset("Vue3Lottie-23af1d5aa7baee8b1ca8.digested.js", "old", created_at: Time.now - 300) + current = output_asset("Vue3Lottie-337a4df42c71a547bccd.digested.js", "current", created_at: Time.now - 180) + + @output_path.clean(1, 0) + + assert File.exist?(current), "#{current} should still exist" + assert_not File.exist?(old), "#{old} should be removed" + ensure + FileUtils.rm(old) if File.exist?(old) + FileUtils.rm(current) if File.exist?(current) + end + private def output_asset(filename, content, created_at: Time.now) load_path = Propshaft::LoadPath.new([], compilers: Propshaft::Compilers.new(nil))