rails / propshaft

Deliver assets for Rails
MIT License
864 stars 93 forks source link

Exclude certain assets declared in importmap #197

Closed GyozaGuy closed 1 month ago

GyozaGuy commented 1 month ago

I've been trying to make the switch to Propshaft and importmaps for all of my new projects and overall I've really liked it, but there are two issues I haven't been able to figure out quite yet and could use some help.

For Shoelace, I'm trying to use the autoloader script instead of importing everything at once or cherry-picking components. This is my entry for Shoelace in importmap.rb:

pin 'shoelace', to: 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.1/cdn/shoelace-autoloader.js'

When trying to use a component, I get an error like this in the console:

GET https://cdn.jsdelivr.net/components/button/button.js net::ERR_ABORTED 404 (Not Found)

The URL should be https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.1/cdn/components/button/button.js, but most of that is being removed, I assume by Propshaft.

For Leaflet, the issue is similar. The map tiles all load properly, but the default marker icons do not. The app tries to load them from the root path (http://localhost:3000/marker-icon.png) instead of the full remote URL.

Does anybody have any tips for how to work around situations like these when using Propshaft and importmaps? Is there a way to exclude certain online paths or assets declared in the importmap from Propshaft?

theodorton commented 1 month ago

I don't think this is specific to Propshaft. Setting up a minimal application to reproduce the issue I'm seeing the same behaviour with Sprockets as the asset pipeline.

Did you complete the "Setting the Base Path" step of your Shoelace configuration? https://shoelace.style/getting-started/installation#setting-the-base-path

I was able to get rid of the error by adding this to my application template:

<script data-shoelace="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.1/cdn/">
</script>

There's probably a cleaner way to do it, but I'm not very familiar with Shoelace.

If that doesn't help, I'd recommend checking if anyone has raised a similar issue in https://github.com/rails/importmap-rails or report one.

GyozaGuy commented 1 month ago

Thanks! I guess I assumed that section was for a local path based on how the example was written. Works great!

I also did some more digging about the Leaflet issue and found it's a known issue that happens with lots of bundlers. I was able to find a workaround for that too.

Thanks again!