Skip to content

Commit

Permalink
Merge pull request #192 from jsntv200/fix/module_path_from
Browse files Browse the repository at this point in the history
Remove prepended forward slash when mapping.path is empty
  • Loading branch information
rafaelfranca authored Oct 27, 2023
2 parents fa34a62 + 4b6a838 commit f80d0a3
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/dummy/config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << Rails.root.join("app/components")

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
Expand Down
6 changes: 6 additions & 0 deletions test/importmap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f80d0a3

Please sign in to comment.