fix(vendor): existing import map with bare specifier in remote (#15390)

This commit is contained in:
David Sherret 2022-08-04 09:39:14 -04:00 committed by crowlkats
parent 2205d9345e
commit 967b7f8b5b
No known key found for this signature in database
GPG Key ID: A82C9D461FC483E8
2 changed files with 49 additions and 4 deletions

View File

@ -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();

View File

@ -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()))