Skip to content

Commit

Permalink
Revert "Add support for asset host (#118)"
Browse files Browse the repository at this point in the history
This reverts commit 70c0c4c.
  • Loading branch information
brenogazzola committed Mar 6, 2023
1 parent 56d718a commit 3511570
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
7 changes: 3 additions & 4 deletions lib/propshaft/compilers/css_asset_urls.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# frozen_string_literal: true

class Propshaft::Compilers::CssAssetUrls
attr_reader :assembly, :url_prefix
attr_reader :assembly

ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|%23|data|http|\/\/))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)/

def initialize(assembly)
@assembly = assembly
@url_prefix = File.join(assembly.config.host.to_s, assembly.config.prefix.to_s).chomp("/")
@assembly = assembly
end

def compile(logical_path, input)
Expand All @@ -27,7 +26,7 @@ def resolve_path(directory, filename)

def asset_url(resolved_path, logical_path, fingerprint, pattern)
if asset = assembly.load_path.find(resolved_path)
%[url("#{url_prefix}/#{asset.digested_path}#{fingerprint}")]
%[url("#{assembly.config.prefix}/#{asset.digested_path}#{fingerprint}")]
else
Propshaft.logger.warn "Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}"
%[url("#{pattern}")]
Expand Down
2 changes: 0 additions & 2 deletions lib/propshaft/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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
Expand All @@ -31,7 +30,6 @@ 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))

Expand Down
17 changes: 5 additions & 12 deletions test/propshaft/compilers/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

class Propshaft::Compilers::CssAssetUrlsTest < ActiveSupport::TestCase
setup do
@options = ActiveSupport::OrderedOptions.new.tap { |config|
@assembly = Propshaft::Assembly.new(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
Expand Down Expand Up @@ -118,23 +120,14 @@ 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")
logical_path = "foobar/source/test.css"

asset = Propshaft::Asset.new(root_path.join(logical_path), logical_path: logical_path)
asset.stub :content, content do
assembly = Propshaft::Assembly.new(@options)
assembly.compilers.register "text/css", Propshaft::Compilers::CssAssetUrls
assembly.compilers.compile(asset)
@assembly.compilers.compile(asset)
end
end
end

0 comments on commit 3511570

Please sign in to comment.