Skip to content

Commit cb618de

Browse files
committed
fixup! Add support to specify the integrity hash of a asset when pinning
1 parent 9c26684 commit cb618de

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

lib/importmap/map.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,7 @@ def preloaded_module_packages(resolver:, entry_point: "application", cache_key:
106106
def to_json(resolver:, cache_key: :json)
107107
cache_as(cache_key) do
108108
packages = expanded_packages_and_directories
109-
map = { "imports" => resolve_asset_paths(packages, resolver: resolver) }
110-
integrity = packages.to_h do |name, mapping|
111-
[resolve_asset_path(mapping.path, resolver: resolver), mapping.integrity]
112-
end.compact
113-
map["integrity"] = integrity unless integrity.empty?
109+
map = build_import_map(packages, resolver: resolver)
114110
JSON.pretty_generate(map)
115111
end
116112
end
@@ -180,6 +176,24 @@ def resolve_asset_path(path, resolver:)
180176
end
181177
end
182178

179+
def build_import_map(packages, resolver:)
180+
map = { "imports" => resolve_asset_paths(packages, resolver: resolver) }
181+
integrity = build_integrity_hash(packages, resolver: resolver)
182+
map["integrity"] = integrity unless integrity.empty?
183+
map
184+
end
185+
186+
def build_integrity_hash(packages, resolver:)
187+
packages.filter_map do |name, mapping|
188+
next unless mapping.integrity
189+
190+
resolved_path = resolve_asset_path(mapping.path, resolver: resolver)
191+
next unless resolved_path
192+
193+
[resolved_path, mapping.integrity]
194+
end.to_h
195+
end
196+
183197
def expanded_preloading_packages_and_directories(entry_point:)
184198
expanded_packages_and_directories.select { |name, mapping| mapping.preload.in?([true, false]) ? mapping.preload : (Array(mapping.preload) & Array(entry_point)).any? }
185199
end

test/importmap_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def setup
66
map.draw do
77
pin "application", preload: false
88
pin "editor", to: "rich_text.js", preload: false, integrity: "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb"
9-
pin "not_there", to: "nowhere.js", preload: false
9+
pin "not_there", to: "nowhere.js", preload: false, integrity: "sha384-somefakehash"
1010
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
1111
pin "leaflet", to: "https://cdn.skypack.dev/leaflet", preload: 'application'
1212
pin "chartkick", to: "https://cdn.skypack.dev/chartkick", preload: ['application', 'alternate']
@@ -34,6 +34,8 @@ def setup
3434
editor_path = generate_importmap_json["imports"]["editor"]
3535
assert_match %r|assets/rich_text-.*\.js|, editor_path
3636
assert_equal "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb", generate_importmap_json["integrity"][editor_path]
37+
assert_nil generate_importmap_json["imports"]["not_there"]
38+
assert_not_includes generate_importmap_json["integrity"].values, "sha384-somefakehash"
3739
end
3840

3941
test "integrity is not present if there is no integrity set in the map" do
@@ -228,7 +230,6 @@ def setup
228230
end
229231

230232
test "preloaded_module_packages includes package integrity when present" do
231-
# Create a new importmap with a preloaded package that has integrity
232233
importmap = Importmap::Map.new.tap do |map|
233234
map.pin "editor", to: "rich_text.js", preload: true, integrity: "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb"
234235
end

test/reloader_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ReloaderTest < ActiveSupport::TestCase
1616
Rails.application.importmap = Importmap::Map.new.draw { pin "md5", to: "https://cdn.skypack.dev/md5" }
1717
assert_not_predicate @reloader, :updated?
1818

19-
assert_changes -> { Rails.application.importmap.packages.keys }, from: %w[ md5 ], to: %w[ md5 not_there rick_text ] do
19+
assert_changes -> { Rails.application.importmap.packages.keys }, from: %w[ md5 ], to: %w[ md5 not_there rich_text ] do
2020
touch_config
2121
assert @reloader.execute_if_updated
2222
end

0 commit comments

Comments
 (0)