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

Cleanup pre-digested versions #231

Closed
wants to merge 1 commit into from
Closed
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: 6 additions & 4 deletions lib/propshaft/output_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* this is css */

.btn {
background-image: asset-path("archive.svg");
}
18 changes: 17 additions & 1 deletion test/propshaft/output_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down