Skip to content

Commit 34defe5

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 34defe5

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

lib/importmap/engine.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ 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 = [
11+
defined?(Sprockets::Rails) && Sprockets::Rails::Helper::AssetNotFound,
12+
].compact
1013

1114
config.autoload_once_paths = %W( #{root}/app/helpers )
1215

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)