Skip to content

Commit 7ffedcb

Browse files
committed
Fix pin_all_from incorrectly removing "js" substring from filenames
When using pin_all_from, filenames containing "js" as a substring (like "foo.jszip.js" or "bar.jsmin.js") were having the substring incorrectly removed, resulting in malformed module names like "foozip" and "barmin" instead of "foo.jszip" and "bar.jsmin". Fixed the logic to only remove the file extension from the end of the filename, preserving any "js" substrings that appear elsewhere in the name. Fixes #282
1 parent dcdb5fe commit 7ffedcb

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

lib/importmap/map.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def module_name_from(filename, mapping)
302302
# folder/index
303303
index_regex = /(?:\/|^)index$/
304304

305-
[ mapping.under, filename.to_s.remove(filename.extname).remove(index_regex).presence ].compact.join("/")
305+
[ mapping.under, filename.to_s.chomp(filename.extname).remove(index_regex).presence ].compact.join("/")
306306
end
307307

308308
def module_path_from(filename, mapping)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function testJsmin() { return 'jsmin test'; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function testJszip() { return 'jszip test'; }

test/importmap_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ def setup
134134
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
135135
end
136136

137+
test "pin_all_from handles filenames with js substring correctly" do
138+
assert_includes generate_importmap_json["imports"].keys, "controllers/foo.jszip"
139+
assert_includes generate_importmap_json["imports"].keys, "controllers/bar.jsmin"
140+
141+
refute_includes generate_importmap_json["imports"].keys, "controllers/foozip"
142+
refute_includes generate_importmap_json["imports"].keys, "controllers/barmin"
143+
144+
assert_match %r|assets/controllers/foo\.jszip-.*\.js|, generate_importmap_json["imports"]["controllers/foo.jszip"]
145+
assert_match %r|assets/controllers/bar\.jsmin-.*\.js|, generate_importmap_json["imports"]["controllers/bar.jsmin"]
146+
end
147+
137148
test "importmap json includes integrity hashes from integrity: true" do
138149
importmap = Importmap::Map.new.tap do |map|
139150
map.enable_integrity!

0 commit comments

Comments
 (0)