Skip to content

Commit ef4df9c

Browse files
committed
Multiple Importmaps
If needed multiple importmaps can be defined in the `config/importmaps` directory. `bin/importmap` have a new option to work with named importmaps `bin/importmap -i admin pin "some-dependency"` The default is still `config/importmap.rb` so no change for exisiting users that does not need multiple importmaps or complexity.
1 parent f588506 commit ef4df9c

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

lib/importmap/commands.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def self.exit_on_failure?
99
false
1010
end
1111

12+
class_option :importmap, type: :string, aliases: :i, default: ""
13+
1214
desc "pin [*PACKAGES]", "Pin new packages"
1315
option :env, type: :string, aliases: :e, default: "production"
1416
option :from, type: :string, aliases: :f, default: "jspm"
@@ -20,9 +22,9 @@ def pin(*packages)
2022
pin = packager.vendored_pin_for(package, url)
2123

2224
if packager.packaged?(package)
23-
gsub_file("config/importmap.rb", /^pin "#{package}".*$/, pin, verbose: false)
25+
gsub_file(importmap_path, /^pin "#{package}".*$/, pin, verbose: false)
2426
else
25-
append_to_file("config/importmap.rb", "#{pin}\n", verbose: false)
27+
append_to_file(importmap_path, "#{pin}\n", verbose: false)
2628
end
2729
end
2830
else
@@ -67,7 +69,12 @@ def pristine
6769
desc "json", "Show the full importmap in json"
6870
def json
6971
require Rails.root.join("config/environment")
70-
puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
72+
73+
if options[:importmap].blank?
74+
puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers)
75+
else
76+
puts Rails.application.importmaps[options[:importmap]].to_json(resolver: ActionController::Base.helpers)
77+
end
7178
end
7279

7380
desc "audit", "Run a security audit"
@@ -123,11 +130,11 @@ def packages
123130

124131
private
125132
def packager
126-
@packager ||= Importmap::Packager.new
133+
@packager ||= Importmap::Packager.new(importmap_path)
127134
end
128135

129136
def npm
130-
@npm ||= Importmap::Npm.new
137+
@npm ||= Importmap::Npm.new(importmap_path)
131138
end
132139

133140
def remove_line_from_file(path, pattern)
@@ -154,6 +161,14 @@ def puts_table(array)
154161
puts divider if row_number == 0
155162
end
156163
end
164+
165+
def importmap_path
166+
if options[:importmap].blank?
167+
"config/importmap.rb"
168+
else
169+
"config/importmaps/#{options[:importmap]}.rb"
170+
end
171+
end
157172
end
158173

159174
Importmap::Commands.start(ARGV)

lib/importmap/engine.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Use Rails.application.importmap to access the map
44
Rails::Application.send(:attr_accessor, :importmap)
5+
Rails::Application.send(:attr_accessor, :importmaps)
56

67
module Importmap
78
class Engine < ::Rails::Engine
@@ -15,8 +16,14 @@ class Engine < ::Rails::Engine
1516

1617
initializer "importmap" do |app|
1718
app.importmap = Importmap::Map.new
19+
app.importmaps = ActiveSupport::OrderedOptions.new
20+
1821
app.config.importmap.paths << app.root.join("config/importmap.rb")
1922
app.config.importmap.paths.each { |path| app.importmap.draw(path) }
23+
24+
Dir.glob(app.root.join("config", "importmaps", "*.rb")).each do |path|
25+
app.importmaps[File.basename(path, ".rb")] = Importmap::Map.new.draw(path)
26+
end
2027
end
2128

2229
initializer "importmap.reloader" do |app|
@@ -33,10 +40,15 @@ class Engine < ::Rails::Engine
3340
if app.config.importmap.sweep_cache && !app.config.cache_classes
3441
app.config.importmap.cache_sweepers << app.root.join("app/javascript")
3542
app.config.importmap.cache_sweepers << app.root.join("vendor/javascript")
43+
3644
app.importmap.cache_sweeper(watches: app.config.importmap.cache_sweepers)
45+
app.importmaps.each_value { |map| map.cache_sweeper(watches: app.config.importmap.cache_sweepers) }
3746

3847
ActiveSupport.on_load(:action_controller_base) do
39-
before_action { Rails.application.importmap.cache_sweeper.execute_if_updated }
48+
before_action do
49+
Rails.application.importmap.cache_sweeper.execute_if_updated
50+
Rails.application.importmaps.each_value { |map| map.cache_sweeper.execute_if_updated }
51+
end
4052
end
4153
end
4254
end

test/dummy/config/importmaps/admin.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)