From fc2e00152b162280e78b06028d51274e33275629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 24 Jan 2023 15:05:54 +0100 Subject: feat: support node built-in module imports (#17264) Co-authored-by: David Sherret --- cli/npm/resolution/specifier.rs | 43 ++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'cli/npm/resolution/specifier.rs') diff --git a/cli/npm/resolution/specifier.rs b/cli/npm/resolution/specifier.rs index 9a7f67cf8..6667c60dd 100644 --- a/cli/npm/resolution/specifier.rs +++ b/cli/npm/resolution/specifier.rs @@ -168,8 +168,15 @@ impl NpmVersionMatcher for NpmPackageReq { } } -/// Resolves the npm package requirements from the graph attempting. The order -/// returned is the order they should be resolved in. +pub struct GraphNpmInfo { + /// The order of these package requirements is the order they + /// should be resolved in. + pub package_reqs: Vec, + /// Gets if the graph had a built-in node specifier (ex. `node:fs`). + pub has_node_builtin_specifier: bool, +} + +/// Resolves npm specific information from the graph. /// /// This function will analyze the module graph for parent-most folder /// specifiers of all modules, then group npm specifiers together as found in @@ -211,7 +218,7 @@ impl NpmVersionMatcher for NpmPackageReq { /// /// Then it would resolve the npm specifiers in each of those groups according /// to that tree going by tree depth. -pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec { +pub fn resolve_graph_npm_info(graph: &ModuleGraph) -> GraphNpmInfo { fn collect_specifiers<'a>( graph: &'a ModuleGraph, module: &'a deno_graph::Module, @@ -248,6 +255,7 @@ pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec { graph: &ModuleGraph, specifier_graph: &mut SpecifierTree, seen: &mut HashSet, + has_node_builtin_specifier: &mut bool, ) { if !seen.insert(module.specifier.clone()) { return; // already visited @@ -267,12 +275,22 @@ pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec { .dependencies .insert(get_folder_path_specifier(specifier)); } + + if !*has_node_builtin_specifier && specifier.scheme() == "node" { + *has_node_builtin_specifier = true; + } } // now visit all the dependencies for specifier in &specifiers { if let Some(module) = graph.get(specifier) { - analyze_module(module, graph, specifier_graph, seen); + analyze_module( + module, + graph, + specifier_graph, + seen, + has_node_builtin_specifier, + ); } } } @@ -284,9 +302,16 @@ pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec { .collect::>(); let mut seen = HashSet::new(); let mut specifier_graph = SpecifierTree::default(); + let mut has_node_builtin_specifier = false; for root in &root_specifiers { if let Some(module) = graph.get(root) { - analyze_module(module, graph, &mut specifier_graph, &mut seen); + analyze_module( + module, + graph, + &mut specifier_graph, + &mut seen, + &mut has_node_builtin_specifier, + ); } } @@ -324,7 +349,10 @@ pub fn resolve_npm_package_reqs(graph: &ModuleGraph) -> Vec { } } - result + GraphNpmInfo { + has_node_builtin_specifier, + package_reqs: result, + } } fn get_folder_path_specifier(specifier: &ModuleSpecifier) -> ModuleSpecifier { @@ -979,7 +1007,8 @@ mod tests { }, ) .await; - let reqs = resolve_npm_package_reqs(&graph) + let reqs = resolve_graph_npm_info(&graph) + .package_reqs .into_iter() .map(|r| r.to_string()) .collect::>(); -- cgit v1.2.3