From ef4df9c365097dfbdb15189d17feea847d2484ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Thu, 7 Nov 2024 10:29:27 +0100 Subject: [PATCH] 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. --- lib/importmap/commands.rb | 25 ++++++++++++++++++++----- lib/importmap/engine.rb | 14 +++++++++++++- test/dummy/config/importmaps/admin.rb | 0 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 test/dummy/config/importmaps/admin.rb diff --git a/lib/importmap/commands.rb b/lib/importmap/commands.rb index 0a6fbec..7beb9dd 100644 --- a/lib/importmap/commands.rb +++ b/lib/importmap/commands.rb @@ -9,6 +9,8 @@ def self.exit_on_failure? false end + class_option :importmap, type: :string, aliases: :i, default: "" + desc "pin [*PACKAGES]", "Pin new packages" option :env, type: :string, aliases: :e, default: "production" option :from, type: :string, aliases: :f, default: "jspm" @@ -20,9 +22,9 @@ def pin(*packages) pin = packager.vendored_pin_for(package, url) if packager.packaged?(package) - gsub_file("config/importmap.rb", /^pin "#{package}".*$/, pin, verbose: false) + gsub_file(importmap_path, /^pin "#{package}".*$/, pin, verbose: false) else - append_to_file("config/importmap.rb", "#{pin}\n", verbose: false) + append_to_file(importmap_path, "#{pin}\n", verbose: false) end end else @@ -67,7 +69,12 @@ def pristine desc "json", "Show the full importmap in json" def json require Rails.root.join("config/environment") - puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers) + + if options[:importmap].blank? + puts Rails.application.importmap.to_json(resolver: ActionController::Base.helpers) + else + puts Rails.application.importmaps[options[:importmap]].to_json(resolver: ActionController::Base.helpers) + end end desc "audit", "Run a security audit" @@ -123,11 +130,11 @@ def packages private def packager - @packager ||= Importmap::Packager.new + @packager ||= Importmap::Packager.new(importmap_path) end def npm - @npm ||= Importmap::Npm.new + @npm ||= Importmap::Npm.new(importmap_path) end def remove_line_from_file(path, pattern) @@ -154,6 +161,14 @@ def puts_table(array) puts divider if row_number == 0 end end + + def importmap_path + if options[:importmap].blank? + "config/importmap.rb" + else + "config/importmaps/#{options[:importmap]}.rb" + end + end end Importmap::Commands.start(ARGV) diff --git a/lib/importmap/engine.rb b/lib/importmap/engine.rb index 1c823f4..ef36e32 100755 --- a/lib/importmap/engine.rb +++ b/lib/importmap/engine.rb @@ -2,6 +2,7 @@ # Use Rails.application.importmap to access the map Rails::Application.send(:attr_accessor, :importmap) +Rails::Application.send(:attr_accessor, :importmaps) module Importmap class Engine < ::Rails::Engine @@ -15,8 +16,14 @@ class Engine < ::Rails::Engine initializer "importmap" do |app| app.importmap = Importmap::Map.new + app.importmaps = ActiveSupport::OrderedOptions.new + app.config.importmap.paths << app.root.join("config/importmap.rb") app.config.importmap.paths.each { |path| app.importmap.draw(path) } + + Dir.glob(app.root.join("config", "importmaps", "*.rb")).each do |path| + app.importmaps[File.basename(path, ".rb")] = Importmap::Map.new.draw(path) + end end initializer "importmap.reloader" do |app| @@ -33,10 +40,15 @@ class Engine < ::Rails::Engine 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) + app.importmaps.each_value { |map| map.cache_sweeper(watches: app.config.importmap.cache_sweepers) } ActiveSupport.on_load(:action_controller_base) do - before_action { Rails.application.importmap.cache_sweeper.execute_if_updated } + before_action do + Rails.application.importmap.cache_sweeper.execute_if_updated + Rails.application.importmaps.each_value { |map| map.cache_sweeper.execute_if_updated } + end end end end diff --git a/test/dummy/config/importmaps/admin.rb b/test/dummy/config/importmaps/admin.rb new file mode 100644 index 0000000..e69de29