Skip to content

Allow for custom cache keys for Importmap::Map#preloaded_module_paths and Importmap::Map#to_json #88

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 1 commit into from
Dec 29, 2021
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
17 changes: 13 additions & 4 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ def pin_all_from(dir, under: nil, to: nil, preload: false)
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
end

def preloaded_module_paths(resolver:)
cache_as(:preloaded_module_paths) do
# 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.
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

def to_json(resolver:)
cache_as(:json) do
# 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.
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) })
end
end
Expand Down