Skip to content

Commit

Permalink
implement multiple importmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmeurer committed Jan 28, 2024
1 parent ddf9be4 commit 5b65abb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
14 changes: 11 additions & 3 deletions app/helpers/importmap/importmap_tags_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
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.importmap)
def javascript_importmap_tags(entry_point = "application")
entry_point = entry_point.to_s

importmap_identifier =
entry_point != "application" && Rails.application.importmaps.key?(entry_point) ?
entry_point :
"application"
importmap = Rails.application.importmaps.fetch(entry_point)

safe_join [
javascript_inline_importmap_tag(importmap.to_json(resolver: self)),
javascript_importmap_module_preload_tags(importmap),
Expand All @@ -10,7 +18,7 @@ def javascript_importmap_tags(entry_point = "application", importmap: Rails.appl

# 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_json = Rails.application.importmap.to_json(resolver: self))
def javascript_inline_importmap_tag(importmap_json)
tag.script importmap_json.html_safe,
type: "importmap", "data-turbo-track": "reload", nonce: request&.content_security_policy_nonce
end
Expand All @@ -24,7 +32,7 @@ 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 = Rails.application.importmap)
def javascript_importmap_module_preload_tags(importmap)
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
end

Expand Down
37 changes: 27 additions & 10 deletions lib/importmap/engine.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "importmap/map"

# Use Rails.application.importmap to access the map
Rails::Application.send(:attr_accessor, :importmap)
# Use Rails.application.importmaps to access the maps
Rails::Application.send(:attr_accessor, :importmaps)

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

initializer "importmap" do |app|
app.importmap = Importmap::Map.new
importmap = Importmap::Map.new
app.importmaps = {
"application" => importmap
}
app.config.importmap.paths << app.root.join("config/importmap.rb")
app.config.importmap.paths.each { |path| app.importmap.draw(path) }
app.config.importmap.paths.each { |path| importmap.draw(path) }

Dir[app.root.join("config/importmaps/*.rb")].each do |path|
namespace = File.basename(path).delete_suffix(".rb")
importmap = Importmap::Map.new
app.config.importmap.paths.each { |path| importmap.draw(path) }
importmap.draw(path)
app.importmaps[namespace] = importmap
end
end

initializer "importmap.reloader" do |app|
Expand All @@ -30,13 +41,19 @@ class Engine < ::Rails::Engine
end

initializer "importmap.cache_sweeper" do |app|
if app.config.importmap.sweep_cache && !app.config.cache_classes
app.config.importmap.cache_sweepers << app.root.join("app/javascript")
app.config.importmap.cache_sweepers << app.root.join("vendor/javascript")
app.importmap.cache_sweeper(watches: app.config.importmap.cache_sweepers)
next unless app.config.cache_classes

ActiveSupport.on_load(:action_controller_base) do
before_action { Rails.application.importmap.cache_sweeper.execute_if_updated }
app.importmaps.values.select(&:sweep_cache).each do |importmap|
importmap.cache_sweepers << app.root.join("app/javascript")
importmap.cache_sweepers << app.root.join("vendor/javascript")
app.importmap.cache_sweeper(watches: importmap.cache_sweepers)
end

ActiveSupport.on_load(:action_controller_base) do
before_action do
Rails.application.importmaps.values.select(&:sweep_cache).each do |importmap|
importmap.cache_sweeper.execute_if_updated
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/importmap/reloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Importmap::Reloader
delegate :execute_if_updated, :execute, :updated?, to: :updater

def reload!
import_map_paths.each { |path| Rails.application.importmap.draw(path) }
Rails.application.importmaps.values.each do |importmap|
import_map_paths.each { |path| importmap.draw(path) }
end
end

private
Expand Down

0 comments on commit 5b65abb

Please sign in to comment.