Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove prepended forward slash when mapping.path is empty #192

Merged
merged 3 commits into from
Oct 27, 2023

Conversation

jsntv200
Copy link
Contributor

While using ViewComponents I ran into an issue with sidecar loaded js files not being mapped correctly when using importmap-rails and propshaft. The issue has been raised in Propshaft Issue #87 and ViewComponents Issue #1064 neither of which provided solutions I wanted to use.

Digging into it I found that using the following config nearly produced the desired output:

# config/initializers/assets.rb

Rails.application.config.assets.paths << "app/components"

# config/importmap.rb

pin_all_from "app/components", under: "controllers", to: ""

# bin/importmap json

{
  "imports": {
    "controllers/button/component_controller": "/button/component_controller.js"
  }
}

I assume Propshaft interprets the path as absolute and fails to digest it.

This fix just adds reject(&:empty?) to remove empty strings allowing mapping.path to essentially overwrite mapping.under allowing the path to be resolved by Propshaft.

After fix:

# bin/importmap json

{
  "imports": {
    "controllers/button/component_controller": "/assets/button/component_controller-c4927eee0e86a379df3997e9f426970d37d60dc2.js"
  }
}

The test covers the ViewComponent scenario of a directory located at app/components which is the reason for passing to: "" in the first place.

If there's a legitimate reason for requiring the forward slash then I assume to: "/" would still work.

@rafaelfranca rafaelfranca merged commit f80d0a3 into rails:main Oct 27, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants