Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Importmaps #279

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading