From fa5af9039cd30564791f193902edbf9e5e990fa9 Mon Sep 17 00:00:00 2001 From: Breno Gazzola Date: Mon, 14 Nov 2022 14:49:13 -0300 Subject: [PATCH] Add support for asset host --- lib/propshaft/compilers/css_asset_urls.rb | 7 ++++--- lib/propshaft/railtie.rb | 2 ++ test/propshaft/compilers/css_asset_urls_test.rb | 17 ++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/propshaft/compilers/css_asset_urls.rb b/lib/propshaft/compilers/css_asset_urls.rb index 1132b5ad..7c8815fd 100644 --- a/lib/propshaft/compilers/css_asset_urls.rb +++ b/lib/propshaft/compilers/css_asset_urls.rb @@ -1,12 +1,13 @@ # frozen_string_literal: true class Propshaft::Compilers::CssAssetUrls - attr_reader :assembly + attr_reader :assembly, :url_prefix ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|%23|data|http|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/ def initialize(assembly) - @assembly = assembly + @assembly = assembly + @url_prefix = File.join(assembly.config.host.to_s, assembly.config.prefix.to_s).chomp("/") end def compile(logical_path, input) @@ -26,7 +27,7 @@ def resolve_path(directory, filename) def asset_url(resolved_path, logical_path, fingerprint, pattern) if asset = assembly.load_path.find(resolved_path) - %[url("#{assembly.config.prefix}/#{asset.digested_path}#{fingerprint}")] + %[url("#{url_prefix}/#{asset.digested_path}#{fingerprint}")] else Propshaft.logger.warn "Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}" %[url("#{pattern}")] diff --git a/lib/propshaft/railtie.rb b/lib/propshaft/railtie.rb index 4fe2954b..78db6ec8 100644 --- a/lib/propshaft/railtie.rb +++ b/lib/propshaft/railtie.rb @@ -17,6 +17,7 @@ class Railtie < ::Rails::Railtie ] config.assets.sweep_cache = Rails.env.development? config.assets.server = Rails.env.development? || Rails.env.test? + config.assets.host = nil # Register propshaft initializer to copy the assets path in all the Rails Engines. # This makes possible for us to keep all `assets` config in this Railtie, but still @@ -30,6 +31,7 @@ class Railtie < ::Rails::Railtie end config.after_initialize do |app| + config.assets.host = app.config.asset_host config.assets.output_path ||= Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix)) diff --git a/test/propshaft/compilers/css_asset_urls_test.rb b/test/propshaft/compilers/css_asset_urls_test.rb index 6904530a..577be39e 100644 --- a/test/propshaft/compilers/css_asset_urls_test.rb +++ b/test/propshaft/compilers/css_asset_urls_test.rb @@ -6,13 +6,11 @@ class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase setup do - @assembly = Propshaft::Assembly.new(ActiveSupport::OrderedOptions.new.tap { |config| + @options = ActiveSupport::OrderedOptions.new.tap { |config| config.paths = [ Pathname.new("#{__dir__}/../../fixtures/assets/vendor") ] config.output_path = Pathname.new("#{__dir__}/../../fixtures/output") config.prefix = "/assets" - }) - - @assembly.compilers.register "text/css", Propshaft::Compilers::CssAssetUrls + } end test "basic" do @@ -120,6 +118,13 @@ class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase assert_match(/{ background: url\("file-not-found.jpg"\); }/, compiled) end + test "host" do + @options.host = "https://example.com" + + compiled = compile_asset_with_content(%({ background: url(file.jpg); })) + assert_match(/{ background: url\("https:\/\/example.com\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled) + end + private def compile_asset_with_content(content) root_path = Pathname.new("#{__dir__}/../../fixtures/assets/vendor") @@ -127,7 +132,9 @@ def compile_asset_with_content(content) asset = Propshaft::Asset.new(root_path.join(logical_path), logical_path: logical_path) asset.stub :content, content do - @assembly.compilers.compile(asset) + assembly = Propshaft::Assembly.new(@options) + assembly.compilers.register "text/css", Propshaft::Compilers::CssAssetUrls + assembly.compilers.compile(asset) end end end