From 8f102e49c10f28faaf88fd644a4bc6a46d2b36dc Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 4 Aug 2022 09:39:14 -0400 Subject: [PATCH] fix(vendor): existing import map with bare specifier in remote (#15390) --- cli/tools/vendor/build.rs | 47 +++++++++++++++++++++++++++++++++- cli/tools/vendor/import_map.rs | 6 ++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/cli/tools/vendor/build.rs b/cli/tools/vendor/build.rs index f56204c772..f7921251eb 100644 --- a/cli/tools/vendor/build.rs +++ b/cli/tools/vendor/build.rs @@ -66,7 +66,6 @@ pub fn build( // build the graph graph.lock()?; - graph.valid()?; let graph_errors = graph.errors(); if !graph_errors.is_empty() { @@ -828,6 +827,52 @@ mod test { ); } + #[tokio::test] + async fn existing_import_map_remote_dep_bare_specifier() { + let mut builder = VendorTestBuilder::with_default_setup(); + let mut original_import_map = builder.new_import_map("/import_map2.json"); + original_import_map + .imports_mut() + .append( + "twind".to_string(), + "https://localhost/twind.ts".to_string(), + ) + .unwrap(); + + let output = builder + .with_loader(|loader| { + loader.add("/mod.ts", "import 'https://remote/mod.ts';"); + loader.add("https://remote/mod.ts", "import 'twind';"); + loader.add("https://localhost/twind.ts", "export class Test {}"); + }) + .set_original_import_map(original_import_map.clone()) + .build() + .await + .unwrap(); + + assert_eq!( + output.import_map, + Some(json!({ + "imports": { + "https://localhost/": "./localhost/", + "https://remote/": "./remote/" + }, + "scopes": { + "./remote/": { + "twind": "./localhost/twind.ts" + }, + } + })) + ); + assert_eq!( + output.files, + to_file_vec(&[ + ("/vendor/localhost/twind.ts", "export class Test {}"), + ("/vendor/remote/mod.ts", "import 'twind';"), + ]), + ); + } + #[tokio::test] async fn existing_import_map_mapped_bare_specifier() { let mut builder = VendorTestBuilder::with_default_setup(); diff --git a/cli/tools/vendor/import_map.rs b/cli/tools/vendor/import_map.rs index e03260e3e3..7e72693a1e 100644 --- a/cli/tools/vendor/import_map.rs +++ b/cli/tools/vendor/import_map.rs @@ -333,12 +333,12 @@ fn handle_remote_dep_specifier( return; } - let base_specifier = mappings.base_specifier(specifier); + let base_referrer = mappings.base_specifier(referrer); let base_dir = import_map.base_dir().clone(); - let imports = import_map.scope(base_specifier); + let imports = import_map.scope(base_referrer); if text.starts_with("./") || text.starts_with("../") { // resolve relative specifier key - let mut local_base_specifier = mappings.local_uri(base_specifier); + let mut local_base_specifier = mappings.local_uri(base_referrer); local_base_specifier = local_base_specifier // path includes "/" so make it relative .join(&format!(".{}", unresolved_specifier.path()))