1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
# frozen_string_literal: true
module Roadie
module Rails
class AssetPropshaftProvider
include Roadie::AssetProvider
def initialize(assembly)
@assembly = assembly
@stylesheets = {}
end
def find_stylesheet(stylesheet_asset_path)
stylesheet = @stylesheets[stylesheet_asset_path]
return stylesheet if stylesheet.present?
path, digest = extract_path_and_digest(stylesheet_asset_path)
asset = @assembly.load_path.find(path)
if asset.present? && asset.fresh?(digest)
compiled_content = @assembly.compilers.compile(asset)
stylesheet = Stylesheet.new(stylesheet_asset_path, compiled_content.force_encoding("UTF-8"))
@stylesheets[stylesheet_asset_path] = stylesheet
end
stylesheet
end
private
def extract_path_and_digest(stylesheet_asset_path)
full_path = stylesheet_asset_path.sub(%r{#{::Rails.configuration.assets.prefix}/}, "")
digest = full_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)[^.]+\z/, 1]
path = digest ? full_path.sub("-#{digest}", "") : full_path
[path, digest]
end
end
end
end
|