Skip to content

Add package information in vendored package file #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/importmap/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ def download_package_file(package, url)
response = Net::HTTP.get_response(URI(url))

if response.code == "200"
save_vendored_package(package, response.body)
save_vendored_package(package, url, response.body)
else
handle_failure_response(response)
end
end

def save_vendored_package(package, source)
def save_vendored_package(package, url, source)
File.open(vendored_package_path(package), "w+") do |vendored_package|
vendored_package.write "// #{package}#{extract_package_version_from(url)} downloaded from #{url}\n\n"

vendored_package.write remove_sourcemap_comment_from(source).force_encoding("UTF-8")
end
end
Expand Down
17 changes: 11 additions & 6 deletions test/packager_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ class Importmap::PackagerIntegrationTest < ActiveSupport::TestCase
Rails.root.join("config/importmap.rb"),
vendor_path: Pathname.new(vendor_dir)

@packager.download("@github/webauthn-json",
"https://ga.jspm.io/npm:@github/[email protected]/dist/main/webauthn-json.js")
assert File.exist?(Pathname.new(vendor_dir).join("@github--webauthn-json.js"))

@packager.download("react", "https://ga.jspm.io/npm:[email protected]/index.js")
assert File.exist?(Pathname.new(vendor_dir).join("react.js"))
package_url = "https://ga.jspm.io/npm:@github/[email protected]/dist/main/webauthn-json.js"
@packager.download("@github/webauthn-json", package_url)
vendored_package_file = Pathname.new(vendor_dir).join("@github--webauthn-json.js")
assert File.exist?(vendored_package_file)
assert_equal "// @github/[email protected] downloaded from #{package_url}", File.readlines(vendored_package_file).first.strip

package_url = "https://ga.jspm.io/npm:[email protected]/index.js"
vendored_package_file = Pathname.new(vendor_dir).join("react.js")
@packager.download("react", package_url)
assert File.exist?(vendored_package_file)
assert_equal "// [email protected] downloaded from #{package_url}", File.readlines(vendored_package_file).first.strip

@packager.remove("react")
assert_not File.exist?(Pathname.new(vendor_dir).join("react.js"))
Expand Down