Skip to content

Allow multiple importmaps in config/importmaps/ #241

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

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
allow specifying importmap name in helpers
  • Loading branch information
manuelmeurer committed Sep 9, 2024
commit 91c7feb8b79fe3dd5dc9ee3947c0b18cdf798ddd
18 changes: 7 additions & 11 deletions app/helpers/importmap/importmap_tags_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
module Importmap::ImportmapTagsHelper
# Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
def javascript_importmap_tags(entry_point = "application")
importmap = Rails.application.importmaps.fetch(entry_point.to_s)

unless importmap
raise "No importmap found for entry point '#{entry_point}'."
end

def javascript_importmap_tags(entry_point = "application", importmap_name = "application")
safe_join [
javascript_inline_importmap_tag(importmap),
javascript_importmap_module_preload_tags(importmap),
javascript_inline_importmap_tag(importmap_name),
javascript_importmap_module_preload_tags(importmap_name),
javascript_import_module_tag(entry_point)
], "\n"
end

# Generate an inline importmap tag using the passed `importmap_json` JSON string.
# By default, `Rails.application.importmap.to_json(resolver: self)` is used.
def javascript_inline_importmap_tag(importmap)
def javascript_inline_importmap_tag(importmap_name = "application")
importmap = Rails.application.importmaps.fetch(importmap_name.to_s)
importmap_json = importmap.to_json(resolver: self)
tag.script importmap_json.html_safe,
type: "importmap", "data-turbo-track": "reload", nonce: request&.content_security_policy_nonce
Expand All @@ -31,7 +26,8 @@ def javascript_import_module_tag(*module_names)
# Link tags for preloading all modules marked as preload: true in the `importmap`
# (defaults to Rails.application.importmap), such that they'll be fetched
# in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
def javascript_importmap_module_preload_tags(importmap)
def javascript_importmap_module_preload_tags(importmap_name = "application")
importmap = Rails.application.importmaps.fetch(importmap_name.to_s)
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
end

Expand Down