From 033b70af19300a4e34dcf19ab0031245bfc19625 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 24 Feb 2023 19:35:43 -0500 Subject: fix(npm): lazily install package.json dependencies only when necessary (#17931) This lazily does an "npm install" when any package name matches what's found in the package.json or when running a script from package.json with deno task. Part of #17916 Closes #17928 --- cli/node/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'cli/node') diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 470003057..e45694fd6 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -40,6 +40,7 @@ use regex::Regex; use crate::cache::NodeAnalysisCache; use crate::file_fetcher::FileFetcher; use crate::npm::NpmPackageResolver; +use crate::util::fs::canonicalize_path_maybe_not_exists; mod analyze; @@ -285,6 +286,25 @@ pub fn node_resolve_npm_reference( Ok(Some(resolve_response)) } +/// Resolves a specifier that is pointing into a node_modules folder. +/// +/// Note: This should be called whenever getting the specifier from +/// a Module::External(module) reference because that module might +/// not be fully resolved at the time deno_graph is analyzing it +/// because the node_modules folder might not exist at that time. +pub fn resolve_specifier_into_node_modules( + specifier: &ModuleSpecifier, +) -> ModuleSpecifier { + specifier + .to_file_path() + .ok() + // this path might not exist at the time the graph is being created + // because the node_modules folder might not yet exist + .and_then(|path| canonicalize_path_maybe_not_exists(&path).ok()) + .and_then(|path| ModuleSpecifier::from_file_path(path).ok()) + .unwrap_or_else(|| specifier.clone()) +} + pub fn node_resolve_binary_commands( pkg_nv: &NpmPackageNv, npm_resolver: &NpmPackageResolver, -- cgit v1.2.3