summaryrefslogtreecommitdiff
path: root/cli/cache/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/cache/mod.rs')
-rw-r--r--cli/cache/mod.rs101
1 files changed, 22 insertions, 79 deletions
diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs
index bf8f1b1f0..50fc135dd 100644
--- a/cli/cache/mod.rs
+++ b/cli/cache/mod.rs
@@ -8,14 +8,9 @@ use crate::file_fetcher::FetchOptions;
use crate::file_fetcher::FetchPermissionsOptionRef;
use crate::file_fetcher::FileFetcher;
use crate::file_fetcher::FileOrRedirect;
-use crate::npm::CliNpmResolver;
-use crate::resolver::CliNodeResolver;
use crate::util::fs::atomic_write_file_with_retries;
use crate::util::fs::atomic_write_file_with_retries_and_fs;
use crate::util::fs::AtomicWriteFileFsAdapter;
-use crate::util::path::specifier_has_extension;
-use crate::util::text_encoding::arc_str_to_bytes;
-use crate::util::text_encoding::from_utf8_lossy_owned;
use deno_ast::MediaType;
use deno_core::futures;
@@ -25,7 +20,9 @@ use deno_graph::source::CacheInfo;
use deno_graph::source::LoadFuture;
use deno_graph::source::LoadResponse;
use deno_graph::source::Loader;
+use deno_runtime::deno_fs;
use deno_runtime::deno_permissions::PermissionsContainer;
+use node_resolver::InNpmPackageChecker;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
@@ -60,7 +57,6 @@ pub use fast_check::FastCheckCache;
pub use incremental::IncrementalCache;
pub use module_info::ModuleInfoCache;
pub use node::NodeAnalysisCache;
-pub use parsed_source::EsmOrCjsChecker;
pub use parsed_source::LazyGraphSourceParser;
pub use parsed_source::ParsedSourceCache;
@@ -181,46 +177,40 @@ pub struct FetchCacherOptions {
pub permissions: PermissionsContainer,
/// If we're publishing for `deno publish`.
pub is_deno_publish: bool,
- pub unstable_detect_cjs: bool,
}
/// A "wrapper" for the FileFetcher and DiskCache for the Deno CLI that provides
/// a concise interface to the DENO_DIR when building module graphs.
pub struct FetchCacher {
pub file_header_overrides: HashMap<ModuleSpecifier, HashMap<String, String>>,
- esm_or_cjs_checker: Arc<EsmOrCjsChecker>,
file_fetcher: Arc<FileFetcher>,
+ fs: Arc<dyn deno_fs::FileSystem>,
global_http_cache: Arc<GlobalHttpCache>,
- node_resolver: Arc<CliNodeResolver>,
- npm_resolver: Arc<dyn CliNpmResolver>,
+ in_npm_pkg_checker: Arc<dyn InNpmPackageChecker>,
module_info_cache: Arc<ModuleInfoCache>,
permissions: PermissionsContainer,
is_deno_publish: bool,
- unstable_detect_cjs: bool,
cache_info_enabled: bool,
}
impl FetchCacher {
pub fn new(
- esm_or_cjs_checker: Arc<EsmOrCjsChecker>,
file_fetcher: Arc<FileFetcher>,
+ fs: Arc<dyn deno_fs::FileSystem>,
global_http_cache: Arc<GlobalHttpCache>,
- node_resolver: Arc<CliNodeResolver>,
- npm_resolver: Arc<dyn CliNpmResolver>,
+ in_npm_pkg_checker: Arc<dyn InNpmPackageChecker>,
module_info_cache: Arc<ModuleInfoCache>,
options: FetchCacherOptions,
) -> Self {
Self {
file_fetcher,
- esm_or_cjs_checker,
+ fs,
global_http_cache,
- node_resolver,
- npm_resolver,
+ in_npm_pkg_checker,
module_info_cache,
file_header_overrides: options.file_header_overrides,
permissions: options.permissions,
is_deno_publish: options.is_deno_publish,
- unstable_detect_cjs: options.unstable_detect_cjs,
cache_info_enabled: false,
}
}
@@ -271,70 +261,23 @@ impl Loader for FetchCacher {
) -> LoadFuture {
use deno_graph::source::CacheSetting as LoaderCacheSetting;
- if specifier.scheme() == "file" {
- if specifier.path().contains("/node_modules/") {
- // The specifier might be in a completely different symlinked tree than
- // what the node_modules url is in (ex. `/my-project-1/node_modules`
- // symlinked to `/my-project-2/node_modules`), so first we checked if the path
- // is in a node_modules dir to avoid needlessly canonicalizing, then now compare
- // against the canonicalized specifier.
- let specifier =
- crate::node::resolve_specifier_into_node_modules(specifier);
- if self.npm_resolver.in_npm_package(&specifier) {
- return Box::pin(futures::future::ready(Ok(Some(
- LoadResponse::External { specifier },
- ))));
- }
- }
-
- // make local CJS modules external to the graph
- if specifier_has_extension(specifier, "cjs") {
+ if specifier.scheme() == "file"
+ && specifier.path().contains("/node_modules/")
+ {
+ // The specifier might be in a completely different symlinked tree than
+ // what the node_modules url is in (ex. `/my-project-1/node_modules`
+ // symlinked to `/my-project-2/node_modules`), so first we checked if the path
+ // is in a node_modules dir to avoid needlessly canonicalizing, then now compare
+ // against the canonicalized specifier.
+ let specifier = crate::node::resolve_specifier_into_node_modules(
+ specifier,
+ self.fs.as_ref(),
+ );
+ if self.in_npm_pkg_checker.in_npm_package(&specifier) {
return Box::pin(futures::future::ready(Ok(Some(
- LoadResponse::External {
- specifier: specifier.clone(),
- },
+ LoadResponse::External { specifier },
))));
}
-
- if self.unstable_detect_cjs && specifier_has_extension(specifier, "js") {
- if let Ok(Some(pkg_json)) =
- self.node_resolver.get_closest_package_json(specifier)
- {
- if pkg_json.typ == "commonjs" {
- if let Ok(path) = specifier.to_file_path() {
- if let Ok(bytes) = std::fs::read(&path) {
- let text: Arc<str> = from_utf8_lossy_owned(bytes).into();
- let is_es_module = match self.esm_or_cjs_checker.is_esm(
- specifier,
- text.clone(),
- MediaType::JavaScript,
- ) {
- Ok(value) => value,
- Err(err) => {
- return Box::pin(futures::future::ready(Err(err.into())));
- }
- };
- if !is_es_module {
- self.node_resolver.mark_cjs_resolution(specifier.clone());
- return Box::pin(futures::future::ready(Ok(Some(
- LoadResponse::External {
- specifier: specifier.clone(),
- },
- ))));
- } else {
- return Box::pin(futures::future::ready(Ok(Some(
- LoadResponse::Module {
- specifier: specifier.clone(),
- content: arc_str_to_bytes(text),
- maybe_headers: None,
- },
- ))));
- }
- }
- }
- }
- }
- }
}
if self.is_deno_publish