Skip to content

Commit

Permalink
Preserve order of to pin option
Browse files Browse the repository at this point in the history
See comment for context: #274 (comment)
  • Loading branch information
vietqhoang committed Oct 24, 2024
1 parent 9e35750 commit 1fca7d7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/importmap/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ def pin_for(package, url)
def vendored_pin_for(package, url)
filename = package_filename(package)
version = extract_package_version_from(url)
line_formatted_pin_options = pin_options_for_package(package).except("to").map { |option, value| %(#{option}: #{value.is_a?(String) ? %("#{value}") : value}) }
pin_components = [
%(pin "#{package}"),
(%(to: "#{filename}") unless "#{package}.js" == filename),
*line_formatted_pin_options
].compact
line_formatted_pin_options =
pin_options_for_package(package).
tap { |options| "#{package}.js" == filename ? options.delete("to") : options["to"] = filename }.
map { |option, value| %(#{option}: #{value.is_a?(String) ? %("#{value}") : value}) }
pin_components = [%(pin "#{package}"), *line_formatted_pin_options]

%(#{pin_components.join(", ")} # #{version})
end
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/files/pins_with_various_options_importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
pin "some_file" # 0.2.1
pin "another_file",to:'another_file.js' # @0.0.16
pin "random", random_option: "foobar", hello: "world" # 7.7.7
pin "javascript/typescript", preload: true, to: "https://cdn.skypack.dev/typescript" # 0.0.0
1 change: 1 addition & 0 deletions test/packager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def code() "200" end
assert_equal %(pin "javascript/react", to: "javascript--react.js" # @17.0.2), @packager.vendored_pin_for("javascript/react", "https://cdn/[email protected]")
assert_equal %(pin "md5", preload: true # @2.1.3), @packager.vendored_pin_for("md5", "https://cdn/[email protected]")
assert_equal %(pin "random", random_option: "foobar", hello: "world" # @8.8.8), @packager.vendored_pin_for("random", "https://cdn/[email protected]")
assert_equal %(pin "javascript/typescript", preload: true, to: "javascript--typescript.js" # @0.0.1), @packager.vendored_pin_for("javascript/typescript", "https://cdn/[email protected]")
end

test "pin_options_for_package" do
Expand Down

0 comments on commit 1fca7d7

Please sign in to comment.