Skip to content

Decouple from sprockets-rails #60

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

Merged
merged 2 commits into from
Nov 3, 2021
Merged
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
7 changes: 7 additions & 0 deletions lib/importmap/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Importmap
class Engine < ::Rails::Engine
config.importmap = ActiveSupport::OrderedOptions.new
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
config.importmap.rescuable_asset_errors = []

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

Expand Down Expand Up @@ -49,5 +50,11 @@ class Engine < ::Rails::Engine
helper Importmap::ImportmapTagsHelper
end
end

initializer 'importmap.rescuable_asset_errors' do |app|
if defined?(Sprockets::Rails)
app.config.importmap.rescuable_asset_errors << Sprockets::Rails::Helper::AssetNotFound
end
end
end
end
14 changes: 11 additions & 3 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ def clear_cache
@cached_preloaded_module_paths = nil
end

def rescuable_asset_error?(error)
Rails.application.config.importmap.rescuable_asset_errors.any? { |e| error.is_a?(e) }
end

def resolve_asset_paths(paths, resolver:)
paths.transform_values do |mapping|
begin
resolver.asset_path(mapping.path)
rescue Sprockets::Rails::Helper::AssetNotFound
Rails.logger.warn "Importmap skipped missing path: #{mapping.path}"
nil
rescue => e
if rescuable_asset_error?(e)
Rails.logger.warn "Importmap skipped missing path: #{mapping.path}"
nil
else
raise e
end
end
end.compact
end
Expand Down