diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-01-24 15:05:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-24 09:05:54 -0500 |
commit | fc2e00152b162280e78b06028d51274e33275629 (patch) | |
tree | ee567a99cabc6633454de231939f7d898146f1d8 /cli/graph_util.rs | |
parent | cadeaae045d2489fe125286b8c2c641c6d973c3f (diff) |
feat: support node built-in module imports (#17264)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/graph_util.rs')
-rw-r--r-- | cli/graph_util.rs | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/cli/graph_util.rs b/cli/graph_util.rs index 22bddf137..243fd6eef 100644 --- a/cli/graph_util.rs +++ b/cli/graph_util.rs @@ -8,7 +8,7 @@ use crate::cache; use crate::cache::TypeCheckCache; use crate::colors; use crate::errors::get_error_class_name; -use crate::npm::resolve_npm_package_reqs; +use crate::npm::resolve_graph_npm_info; use crate::npm::NpmPackageReference; use crate::npm::NpmPackageReq; use crate::proc_state::ProcState; @@ -25,6 +25,7 @@ use deno_graph::GraphImport; use deno_graph::MediaType; use deno_graph::ModuleGraph; use deno_graph::ModuleGraphError; +use deno_graph::ModuleKind; use deno_graph::Range; use deno_graph::Resolved; use deno_runtime::permissions::PermissionsContainer; @@ -54,7 +55,10 @@ pub enum ModuleEntry { #[derive(Debug, Default)] pub struct GraphData { modules: HashMap<ModuleSpecifier, ModuleEntry>, + /// Specifiers that are built-in or external. + external_specifiers: HashSet<ModuleSpecifier>, npm_packages: Vec<NpmPackageReq>, + has_node_builtin_specifier: bool, /// Map of first known referrer locations for each module. Used to enhance /// error messages. referrer_map: HashMap<ModuleSpecifier, Box<Range>>, @@ -83,13 +87,12 @@ impl GraphData { let mut has_npm_specifier_in_graph = false; for (specifier, result) in graph.specifiers() { - if NpmPackageReference::from_specifier(specifier).is_ok() { - has_npm_specifier_in_graph = true; + if self.modules.contains_key(specifier) { continue; } - if self.modules.contains_key(specifier) { - continue; + if !self.has_node_builtin_specifier && specifier.scheme() == "node" { + self.has_node_builtin_specifier = true; } if let Some(found) = graph.redirects.get(specifier) { @@ -97,8 +100,19 @@ impl GraphData { self.modules.insert(specifier.clone(), module_entry); continue; } + match result { - Ok((_, _, media_type)) => { + Ok((_, module_kind, media_type)) => { + if module_kind == ModuleKind::External { + if !has_npm_specifier_in_graph + && NpmPackageReference::from_specifier(specifier).is_ok() + { + has_npm_specifier_in_graph = true; + } + self.external_specifiers.insert(specifier.clone()); + continue; // ignore npm and node specifiers + } + let module = graph.get(specifier).unwrap(); let code = match &module.maybe_source { Some(source) => source.clone(), @@ -147,7 +161,9 @@ impl GraphData { } if has_npm_specifier_in_graph { - self.npm_packages.extend(resolve_npm_package_reqs(graph)); + self + .npm_packages + .extend(resolve_graph_npm_info(graph).package_reqs); } } @@ -157,6 +173,11 @@ impl GraphData { self.modules.iter() } + /// Gets if the graph had a "node:" specifier. + pub fn has_node_builtin_specifier(&self) -> bool { + self.has_node_builtin_specifier + } + /// Gets the npm package requirements from all the encountered graphs /// in the order that they should be resolved. pub fn npm_package_reqs(&self) -> &Vec<NpmPackageReq> { @@ -195,13 +216,14 @@ impl GraphData { } } while let Some(specifier) = visiting.pop_front() { - if NpmPackageReference::from_specifier(specifier).is_ok() { - continue; // skip analyzing npm specifiers - } - let (specifier, entry) = match self.modules.get_key_value(specifier) { Some(pair) => pair, - None => return None, + None => { + if self.external_specifiers.contains(specifier) { + continue; + } + return None; + } }; result.insert(specifier, entry); match entry { @@ -281,6 +303,8 @@ impl GraphData { } Some(Self { modules, + external_specifiers: self.external_specifiers.clone(), + has_node_builtin_specifier: self.has_node_builtin_specifier, npm_packages: self.npm_packages.clone(), referrer_map, graph_imports: self.graph_imports.to_vec(), @@ -547,6 +571,14 @@ pub async fn create_graph_and_maybe_check( } if ps.options.type_check_mode() != TypeCheckMode::None { + // node built-in specifiers use the @types/node package to determine + // types, so inject that now after the lockfile has been written + if graph_data.has_node_builtin_specifier() { + ps.npm_resolver + .inject_synthetic_types_node_package() + .await?; + } + let ts_config_result = ps.options.resolve_ts_config_for_emit(TsConfigType::Check { lib: ps.options.ts_type_lib_window(), |