Skip to content

Commit bc9792d

Browse files
committed
implement multiple importmaps
1 parent ddf9be4 commit bc9792d

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

app/helpers/importmap/importmap_tags_helper.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
module Importmap::ImportmapTagsHelper
22
# Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
3-
def javascript_importmap_tags(entry_point = "application", importmap: Rails.application.importmap)
3+
def javascript_importmap_tags(entry_point = "application")
4+
entry_point = entry_point.to_s
5+
6+
importmap_identifier =
7+
entry_point != "application" && Rails.application.importmaps.key?(entry_point) ?
8+
entry_point :
9+
"application"
10+
importmap = Rails.application.importmaps.fetch(entry_point)
11+
412
safe_join [
513
javascript_inline_importmap_tag(importmap.to_json(resolver: self)),
614
javascript_importmap_module_preload_tags(importmap),
@@ -10,7 +18,7 @@ def javascript_importmap_tags(entry_point = "application", importmap: Rails.appl
1018

1119
# Generate an inline importmap tag using the passed `importmap_json` JSON string.
1220
# By default, `Rails.application.importmap.to_json(resolver: self)` is used.
13-
def javascript_inline_importmap_tag(importmap_json = Rails.application.importmap.to_json(resolver: self))
21+
def javascript_inline_importmap_tag(importmap_json)
1422
tag.script importmap_json.html_safe,
1523
type: "importmap", "data-turbo-track": "reload", nonce: request&.content_security_policy_nonce
1624
end
@@ -24,7 +32,7 @@ def javascript_import_module_tag(*module_names)
2432
# Link tags for preloading all modules marked as preload: true in the `importmap`
2533
# (defaults to Rails.application.importmap), such that they'll be fetched
2634
# in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
27-
def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap)
35+
def javascript_importmap_module_preload_tags(importmap)
2836
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
2937
end
3038

lib/importmap/engine.rb

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "importmap/map"
22

3-
# Use Rails.application.importmap to access the map
4-
Rails::Application.send(:attr_accessor, :importmap)
3+
# Use Rails.application.importmaps to access the maps
4+
Rails::Application.send(:attr_accessor, :importmaps)
55

66
module Importmap
77
class Engine < ::Rails::Engine
@@ -14,9 +14,20 @@ class Engine < ::Rails::Engine
1414
config.autoload_once_paths = %W( #{root}/app/helpers )
1515

1616
initializer "importmap" do |app|
17-
app.importmap = Importmap::Map.new
17+
importmap = Importmap::Map.new
18+
app.importmaps = {
19+
"application" => importmap
20+
}
1821
app.config.importmap.paths << app.root.join("config/importmap.rb")
19-
app.config.importmap.paths.each { |path| app.importmap.draw(path) }
22+
app.config.importmap.paths.each { |path| importmap.draw(path) }
23+
24+
Dir[app.root.join("config/importmaps/*.rb")].each do |path|
25+
namespace = File.basename(path).delete_suffix(".rb")
26+
importmap = Importmap::Map.new
27+
app.config.importmap.paths.each { |path| importmap.draw(path) }
28+
importmap.draw(path)
29+
app.importmaps[namespace] = importmap
30+
end
2031
end
2132

2233
initializer "importmap.reloader" do |app|
@@ -33,10 +44,17 @@ class Engine < ::Rails::Engine
3344
if app.config.importmap.sweep_cache && !app.config.cache_classes
3445
app.config.importmap.cache_sweepers << app.root.join("app/javascript")
3546
app.config.importmap.cache_sweepers << app.root.join("vendor/javascript")
36-
app.importmap.cache_sweeper(watches: app.config.importmap.cache_sweepers)
47+
48+
app.importmaps.values.each do |importmap|
49+
importmap.cache_sweeper(watches: app.config.importmap.cache_sweepers)
50+
end
3751

3852
ActiveSupport.on_load(:action_controller_base) do
39-
before_action { Rails.application.importmap.cache_sweeper.execute_if_updated }
53+
before_action do
54+
Rails.application.importmaps.values.each do |importmap|
55+
importmap.cache_sweeper.execute_if_updated
56+
end
57+
end
4058
end
4159
end
4260
end

lib/importmap/reloader.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ class Importmap::Reloader
55
delegate :execute_if_updated, :execute, :updated?, to: :updater
66

77
def reload!
8-
import_map_paths.each { |path| Rails.application.importmap.draw(path) }
8+
Rails.application.importmaps.values.each do |importmap|
9+
import_map_paths.each { |path| importmap.draw(path) }
10+
end
911
end
1012

1113
private

0 commit comments

Comments
 (0)