Skip to content

Commit 5f544b5

Browse files
ollie-nyeboardfish
andcommitted
Prevent asset_path clashing with route definition in a rails app
Co-authored-by: Simon Fish <si@mon.fish>
1 parent ef8dc82 commit 5f544b5

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/importmap/map.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,22 @@ def pin_all_from(dir, under: nil, to: nil, preload: false)
3434
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
3535
end
3636

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

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

0 commit comments

Comments
 (0)