Skip to content

Prevent asset_path clashing with route definition in a rails app #105

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
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
19 changes: 10 additions & 9 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ def pin_all_from(dir, under: nil, to: nil, preload: false)
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
end

# Returns an array of all the resolved module paths of the pinned packages. The `resolver` must respond to `asset_path`,
# such as `ActionController::Base.helpers` or `ApplicationController.helpers`. You'll want to use the resolver that has
# been configured for the `asset_host` you want these resolved paths to use. In case you need to resolve for different
# asset hosts, you can pass in a custom `cache_key` to vary the cache used by this method for the different cases.
# Returns an array of all the resolved module paths of the pinned packages. The `resolver` must respond to
# `path_to_asset`, such as `ActionController::Base.helpers` or `ApplicationController.helpers`. You'll want to use the
# resolver that has been configured for the `asset_host` you want these resolved paths to use. In case you need to
# resolve for different asset hosts, you can pass in a custom `cache_key` to vary the cache used by this method for
# the different cases.
def preloaded_module_paths(resolver:, cache_key: :preloaded_module_paths)
cache_as(cache_key) do
resolve_asset_paths(expanded_preloading_packages_and_directories, resolver: resolver).values
end
end

# Returns a JSON hash (as a string) of all the resolved module paths of the pinned packages in the import map format.
# The `resolver` must respond to `asset_path`, such as `ActionController::Base.helpers` or `ApplicationController.helpers`.
# You'll want to use the resolver that has been configured for the `asset_host` you want these resolved paths to use.
# In case you need to resolve for different asset hosts, you can pass in a custom `cache_key` to vary the cache used
# by this method for the different cases.
# The `resolver` must respond to `path_to_asset`, such as `ActionController::Base.helpers` or
# `ApplicationController.helpers`. You'll want to use the resolver that has been configured for the `asset_host` you
# want these resolved paths to use. In case you need to resolve for different asset hosts, you can pass in a custom
# `cache_key` to vary the cache used by this method for the different cases.
def to_json(resolver:, cache_key: :json)
cache_as(cache_key) do
JSON.pretty_generate({ "imports" => resolve_asset_paths(expanded_packages_and_directories, resolver: resolver) })
Expand Down Expand Up @@ -113,7 +114,7 @@ def rescuable_asset_error?(error)
def resolve_asset_paths(paths, resolver:)
paths.transform_values do |mapping|
begin
resolver.asset_path(mapping.path)
resolver.path_to_asset(mapping.path)
rescue => e
if rescuable_asset_error?(e)
Rails.logger.warn "Importmap skipped missing path: #{mapping.path}"
Expand Down