From 1fca7d78b14cfa409bf3e6eabbba97675aade929 Mon Sep 17 00:00:00 2001 From: Viet Hoang <1300077+vietqhoang@users.noreply.github.com> Date: Fri, 25 Oct 2024 07:34:54 +0900 Subject: [PATCH] Preserve order of `to` pin option See comment for context: https://github.com/rails/importmap-rails/pull/274#discussion_r1815765685 --- lib/importmap/packager.rb | 11 +++++------ .../files/pins_with_various_options_importmap.rb | 1 + test/packager_test.rb | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/importmap/packager.rb b/lib/importmap/packager.rb index 237b9b6..32e642e 100644 --- a/lib/importmap/packager.rb +++ b/lib/importmap/packager.rb @@ -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 diff --git a/test/fixtures/files/pins_with_various_options_importmap.rb b/test/fixtures/files/pins_with_various_options_importmap.rb index e4a9bbf..1285857 100644 --- a/test/fixtures/files/pins_with_various_options_importmap.rb +++ b/test/fixtures/files/pins_with_various_options_importmap.rb @@ -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 diff --git a/test/packager_test.rb b/test/packager_test.rb index 1d173e0..baced4d 100644 --- a/test/packager_test.rb +++ b/test/packager_test.rb @@ -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/react@17.0.2") assert_equal %(pin "md5", preload: true # @2.1.3), @packager.vendored_pin_for("md5", "https://cdn/md5@2.1.3") assert_equal %(pin "random", random_option: "foobar", hello: "world" # @8.8.8), @packager.vendored_pin_for("random", "https://cdn/random@8.8.8") + assert_equal %(pin "javascript/typescript", preload: true, to: "javascript--typescript.js" # @0.0.1), @packager.vendored_pin_for("javascript/typescript", "https://cdn/typescript@0.0.1") end test "pin_options_for_package" do