lib/importmap/map.rb in importmap-rails-2.0.1 vs lib/importmap/map.rb in importmap-rails-2.0.2
- old
+ new
@@ -38,13 +38,13 @@
# 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)
+ def preloaded_module_paths(resolver:, entry_point: "application", 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
# 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 `path_to_asset`, such as `ActionController::Base.helpers` or
@@ -116,12 +116,12 @@
end
end
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.in?([true, false]) ? mapping.preload : (Array(mapping.preload) & Array(entry_point)).any? }
end
def expanded_packages_and_directories
@packages.dup.tap { |expanded| expand_directories_into expanded }
end
@@ -139,10 +139,20 @@
end
end
end
def module_name_from(filename, mapping)
- [ mapping.under, filename.to_s.remove(filename.extname).remove(/\/?index$/).presence ].compact.join("/")
+ # Regex explanation:
+ # (?:\/|^) # Matches either / OR the start of the string
+ # index # Matches the word index
+ # $ # Matches the end of the string
+ #
+ # Sample matches
+ # index
+ # folder/index
+ index_regex = /(?:\/|^)index$/
+
+ [ mapping.under, filename.to_s.remove(filename.extname).remove(index_regex).presence ].compact.join("/")
end
def module_path_from(filename, mapping)
[ mapping.path || mapping.under, filename.to_s ].compact.reject(&:empty?).join("/")
end