Skip to content

Commit 2ee1e26

Browse files
authored
Allow for custom cache keys (#88)
1 parent a3a5d46 commit 2ee1e26

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/importmap/map.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,23 @@ def pin_all_from(dir, under: nil, to: nil, preload: false)
3232
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
3333
end
3434

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

41-
def to_json(resolver:)
42-
cache_as(:json) do
45+
# Returns a JSON hash (as a string) of all the resolved module paths of the pinned packages in the import map format.
46+
# The `resolver` must respond to `asset_path`, such as `ActionController::Base.helpers` or `ApplicationController.helpers`.
47+
# You'll want to use the resolver that has been configured for the `asset_host` you want these resolved paths to use.
48+
# In case you need to resolve for different asset hosts, you can pass in a custom `cache_key` to vary the cache used
49+
# by this method for the different cases.
50+
def to_json(resolver:, cache_key: :json)
51+
cache_as(cache_key) do
4352
JSON.pretty_generate({ "imports" => resolve_asset_paths(expanded_packages_and_directories, resolver: resolver) })
4453
end
4554
end

0 commit comments

Comments
 (0)