|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "sprockets/manifest" |
| 4 | + |
| 5 | +module NonStupidDigestAssets |
| 6 | + mattr_accessor :whitelist |
| 7 | + @@whitelist = [] |
| 8 | + |
| 9 | + class << self |
| 10 | + def assets(assets) |
| 11 | + return assets if whitelist.empty? |
| 12 | + |
| 13 | + whitelisted_assets(assets) |
| 14 | + end |
| 15 | + |
| 16 | + private |
| 17 | + |
| 18 | + def whitelisted_assets(assets) |
| 19 | + assets.select do |logical_path, _digest_path| |
| 20 | + whitelist.any? do |item| |
| 21 | + item == logical_path |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + module CompileWithNonDigest |
| 28 | + def compile(*args) |
| 29 | + paths = super |
| 30 | + NonStupidDigestAssets.assets(assets).each do |(logical_path, digest_path)| |
| 31 | + full_digest_path = File.join dir, digest_path |
| 32 | + full_digest_gz_path = "#{full_digest_path}.gz" |
| 33 | + full_non_digest_path = File.join dir, logical_path |
| 34 | + full_non_digest_gz_path = "#{full_non_digest_path}.gz" |
| 35 | + |
| 36 | + if File.exist? full_digest_path |
| 37 | + logger.debug "Writing #{full_non_digest_path}" |
| 38 | + FileUtils.copy_file full_digest_path, full_non_digest_path, :preserve_attributes |
| 39 | + else |
| 40 | + logger.debug "Could not find: #{full_digest_path}" |
| 41 | + end |
| 42 | + if File.exist? full_digest_gz_path |
| 43 | + logger.debug "Writing #{full_non_digest_gz_path}" |
| 44 | + FileUtils.copy_file full_digest_gz_path, full_non_digest_gz_path, :preserve_attributes |
| 45 | + else |
| 46 | + logger.debug "Could not find: #{full_digest_gz_path}" |
| 47 | + end |
| 48 | + end |
| 49 | + paths |
| 50 | + end |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +Sprockets::Manifest.prepend NonStupidDigestAssets::CompileWithNonDigest |
0 commit comments