mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 04:52:26 -05:00
refactor(node_resolver): make conditions_from_resolution_mode configurable (#27596)
This commit is contained in:
parent
f912aac2cb
commit
714b40262e
5 changed files with 70 additions and 10 deletions
|
@ -670,6 +670,7 @@ impl CliFactory {
|
||||||
.into_npm_pkg_folder_resolver(),
|
.into_npm_pkg_folder_resolver(),
|
||||||
self.pkg_json_resolver().clone(),
|
self.pkg_json_resolver().clone(),
|
||||||
self.sys(),
|
self.sys(),
|
||||||
|
node_resolver::ConditionsFromResolutionMode::default(),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
.boxed_local(),
|
.boxed_local(),
|
||||||
|
|
|
@ -783,6 +783,7 @@ impl<'a> ResolverFactory<'a> {
|
||||||
npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
||||||
self.pkg_json_resolver.clone(),
|
self.pkg_json_resolver.clone(),
|
||||||
self.sys.clone(),
|
self.sys.clone(),
|
||||||
|
node_resolver::ConditionsFromResolutionMode::default(),
|
||||||
)))
|
)))
|
||||||
})
|
})
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
|
|
@ -833,6 +833,7 @@ pub async fn run(
|
||||||
npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
||||||
pkg_json_resolver.clone(),
|
pkg_json_resolver.clone(),
|
||||||
sys.clone(),
|
sys.clone(),
|
||||||
|
node_resolver::ConditionsFromResolutionMode::default(),
|
||||||
));
|
));
|
||||||
let cjs_tracker = Arc::new(CjsTracker::new(
|
let cjs_tracker = Arc::new(CjsTracker::new(
|
||||||
in_npm_pkg_checker.clone(),
|
in_npm_pkg_checker.clone(),
|
||||||
|
|
|
@ -9,6 +9,7 @@ mod npm;
|
||||||
mod package_json;
|
mod package_json;
|
||||||
mod path;
|
mod path;
|
||||||
mod resolution;
|
mod resolution;
|
||||||
|
|
||||||
mod sync;
|
mod sync;
|
||||||
|
|
||||||
pub use deno_package_json::PackageJson;
|
pub use deno_package_json::PackageJson;
|
||||||
|
@ -22,6 +23,7 @@ pub use package_json::PackageJsonThreadLocalCache;
|
||||||
pub use path::PathClean;
|
pub use path::PathClean;
|
||||||
pub use resolution::parse_npm_pkg_name;
|
pub use resolution::parse_npm_pkg_name;
|
||||||
pub use resolution::resolve_specifier_into_node_modules;
|
pub use resolution::resolve_specifier_into_node_modules;
|
||||||
|
pub use resolution::ConditionsFromResolutionMode;
|
||||||
pub use resolution::IsBuiltInNodeModuleChecker;
|
pub use resolution::IsBuiltInNodeModuleChecker;
|
||||||
pub use resolution::NodeResolution;
|
pub use resolution::NodeResolution;
|
||||||
pub use resolution::NodeResolutionKind;
|
pub use resolution::NodeResolutionKind;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018-2025 the Deno authors. MIT license.
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::fmt::Debug;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
@ -54,12 +55,32 @@ pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"];
|
||||||
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
|
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
|
||||||
static TYPES_ONLY_CONDITIONS: &[&str] = &["types"];
|
static TYPES_ONLY_CONDITIONS: &[&str] = &["types"];
|
||||||
|
|
||||||
fn conditions_from_resolution_mode(
|
type ConditionsFromResolutionModeFn = Box<
|
||||||
|
dyn Fn(ResolutionMode) -> &'static [&'static str] + Send + Sync + 'static,
|
||||||
|
>;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct ConditionsFromResolutionMode(Option<ConditionsFromResolutionModeFn>);
|
||||||
|
|
||||||
|
impl Debug for ConditionsFromResolutionMode {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("ConditionsFromResolutionMode").finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConditionsFromResolutionMode {
|
||||||
|
pub fn new(func: ConditionsFromResolutionModeFn) -> Self {
|
||||||
|
Self(Some(func))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve(
|
||||||
|
&self,
|
||||||
resolution_mode: ResolutionMode,
|
resolution_mode: ResolutionMode,
|
||||||
) -> &'static [&'static str] {
|
) -> &'static [&'static str] {
|
||||||
match resolution_mode {
|
match &self.0 {
|
||||||
ResolutionMode::Import => DEFAULT_CONDITIONS,
|
Some(func) => func(ResolutionMode::Import),
|
||||||
ResolutionMode::Require => REQUIRE_CONDITIONS,
|
None => resolution_mode.default_conditions(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +90,15 @@ pub enum ResolutionMode {
|
||||||
Require,
|
Require,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ResolutionMode {
|
||||||
|
pub fn default_conditions(&self) -> &'static [&'static str] {
|
||||||
|
match self {
|
||||||
|
ResolutionMode::Import => DEFAULT_CONDITIONS,
|
||||||
|
ResolutionMode::Require => REQUIRE_CONDITIONS,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum NodeResolutionKind {
|
pub enum NodeResolutionKind {
|
||||||
Execution,
|
Execution,
|
||||||
|
@ -120,6 +150,7 @@ pub struct NodeResolver<
|
||||||
npm_pkg_folder_resolver: NpmPackageFolderResolverRc,
|
npm_pkg_folder_resolver: NpmPackageFolderResolverRc,
|
||||||
pkg_json_resolver: PackageJsonResolverRc<TSys>,
|
pkg_json_resolver: PackageJsonResolverRc<TSys>,
|
||||||
sys: TSys,
|
sys: TSys,
|
||||||
|
conditions_from_resolution_mode: ConditionsFromResolutionMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
|
@ -133,6 +164,7 @@ impl<
|
||||||
npm_pkg_folder_resolver: NpmPackageFolderResolverRc,
|
npm_pkg_folder_resolver: NpmPackageFolderResolverRc,
|
||||||
pkg_json_resolver: PackageJsonResolverRc<TSys>,
|
pkg_json_resolver: PackageJsonResolverRc<TSys>,
|
||||||
sys: TSys,
|
sys: TSys,
|
||||||
|
conditions_from_resolution_mode: ConditionsFromResolutionMode,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
in_npm_pkg_checker,
|
in_npm_pkg_checker,
|
||||||
|
@ -140,6 +172,7 @@ impl<
|
||||||
npm_pkg_folder_resolver,
|
npm_pkg_folder_resolver,
|
||||||
pkg_json_resolver,
|
pkg_json_resolver,
|
||||||
sys,
|
sys,
|
||||||
|
conditions_from_resolution_mode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,11 +230,14 @@ impl<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let conditions = self
|
||||||
|
.conditions_from_resolution_mode
|
||||||
|
.resolve(resolution_mode);
|
||||||
let url = self.module_resolve(
|
let url = self.module_resolve(
|
||||||
specifier,
|
specifier,
|
||||||
referrer,
|
referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
conditions_from_resolution_mode(resolution_mode),
|
conditions,
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
@ -211,6 +247,7 @@ impl<
|
||||||
&file_path,
|
&file_path,
|
||||||
Some(referrer),
|
Some(referrer),
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
)?
|
)?
|
||||||
} else {
|
} else {
|
||||||
url
|
url
|
||||||
|
@ -343,7 +380,9 @@ impl<
|
||||||
&package_subpath,
|
&package_subpath,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
conditions_from_resolution_mode(resolution_mode),
|
self
|
||||||
|
.conditions_from_resolution_mode
|
||||||
|
.resolve(resolution_mode),
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)?;
|
)?;
|
||||||
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
|
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
|
||||||
|
@ -411,6 +450,7 @@ impl<
|
||||||
path: &Path,
|
path: &Path,
|
||||||
maybe_referrer: Option<&Url>,
|
maybe_referrer: Option<&Url>,
|
||||||
resolution_mode: ResolutionMode,
|
resolution_mode: ResolutionMode,
|
||||||
|
conditions: &[&str],
|
||||||
) -> Result<Url, TypesNotFoundError> {
|
) -> Result<Url, TypesNotFoundError> {
|
||||||
fn probe_extensions<TSys: FsMetadata>(
|
fn probe_extensions<TSys: FsMetadata>(
|
||||||
sys: &TSys,
|
sys: &TSys,
|
||||||
|
@ -474,7 +514,7 @@ impl<
|
||||||
/* sub path */ ".",
|
/* sub path */ ".",
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
conditions_from_resolution_mode(resolution_mode),
|
conditions,
|
||||||
NodeResolutionKind::Types,
|
NodeResolutionKind::Types,
|
||||||
);
|
);
|
||||||
if let Ok(resolution) = resolution_result {
|
if let Ok(resolution) = resolution_result {
|
||||||
|
@ -855,6 +895,7 @@ impl<
|
||||||
&path,
|
&path,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
)?));
|
)?));
|
||||||
} else {
|
} else {
|
||||||
return Ok(Some(url));
|
return Ok(Some(url));
|
||||||
|
@ -1212,6 +1253,7 @@ impl<
|
||||||
package_subpath,
|
package_subpath,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)
|
)
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
|
@ -1249,6 +1291,7 @@ impl<
|
||||||
package_json,
|
package_json,
|
||||||
referrer,
|
referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)
|
)
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
|
@ -1268,6 +1311,7 @@ impl<
|
||||||
package_json,
|
package_json,
|
||||||
referrer,
|
referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)
|
)
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
|
@ -1281,6 +1325,7 @@ impl<
|
||||||
package_subpath,
|
package_subpath,
|
||||||
referrer,
|
referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)
|
)
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
|
@ -1294,12 +1339,18 @@ impl<
|
||||||
package_subpath: &str,
|
package_subpath: &str,
|
||||||
referrer: Option<&Url>,
|
referrer: Option<&Url>,
|
||||||
resolution_mode: ResolutionMode,
|
resolution_mode: ResolutionMode,
|
||||||
|
conditions: &[&str],
|
||||||
resolution_kind: NodeResolutionKind,
|
resolution_kind: NodeResolutionKind,
|
||||||
) -> Result<Url, TypesNotFoundError> {
|
) -> Result<Url, TypesNotFoundError> {
|
||||||
assert_ne!(package_subpath, ".");
|
assert_ne!(package_subpath, ".");
|
||||||
let file_path = directory.join(package_subpath);
|
let file_path = directory.join(package_subpath);
|
||||||
if resolution_kind.is_types() {
|
if resolution_kind.is_types() {
|
||||||
Ok(self.path_to_declaration_url(&file_path, referrer, resolution_mode)?)
|
Ok(self.path_to_declaration_url(
|
||||||
|
&file_path,
|
||||||
|
referrer,
|
||||||
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
|
)?)
|
||||||
} else {
|
} else {
|
||||||
Ok(url_from_file_path(&file_path).unwrap())
|
Ok(url_from_file_path(&file_path).unwrap())
|
||||||
}
|
}
|
||||||
|
@ -1311,6 +1362,7 @@ impl<
|
||||||
package_subpath: &str,
|
package_subpath: &str,
|
||||||
maybe_referrer: Option<&Url>,
|
maybe_referrer: Option<&Url>,
|
||||||
resolution_mode: ResolutionMode,
|
resolution_mode: ResolutionMode,
|
||||||
|
conditions: &[&str],
|
||||||
resolution_kind: NodeResolutionKind,
|
resolution_kind: NodeResolutionKind,
|
||||||
) -> Result<Url, LegacyResolveError> {
|
) -> Result<Url, LegacyResolveError> {
|
||||||
if package_subpath == "." {
|
if package_subpath == "." {
|
||||||
|
@ -1327,6 +1379,7 @@ impl<
|
||||||
package_subpath,
|
package_subpath,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
resolution_kind,
|
resolution_kind,
|
||||||
)
|
)
|
||||||
.map_err(|err| err.into())
|
.map_err(|err| err.into())
|
||||||
|
@ -1338,6 +1391,7 @@ impl<
|
||||||
package_json: &PackageJson,
|
package_json: &PackageJson,
|
||||||
maybe_referrer: Option<&Url>,
|
maybe_referrer: Option<&Url>,
|
||||||
resolution_mode: ResolutionMode,
|
resolution_mode: ResolutionMode,
|
||||||
|
conditions: &[&str],
|
||||||
resolution_kind: NodeResolutionKind,
|
resolution_kind: NodeResolutionKind,
|
||||||
) -> Result<Url, LegacyResolveError> {
|
) -> Result<Url, LegacyResolveError> {
|
||||||
let pkg_json_kind = match resolution_mode {
|
let pkg_json_kind = match resolution_mode {
|
||||||
|
@ -1356,6 +1410,7 @@ impl<
|
||||||
&main,
|
&main,
|
||||||
maybe_referrer,
|
maybe_referrer,
|
||||||
resolution_mode,
|
resolution_mode,
|
||||||
|
conditions,
|
||||||
);
|
);
|
||||||
// don't surface errors, fallback to checking the index now
|
// don't surface errors, fallback to checking the index now
|
||||||
if let Ok(url) = decl_url_result {
|
if let Ok(url) = decl_url_result {
|
||||||
|
|
Loading…
Add table
Reference in a new issue