From 557766d2a4737b144860e292900c8088b5394749 Mon Sep 17 00:00:00 2001 From: Jason Taylor Date: Wed, 19 Jul 2023 10:07:25 +1000 Subject: [PATCH 1/3] Add failing test to cover empty to value --- test/dummy/app/assets/config/manifest.js | 1 + test/dummy/app/components/another_component_controller.js | 0 test/dummy/app/components/spina/component_controller.js | 0 test/dummy/config/initializers/assets.rb | 1 + test/importmap_test.rb | 6 ++++++ 5 files changed, 8 insertions(+) create mode 100644 test/dummy/app/components/another_component_controller.js create mode 100644 test/dummy/app/components/spina/component_controller.js diff --git a/test/dummy/app/assets/config/manifest.js b/test/dummy/app/assets/config/manifest.js index 0c7d56d..c86e0e2 100644 --- a/test/dummy/app/assets/config/manifest.js +++ b/test/dummy/app/assets/config/manifest.js @@ -1,4 +1,5 @@ //= link_tree ../images //= link_directory ../stylesheets .css //= link_tree ../../javascript .js +//= link_tree ../../components .js //= link_tree ../../../lib/assets/javascripts .js diff --git a/test/dummy/app/components/another_component_controller.js b/test/dummy/app/components/another_component_controller.js new file mode 100644 index 0000000..e69de29 diff --git a/test/dummy/app/components/spina/component_controller.js b/test/dummy/app/components/spina/component_controller.js new file mode 100644 index 0000000..e69de29 diff --git a/test/dummy/config/initializers/assets.rb b/test/dummy/config/initializers/assets.rb index fe48fc3..8bd926a 100644 --- a/test/dummy/config/initializers/assets.rb +++ b/test/dummy/config/initializers/assets.rb @@ -5,6 +5,7 @@ # Add additional assets to the asset load path. # Rails.application.config.assets.paths << Emoji.images_path +Rails.application.config.assets.paths << "app/components" # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in the app/assets diff --git a/test/importmap_test.rb b/test/importmap_test.rb index 1bb1c94..fb51dab 100644 --- a/test/importmap_test.rb +++ b/test/importmap_test.rb @@ -14,6 +14,7 @@ def setup 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_all_from "app/components", under: "controllers", to: "", preload: true end end end @@ -52,6 +53,11 @@ def setup assert_match %r|assets/spina/controllers/deeper/again_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/deeper/again_controller"] end + test "directory pin under custom asset path with empty to" do + assert_match %r|assets/spina/component_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/component_controller"] + assert_match %r|assets/another_component_controller-.*\.js|, generate_importmap_json["imports"]["controllers/another_component_controller"] + end + test "directory pin without path or under" do assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"] end From a9f7c41cfee5611b400fb6be92c652f90cb342be Mon Sep 17 00:00:00 2001 From: Jason Taylor Date: Wed, 19 Jul 2023 10:08:44 +1000 Subject: [PATCH 2/3] Reject empty path values when generating module_path --- lib/importmap/map.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/importmap/map.rb b/lib/importmap/map.rb index ac0885e..3ef8107 100644 --- a/lib/importmap/map.rb +++ b/lib/importmap/map.rb @@ -145,7 +145,7 @@ def module_name_from(filename, mapping) end def module_path_from(filename, mapping) - [ mapping.path || mapping.under, filename.to_s ].compact.join("/") + [ mapping.path || mapping.under, filename.to_s ].compact.reject(&:empty?).join("/") end def find_javascript_files_in_tree(path) From 4b6a838d6fb975d50fd52c101a4bc9022552f72f Mon Sep 17 00:00:00 2001 From: Jason Taylor Date: Wed, 19 Jul 2023 11:35:46 +1000 Subject: [PATCH 3/3] Fix the assets.paths for the tests --- test/dummy/config/initializers/assets.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dummy/config/initializers/assets.rb b/test/dummy/config/initializers/assets.rb index 8bd926a..969a5d8 100644 --- a/test/dummy/config/initializers/assets.rb +++ b/test/dummy/config/initializers/assets.rb @@ -5,7 +5,7 @@ # Add additional assets to the asset load path. # Rails.application.config.assets.paths << Emoji.images_path -Rails.application.config.assets.paths << "app/components" +Rails.application.config.assets.paths << Rails.root.join("app/components") # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in the app/assets