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

Introduce preserve_extname option for directory pin #261

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def pin(name, to: nil, preload: true)
@packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
end

def pin_all_from(dir, under: nil, to: nil, preload: true)
def pin_all_from(dir, under: nil, to: nil, preserve_extname: false, preload: true)
clear_cache
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preserve_extname: preserve_extname, preload: preload)
end

# Returns an array of all the resolved module paths of the pinned packages. The `resolver` must respond to
Expand Down Expand Up @@ -84,7 +84,7 @@ def cache_sweeper(watches: nil)
end

private
MappedDir = Struct.new(:dir, :path, :under, :preload, keyword_init: true)
MappedDir = Struct.new(:dir, :path, :under, :preserve_extname, :preload, keyword_init: true)
MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)

def cache_as(name)
Expand Down Expand Up @@ -151,7 +151,9 @@ def module_name_from(filename, mapping)
# folder/index
index_regex = /(?:\/|^)index$/

[ mapping.under, filename.to_s.remove(filename.extname).remove(index_regex).presence ].compact.join("/")
[ mapping.under, filename.to_s.remove(filename.extname).remove(index_regex).presence ].compact.join("/").tap do |module_name|
module_name << filename.extname if mapping.preserve_extname
end
end

def module_path_from(filename, mapping)
Expand Down
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions test/importmap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def setup
pin_all_from "app/javascript/helpers", under: "helpers", preload: true
pin_all_from "lib/assets/javascripts", preload: true
pin_all_from "app/components", under: "controllers", to: "", preload: true
pin_all_from "vendor/javascript/shoelace", under: "shoelace", preserve_extname: true, preload: true
end
end
end
Expand Down Expand Up @@ -70,6 +71,14 @@ def setup
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
end

test "directory pin respects preserve_extname option" do
assert_nil generate_importmap_json["imports"]["shoelace/shoelace-autoloader"]
assert_match %r|shoelace/shoelace-autoloader-.*\.js|, generate_importmap_json["imports"]["shoelace/shoelace-autoloader.js"]
assert_match %r|shoelace/chunks/chunk.2L6GHXIJ-.*\.js|, generate_importmap_json["imports"]["shoelace/chunks/chunk.2L6GHXIJ.js"]
assert_match %r|shoelace/components/alert/alert-.*\.js|, generate_importmap_json["imports"]["shoelace/components/alert/alert.js"]
assert_match %r|shoelace/components/alert/alert.component-.*\.js|, generate_importmap_json["imports"]["shoelace/components/alert/alert.component.js"]
end

test 'invalid importmap file results in error' do
file = file_fixture('invalid_import_map.rb')
importmap = Importmap::Map.new
Expand Down
Loading