Skip to content

Commit

Permalink
Prioritize app assets over engine assets (#206)
Browse files Browse the repository at this point in the history
* Prioritize app assets over engine assets

* Explain path sorting

---------

Co-authored-by: David Heinemeier Hansson <[email protected]>
  • Loading branch information
brunoprietog and dhh authored Sep 23, 2024
1 parent 73f01f4 commit 48df362
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/propshaft/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Railtie < ::Rails::Railtie
end

config.after_initialize do |app|
# Prioritize assets from within the application over assets of the same path from engines/gems.
config.assets.paths.sort_by! { |path| path.to_s.start_with?(Rails.root.to_s) ? 0 : 1 }

config.assets.relative_url_root ||= app.config.relative_url_root
config.assets.output_path ||=
Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix))
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/sample/load_real_assets.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
<p>Find me in app/views/sample/load_real_assets.html.erb</p>

<%= javascript_include_tag "hello_world" %>
<%= javascript_include_tag "actioncable" %>
2 changes: 1 addition & 1 deletion test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# require "action_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "action_cable/engine"
# require "sprockets/railtie"
require "rails/test_unit/railtie"

Expand Down
1 change: 1 addition & 0 deletions test/dummy/lib/assets/javascripts/actioncable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("actioncable.js")
6 changes: 6 additions & 0 deletions test/propshaft_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class PropshaftIntegrationTest < ActionDispatch::IntegrationTest
assert_select 'script[src="/assets/hello_world-888761f8.js"]'
end

test "should prioritize app assets over engine assets" do
get sample_load_real_assets_url

assert_select 'script[src="/assets/actioncable-2e7de4f9.js"]'
end

test "should find app styles via glob" do
get sample_load_real_assets_url

Expand Down

0 comments on commit 48df362

Please sign in to comment.