Skip to content

Commit 7711d16

Browse files
authored
Merge pull request #3422 from mamhoff/load-importmaps-on-prepare
Wrap importmap drawing in `config.to_prepare` block
2 parents 3a77140 + f4e7697 commit 7711d16

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

lib/alchemy/engine.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Engine < Rails::Engine
4242
end
4343
end
4444

45-
initializer "alchemy.admin_importmap", before: "alchemy.importmap" do
45+
initializer "alchemy.admin_importmap" do
4646
Alchemy.config.admin_importmaps.add(
4747
importmap_path: root.join("config/importmap.rb"),
4848
source_paths: [
@@ -54,21 +54,23 @@ class Engine < Rails::Engine
5454
end
5555

5656
initializer "alchemy.importmap" do |app|
57-
watch_paths = []
58-
59-
Alchemy.config.admin_importmaps.each do |admin_import|
60-
Alchemy.importmap.draw admin_import.importmap_path
61-
watch_paths += admin_import.source_paths.to_a
62-
app.config.assets.paths += admin_import.source_paths.to_a
63-
if admin_import[:name] != "alchemy_admin"
64-
Alchemy.config.admin_js_imports.add(admin_import.name)
57+
app.config.to_prepare do
58+
watch_paths = []
59+
60+
Alchemy.config.admin_importmaps.each do |admin_import|
61+
Alchemy.importmap.draw admin_import.importmap_path
62+
watch_paths += admin_import.source_paths.to_a
63+
app.config.assets.paths += admin_import.source_paths.to_a
64+
if admin_import[:name] != "alchemy_admin"
65+
Alchemy.config.admin_js_imports.add(admin_import.name)
66+
end
6567
end
66-
end
6768

68-
if app.config.importmap.sweep_cache
69-
Alchemy.importmap.cache_sweeper(watches: watch_paths)
70-
ActiveSupport.on_load(:action_controller_base) do
71-
before_action { Alchemy.importmap.cache_sweeper.execute_if_updated }
69+
if app.config.importmap.sweep_cache
70+
Alchemy.importmap.cache_sweeper(watches: watch_paths)
71+
ActiveSupport.on_load(:action_controller_base) do
72+
before_action { Alchemy.importmap.cache_sweeper.execute_if_updated }
73+
end
7274
end
7375
end
7476
end

spec/libraries/alchemy/engine_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
it "adds additional importmap to admin imports" do
5252
initializer = Alchemy::Engine.initializers.find { _1.name == "alchemy.importmap" }
5353
expect(Alchemy.config.admin_js_imports).to receive(:add).with("additional_importmap")
54-
initializer.run(Rails.application)
54+
prepare_blocks = initializer.run(Rails.application)
55+
prepare_blocks.last.call
5556
end
5657
end
5758
end

0 commit comments

Comments
 (0)