Skip to content
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

Allows preload to be determined by provided entry_point #253

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Changes from 1 commit
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
Next Next commit
Allows preload to be determined by provided entry_point
aseroff committed May 28, 2024
commit 3e67ed880372afa0811b6057b354612603fc4e93
6 changes: 3 additions & 3 deletions app/helpers/importmap/importmap_tags_helper.rb
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ module Importmap::ImportmapTagsHelper
def javascript_importmap_tags(entry_point = "application", importmap: Rails.application.importmap)
safe_join [
javascript_inline_importmap_tag(importmap.to_json(resolver: self)),
javascript_importmap_module_preload_tags(importmap),
javascript_importmap_module_preload_tags(importmap, entry_point:),
javascript_import_module_tag(entry_point)
], "\n"
end
@@ -24,8 +24,8 @@ def javascript_import_module_tag(*module_names)
# Link tags for preloading all modules marked as preload: true in the `importmap`
# (defaults to Rails.application.importmap), such that they'll be fetched
# in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap)
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap, entry_point:)
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self, entry_point:))
end

# Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.
8 changes: 4 additions & 4 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
@@ -40,9 +40,9 @@ def pin_all_from(dir, under: nil, to: nil, preload: true)
# 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)
def preloaded_module_paths(resolver:, entry_point:, cache_key: :preloaded_module_paths)
cache_as(cache_key) do
resolve_asset_paths(expanded_preloading_packages_and_directories, resolver: resolver).values
resolve_asset_paths(expanded_preloading_packages_and_directories(entry_point:), resolver:).values
end
end

@@ -118,8 +118,8 @@ def resolve_asset_paths(paths, resolver:)
end.compact
end

def expanded_preloading_packages_and_directories
expanded_packages_and_directories.select { |name, mapping| mapping.preload }
def expanded_preloading_packages_and_directories(entry_point:)
expanded_packages_and_directories.select { |name, mapping| mapping.preload == true || (mapping.preload & Array(entry_point)).any? }
end

def expanded_packages_and_directories