Skip to content

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.

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