Skip to content

Commit

Permalink
Multiple Importmaps
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
henrikbjorn committed Nov 7, 2024
1 parent f588506 commit ef4df9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
25 changes: 20 additions & 5 deletions lib/importmap/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
14 changes: 13 additions & 1 deletion lib/importmap/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand All @@ -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
Expand Down
Empty file.

0 comments on commit ef4df9c

Please sign in to comment.