Skip to content

Make all pin* methods behave conventionally the same #72

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ module MyEngine
app.config.importmap.paths << Engine.root.join("config/importmap.rb")
# ...
end

initializer "my-engine.javascript-assets" do |app|
app.config.assets.paths << File.expand_path("../../app/javascript")
# ...
end
end
end
```
Expand All @@ -220,7 +225,7 @@ And pinning JavaScript modules from the engine:
```ruby
# my_engine/config/importmap.rb

pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
pin_under "blorgh/application"
```


Expand Down
21 changes: 12 additions & 9 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def pin(name, to: nil, preload: false)
@packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
end

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

def preloaded_module_paths(resolver:)
Expand Down Expand Up @@ -71,7 +71,7 @@ def cache_sweeper(watches: nil)
end

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

def cache_as(name)
Expand Down Expand Up @@ -116,9 +116,10 @@ def expanded_packages_and_directories

def expand_directories_into(paths)
@directories.values.each do |mapping|
if (absolute_path = absolute_root_of(mapping.dir)).exist?
find_javascript_files_in_tree(absolute_path).each do |filename|
module_filename = filename.relative_path_from(absolute_path)
unless (asset_path = asset_path_containing(mapping.path)).nil?
dir_path = asset_path.join(mapping.path)
find_javascript_files_in_tree(dir_path).each do |filename|
module_filename = filename.relative_path_from(dir_path)
module_name = module_name_from(module_filename, mapping)
module_path = module_path_from(module_filename, mapping)

Expand All @@ -133,14 +134,16 @@ def module_name_from(filename, mapping)
end

def module_path_from(filename, mapping)
[ mapping.path || mapping.under, filename.to_s ].compact.join("/")
[ mapping.path, filename.to_s ].compact.join("/")
end

def find_javascript_files_in_tree(path)
Dir[path.join("**/*.js{,m}")].collect { |file| Pathname.new(file) }.select(&:file?)
end

def absolute_root_of(path)
(pathname = Pathname.new(path)).absolute? ? pathname : Rails.root.join(path)
def asset_path_containing(path)
Rails.application.config.assets.paths.find do |asset_path|
Pathname.new(asset_path).join(path).directory?
end
end
end
2 changes: 0 additions & 2 deletions test/dummy/config/importmap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pin_all_from "app/assets/javascripts"

pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
pin "not_there", to: "nowhere.js"
23 changes: 11 additions & 12 deletions test/importmap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ def setup
pin "editor", to: "rich_text.js"
pin "not_there", to: "nowhere.js"
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
pin "my_lib", preload: true

pin_all_from "app/javascript/controllers", under: "controllers", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers", preload: true
pin_all_from "app/javascript/helpers", under: "helpers", preload: true
pin_all_from "lib/assets/javascripts", preload: true
pin_under "controllers", preload: true
pin_under "controllers/spina", inside: "spina/controllers"
pin_under "helpers", inside: "helpers"
end
end
end
Expand All @@ -30,32 +29,32 @@ def setup
assert_nil generate_importmap_json["imports"]["not_there"]
end

test "local pin for other folder" do
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
end

test "remote pin is not digest stamped" do
assert_equal "https://cdn.skypack.dev/md5", generate_importmap_json["imports"]["md5"]
end

test "directory pin mounted under matching subdir maps all files" do
test "directory pin with inferred inside" do
assert_match %r|assets/controllers/goodbye_controller-.*\.js|, generate_importmap_json["imports"]["controllers/goodbye_controller"]
assert_match %r|assets/controllers/utilities/md5_controller-.*\.js|, generate_importmap_json["imports"]["controllers/utilities/md5_controller"]
end

test "directory pin mounted under matching subdir maps index as root" do
test "directory pin maps index as root" do
assert_match %r|assets/controllers/index.*\.js|, generate_importmap_json["imports"]["controllers"]
end

test "directory pin mounted under matching subdir maps index as root at second depth" do
assert_match %r|assets/helpers/requests/index.*\.js|, generate_importmap_json["imports"]["helpers/requests"]
end

test "directory pin under custom asset path" do
test "directory pin with explicit inside" do
assert_match %r|assets/spina/controllers/another_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/another_controller"]
assert_match %r|assets/spina/controllers/deeper/again_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/deeper/again_controller"]
end

test "directory pin without path or under" do
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
end

test "preloaded modules are included in preload tags" do
preloading_module_paths = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers).to_s
assert_match /md5/, preloading_module_paths
Expand Down