From 1e617ea2d4a7e2a8e66813e755fd8be8318afc28 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 18 Feb 2022 23:10:40 -0500 Subject: [PATCH] fix(vendor): do not add absolute specifiers to scopes (#13710) --- cli/tools/vendor/build.rs | 51 ++++++++++++++++++++++++++++++++++ cli/tools/vendor/import_map.rs | 7 +---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/cli/tools/vendor/build.rs b/cli/tools/vendor/build.rs index 58f351dd89..260aa39f5e 100644 --- a/cli/tools/vendor/build.rs +++ b/cli/tools/vendor/build.rs @@ -568,6 +568,57 @@ mod test { ); } + #[tokio::test] + async fn same_origin_absolute_with_redirect() { + let mut builder = VendorTestBuilder::with_default_setup(); + let output = builder + .with_loader(|loader| { + loader + .add( + "/mod.ts", + r#"import "https://localhost/subdir/sub/mod.ts";"#, + ) + .add( + "https://localhost/subdir/sub/mod.ts", + "import 'https://localhost/std/hash/mod.ts'", + ) + .add_redirect( + "https://localhost/std/hash/mod.ts", + "https://localhost/std@0.1.0/hash/mod.ts", + ) + .add( + "https://localhost/std@0.1.0/hash/mod.ts", + "export class Test {}", + ); + }) + .build() + .await + .unwrap(); + + assert_eq!( + output.import_map, + Some(json!({ + "imports": { + "https://localhost/": "./localhost/", + "https://localhost/std/hash/mod.ts": "./localhost/std@0.1.0/hash/mod.ts" + } + })) + ); + assert_eq!( + output.files, + to_file_vec(&[ + ( + "/vendor/localhost/std@0.1.0/hash/mod.ts", + "export class Test {}" + ), + ( + "/vendor/localhost/subdir/sub/mod.ts", + "import 'https://localhost/std/hash/mod.ts'" + ), + ]), + ); + } + fn to_file_vec(items: &[(&str, &str)]) -> Vec<(String, String)> { items .iter() diff --git a/cli/tools/vendor/import_map.rs b/cli/tools/vendor/import_map.rs index 7e18d56aa5..502b12f6e4 100644 --- a/cli/tools/vendor/import_map.rs +++ b/cli/tools/vendor/import_map.rs @@ -213,12 +213,7 @@ fn handle_dep_specifier( return; } - if referrer.origin() == specifier.origin() { - let imports = import_map.scope(base_specifier); - imports.add(sub_path.to_string(), &specifier); - } else { - import_map.imports.add(text.to_string(), &specifier); - } + import_map.imports.add(text.to_string(), &specifier); } else { let expected_relative_specifier_text = mappings.relative_specifier_text(referrer, &specifier);