From 66921d92bbfdc29fac8ca4abfb16a0d9a2e95621 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Tue, 6 Dec 2022 20:21:58 +0100 Subject: [PATCH] Fix `assets:precompile` to be idempotent again Each execution regenerates the manifest, so that we also need to rewrite the hashes. --- lib/tasks/sourcemap.rake | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/tasks/sourcemap.rake b/lib/tasks/sourcemap.rake index 48cc6b43..6ddc48db 100644 --- a/lib/tasks/sourcemap.rake +++ b/lib/tasks/sourcemap.rake @@ -48,11 +48,17 @@ namespace :assets do end # Read the source map file and append the source map link - existing_file_content = file.readlines - next if existing_file_content.blank? || existing_file_content[-1].include?(mapping_string) + existing_file_content = file.readlines.map(&:strip) - new_content = existing_file_content + ["\n", mapping_string] - write_asset(file, new_content.join, manifest) + if existing_file_content.blank? || existing_file_content[-1] == mapping_string + # Just "restore" the integrity hash + mtime = Sprockets::PathUtils.stat(file)&.mtime + manifest[File.basename(file)].merge!(changed_manifest(existing_file_content.join("\n"), mtime)) + else + # Append the source map link to the file + new_content = existing_file_content + [mapping_string] + write_asset(file, new_content.join("\n"), manifest) + end end # We need to write the manifest file again to include the new integrity hashes