importmap-rails icon indicating copy to clipboard operation
importmap-rails copied to clipboard

Multiple importmaps

Open manuelmeurer opened this issue 1 year ago • 6 comments

Maybe I'm missing something, but shouldn't it be possible to have multiple separate importmaps? :)

My app has three distinct parts/namespaces ("web" for the public marketing page, "admin" for the admin area, and "clients" for the login area for clients) and for each I'd like to have different pinned (and preloaded) modules.

Right now I'm solving it by keeping three different Importmap::Maps around and delegating Rails.application.importmap to Current.importmap, which loads the correct importmap for the current namespace, but that seems to be much more complicated than it should be.

I'm sure I'm not the only one that has multiple distinct namespaces in their app? :) Is there another way to solve this?

Happy to have a stab at implementing this if it's a welcomed feature.

manuelmeurer avatar Jan 28 '24 10:01 manuelmeurer

I'd also like to see this. Would be very useful for splitting up public/admin JS

deanpcmad avatar Jan 31 '24 15:01 deanpcmad

@deanpcmad Feel free to use my fork/branch as described in the PR for now: https://github.com/rails/importmap-rails/pull/241 I use it in production and it works great!

manuelmeurer avatar Jan 31 '24 15:01 manuelmeurer

@manuelmeurer that's great. Let me know if you need any help in completing the PR. I would be happy to help.

sarmad90 avatar Feb 01 '24 08:02 sarmad90

@manuelmeurer I have added some comments on the PR.

sarmad90 avatar Feb 01 '24 08:02 sarmad90

I +1 the need for this. An approach I was considering taking is keeping a single importmap (since having unnecessary pins doesn't make a significant difference), but changing the pin's preload based on the provided import module tag.

To illustrate: javascript_importmap_tags expands to the importmap, the preloads, and the initial module import defined by the provided entry_point parameter. If we passed the entry_point parameter to javascript_importmap_module_preload_tags, we could enable the preloading to be defined by something like pin 'admin', preload: [:admin] in config/importmap.rb. The result would accomplish the desired result without having to maintain multiple importmaps.

If this seems agreeable I could throw a PR together.

aseroff avatar Mar 14 '24 20:03 aseroff

Definitely need this. any progress?

jimhj avatar May 14 '24 12:05 jimhj