1
1
require "importmap/map"
2
2
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 )
5
5
6
6
module Importmap
7
7
class Engine < ::Rails ::Engine
@@ -14,9 +14,20 @@ class Engine < ::Rails::Engine
14
14
config . autoload_once_paths = %W( #{ root } /app/helpers )
15
15
16
16
initializer "importmap" do |app |
17
- app . importmap = Importmap ::Map . new
17
+ importmap = Importmap ::Map . new
18
+ app . importmaps = {
19
+ "application" => importmap
20
+ }
18
21
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
20
31
end
21
32
22
33
initializer "importmap.reloader" do |app |
@@ -31,12 +42,19 @@ class Engine < ::Rails::Engine
31
42
32
43
initializer "importmap.cache_sweeper" do |app |
33
44
if app . config . importmap . sweep_cache && !app . config . cache_classes
34
- app . config . importmap . cache_sweepers << app . root . join ( "app/javascript" )
35
- app . config . importmap . cache_sweepers << app . root . join ( "vendor/javascript" )
36
- app . importmap . cache_sweeper ( watches : app . config . importmap . cache_sweepers )
45
+ importmap . cache_sweepers << app . root . join ( "app/javascript" )
46
+ importmap . cache_sweepers << app . root . join ( "vendor/javascript" )
47
+
48
+ app . importmaps . values . each do |importmap |
49
+ importmap . cache_sweeper ( watches : app . config . importmap . cache_sweepers )
50
+ end
37
51
38
52
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
40
58
end
41
59
end
42
60
end
0 commit comments