1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(npm): resolve npm specifiers when root redirected (#17144)

Closes #17136
This commit is contained in:
David Sherret 2022-12-20 11:40:26 -05:00 committed by Bartek Iwańczuk
parent a26cd4d073
commit ba8156e578
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750

View file

@ -277,9 +277,14 @@ pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec<NpmPackageReq> {
}
}
let root_specifiers = graph
.roots
.iter()
.map(|(url, _)| graph.resolve(url))
.collect::<Vec<_>>();
let mut seen = HashSet::new();
let mut specifier_graph = SpecifierTree::default();
for (root, _) in graph.roots.iter() {
for root in &root_specifiers {
if let Some(module) = graph.get(root) {
analyze_module(module, graph, &mut specifier_graph, &mut seen);
}
@ -289,7 +294,7 @@ pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec<NpmPackageReq> {
let mut pending_specifiers = VecDeque::new();
let mut result = Vec::new();
for (specifier, _) in &graph.roots {
for specifier in &root_specifiers {
match NpmPackageReference::from_specifier(specifier) {
Ok(npm_ref) => result.push(npm_ref.req),
Err(_) => {
@ -933,15 +938,44 @@ mod tests {
)]),
},
),
// redirect module
(
"https://deno.land/x/module_redirect/mod.ts".to_string(),
deno_graph::source::Source::Module {
specifier: "https://deno.land/x/module_redirect@0.0.1/mod.ts".to_string(),
content: concat!(
"import 'npm:package-a@module_redirect';",
// try another redirect here
"import 'https://deno.land/x/module_redirect/other.ts';",
).to_string(),
maybe_headers: None,
}
),
(
"https://deno.land/x/module_redirect/other.ts".to_string(),
deno_graph::source::Source::Module {
specifier: "https://deno.land/x/module_redirect@0.0.1/other.ts".to_string(),
content: "import 'npm:package-b@module_redirect';".to_string(),
maybe_headers: None,
}
),
],
Vec::new(),
);
let analyzer = deno_graph::CapturingModuleAnalyzer::default();
let graph = deno_graph::create_graph(
vec![(
ModuleSpecifier::parse("file:///dev/local_module_a/mod.ts").unwrap(),
ModuleKind::Esm,
)],
vec![
(
ModuleSpecifier::parse("file:///dev/local_module_a/mod.ts").unwrap(),
ModuleKind::Esm,
),
(
// test redirect at root
ModuleSpecifier::parse("https://deno.land/x/module_redirect/mod.ts")
.unwrap(),
ModuleKind::Esm,
),
],
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
@ -962,6 +996,8 @@ mod tests {
vec![
"package-a@local_module_a",
"package-b@local_module_a",
"package-a@module_redirect",
"package-b@module_redirect",
"package-b@local_module_b",
"package-data@local_module_b",
"package-a@module_a",