summaryrefslogtreecommitdiff
path: root/cli/cache/mod.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-02-22 23:21:05 +0100
committerGitHub <noreply@github.com>2023-02-22 23:21:05 +0100
commit1c14127c4f54d815b3e1be48bddd5198dcb33a50 (patch)
tree56dbbea5e9a39fc95d3c722811b47bce08e21b59 /cli/cache/mod.rs
parentc18e0d1d37878bb4441f7f8d339cc23ac8e68448 (diff)
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 <dsherret@gmail.com>
Diffstat (limited to 'cli/cache/mod.rs')
-rw-r--r--cli/cache/mod.rs14
1 files changed, 14 insertions, 0 deletions
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<FileFetcher>,
root_permissions: PermissionsContainer,
cache_info_enabled: bool,
+ maybe_local_node_modules_url: Option<ModuleSpecifier>,
}
impl FetchCacher {
@@ -52,6 +54,7 @@ impl FetchCacher {
file_fetcher: Arc<FileFetcher>,
root_permissions: PermissionsContainer,
dynamic_permissions: PermissionsContainer,
+ maybe_local_node_modules_url: Option<ModuleSpecifier>,
) -> 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 {