Skip to content

Commit 313e2cf

Browse files
committed
Make rescuable asset error classes configurable
Consumers may be using a different asset pipeline than sprockets-rails, which means that when `Importmap::Map#resolve_asset_paths` invokes the application's `asset_path` helper method, it may need to rescue different exceptions than `Sprockets::Rails::Helper::AssetNotFound`. To support that, we've added a `rescuable_asset_errors` property to the importmap configuration options. We add `Sprockets::Rails::Helper::AssetNotFound` to the array if `Sprockets::Rails` is defined, which should allow the gem to work the same as before in applications that use sprockets-rails. But now it'll also be usable in applications that use something other than sprockets-rails.
1 parent 4e7eca7 commit 313e2cf

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/importmap/engine.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Importmap
77
class Engine < ::Rails::Engine
88
config.importmap = ActiveSupport::OrderedOptions.new
99
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
10+
config.importmap.rescuable_asset_errors = []
1011

1112
config.autoload_once_paths = %W( #{root}/app/helpers )
1213

@@ -49,5 +50,11 @@ class Engine < ::Rails::Engine
4950
helper Importmap::ImportmapTagsHelper
5051
end
5152
end
53+
54+
initializer 'importmap.rescuable_asset_errors' do |app|
55+
if defined?(Sprockets::Rails)
56+
app.config.importmap.rescuable_asset_errors << Sprockets::Rails::Helper::AssetNotFound
57+
end
58+
end
5259
end
5360
end

lib/importmap/map.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def clear_cache
8888
end
8989

9090
def rescuable_asset_error?(error)
91-
error.is_a?(Sprockets::Rails::Helper::AssetNotFound)
91+
Rails.application.config.importmap.rescuable_asset_errors.any? { |e| error.is_a?(e) }
9292
end
9393

9494
def resolve_asset_paths(paths, resolver:)

0 commit comments

Comments
 (0)