From 1c14127c4f54d815b3e1be48bddd5198dcb33a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 22 Feb 2023 23:21:05 +0100 Subject: feat: support bare specifier resolution with package.json (#17864) This commit enables resolution of "bare specifiers" (eg. "import express from 'express';") if a "package.json" file is discovered. It's a step towards being able to run projects authored for Node.js without any changes. With this commit we are able to successfully run Vite projects without any changes to the user code. --------- Co-authored-by: David Sherret --- cli/cache/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'cli/cache') diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index eead9c419..6c50c4c8b 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -3,6 +3,7 @@ use crate::errors::get_error_class_name; use crate::file_fetcher::FileFetcher; +use deno_core::futures; use deno_core::futures::FutureExt; use deno_core::ModuleSpecifier; use deno_graph::source::CacheInfo; @@ -44,6 +45,7 @@ pub struct FetchCacher { file_fetcher: Arc, root_permissions: PermissionsContainer, cache_info_enabled: bool, + maybe_local_node_modules_url: Option, } impl FetchCacher { @@ -52,6 +54,7 @@ impl FetchCacher { file_fetcher: Arc, root_permissions: PermissionsContainer, dynamic_permissions: PermissionsContainer, + maybe_local_node_modules_url: Option, ) -> Self { Self { emit_cache, @@ -59,6 +62,7 @@ impl FetchCacher { file_fetcher, root_permissions, cache_info_enabled: false, + maybe_local_node_modules_url, } } @@ -96,6 +100,16 @@ impl Loader for FetchCacher { specifier: &ModuleSpecifier, is_dynamic: bool, ) -> LoadFuture { + if let Some(node_modules_url) = self.maybe_local_node_modules_url.as_ref() { + if specifier.as_str().starts_with(node_modules_url.as_str()) { + return Box::pin(futures::future::ready(Ok(Some( + LoadResponse::External { + specifier: specifier.clone(), + }, + )))); + } + } + let permissions = if is_dynamic { self.dynamic_permissions.clone() } else { -- cgit v1.2.3