From bdc9d56cb13c80672eccad8778444af65a29c6bb Mon Sep 17 00:00:00 2001 From: Nipun Paradkar Date: Fri, 19 Nov 2021 23:44:11 +0530 Subject: [PATCH 1/2] Replace `pin_all_from` with `pin_under` --- lib/importmap/map.rb | 21 ++++++++++++--------- test/dummy/config/importmap.rb | 2 -- test/importmap_test.rb | 23 +++++++++++------------ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/importmap/map.rb b/lib/importmap/map.rb index b90e080..20fd4bb 100644 --- a/lib/importmap/map.rb +++ b/lib/importmap/map.rb @@ -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:) @@ -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) @@ -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) @@ -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 diff --git a/test/dummy/config/importmap.rb b/test/dummy/config/importmap.rb index 481d62b..c3f66f6 100644 --- a/test/dummy/config/importmap.rb +++ b/test/dummy/config/importmap.rb @@ -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" diff --git a/test/importmap_test.rb b/test/importmap_test.rb index b4a9253..d14243e 100644 --- a/test/importmap_test.rb +++ b/test/importmap_test.rb @@ -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 @@ -30,16 +29,20 @@ 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 @@ -47,15 +50,11 @@ def setup 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 From 5b0d00b0300b6969a14b9157ba4e2a5fd56e3827 Mon Sep 17 00:00:00 2001 From: Nipun Paradkar Date: Sun, 21 Nov 2021 15:36:11 +0530 Subject: [PATCH 2/2] Update README doc with `pin_under` instead of `pin_all_from` --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 41d558b..1dd73b4 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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" ```