Skip to content

Instantly share code, notes, and snippets.

@varyform
Last active April 3, 2024 21:58
Show Gist options
  • Save varyform/92be68c3ea2d94d31de3c3359284c0e5 to your computer and use it in GitHub Desktop.
Save varyform/92be68c3ea2d94d31de3c3359284c0e5 to your computer and use it in GitHub Desktop.
Multiple importmaps
# config/initializers/public_importmap.rb
Rails.application.config.public_importmap = Importmap::Map.new
Rails.application.config.public_importmap.draw(Rails.root.join("config/public_importmap.rb"))
# app/helpers/application_helper.rb
# copied from importmaps replacing javascript_inline_importmap_tag
#
# !!! No longer needed after this change https://github.com/rails/importmap-rails/pull/187
#
# def javascript_public_importmap_tags(entry_point = "application", shim: true)
# safe_join [
# javascript_inline_importmap_tag(Rails.application.config.public_importmap.to_json(resolver: self)),
# javascript_importmap_module_preload_tags(Rails.application.config.public_importmap)
# (javascript_importmap_shim_nonce_configuration_tag if shim),
# (javascript_importmap_shim_tag if shim),
# javascript_import_module_tag(entry_point)
# ].compact, "\n"
# end
# config/public_importmap.rb
# whatever you need
pin "@hotwired/turbo-rails", to: "turbo.js"
pin "public"
@danieldraper
Copy link

Line 10: javascript_importmap_module_preload_tags

Unless you want to prefetch everything including those in the default importmap you probably want to use

javascript_importmap_module_preload_tags(Rails.application.config.public_importmap)

@varyform
Copy link
Author

Line 10: javascript_importmap_module_preload_tags

Unless you want to prefetch everything including those in the default importmap you probably want to use

javascript_importmap_module_preload_tags(Rails.application.config.public_importmap)

Fixed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment