Skip to content

Commit

Permalink
refactor(node_resolver): make conditions_from_resolution_mode configu…
Browse files Browse the repository at this point in the history
…rable (#27596)
  • Loading branch information
theswerd authored and bartlomieju committed Jan 16, 2025
1 parent 6fc6f5e commit 4284566
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
1 change: 1 addition & 0 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ impl CliFactory {
.into_npm_pkg_folder_resolver(),
self.pkg_json_resolver().clone(),
self.sys(),
node_resolver::ConditionsFromResolutionMode::default(),
)))
}
.boxed_local(),
Expand Down
1 change: 1 addition & 0 deletions cli/lsp/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ impl<'a> ResolverFactory<'a> {
npm_resolver.clone().into_npm_pkg_folder_resolver(),
self.pkg_json_resolver.clone(),
self.sys.clone(),
node_resolver::ConditionsFromResolutionMode::default(),
)))
})
.as_ref()
Expand Down
1 change: 1 addition & 0 deletions cli/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ pub async fn run(
npm_resolver.clone().into_npm_pkg_folder_resolver(),
pkg_json_resolver.clone(),
sys.clone(),
node_resolver::ConditionsFromResolutionMode::default(),
));
let cjs_tracker = Arc::new(CjsTracker::new(
in_npm_pkg_checker.clone(),
Expand Down
2 changes: 2 additions & 0 deletions resolvers/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod npm;
mod package_json;
mod path;
mod resolution;

mod sync;

pub use deno_package_json::PackageJson;
Expand All @@ -22,6 +23,7 @@ pub use package_json::PackageJsonThreadLocalCache;
pub use path::PathClean;
pub use resolution::parse_npm_pkg_name;
pub use resolution::resolve_specifier_into_node_modules;
pub use resolution::ConditionsFromResolutionMode;
pub use resolution::IsBuiltInNodeModuleChecker;
pub use resolution::NodeResolution;
pub use resolution::NodeResolutionKind;
Expand Down
75 changes: 65 additions & 10 deletions resolvers/node/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::fmt::Debug;
use std::path::Path;
use std::path::PathBuf;

Expand Down Expand Up @@ -54,12 +55,32 @@ pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"];
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
static TYPES_ONLY_CONDITIONS: &[&str] = &["types"];

fn conditions_from_resolution_mode(
resolution_mode: ResolutionMode,
) -> &'static [&'static str] {
match resolution_mode {
ResolutionMode::Import => DEFAULT_CONDITIONS,
ResolutionMode::Require => REQUIRE_CONDITIONS,
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,
) -> &'static [&'static str] {
match &self.0 {
Some(func) => func(ResolutionMode::Import),
None => resolution_mode.default_conditions(),
}
}
}

Expand All @@ -69,6 +90,15 @@ pub enum ResolutionMode {
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)]
pub enum NodeResolutionKind {
Execution,
Expand Down Expand Up @@ -120,6 +150,7 @@ pub struct NodeResolver<
npm_pkg_folder_resolver: NpmPackageFolderResolverRc,
pkg_json_resolver: PackageJsonResolverRc<TSys>,
sys: TSys,
conditions_from_resolution_mode: ConditionsFromResolutionMode,
}

impl<
Expand All @@ -133,13 +164,15 @@ impl<
npm_pkg_folder_resolver: NpmPackageFolderResolverRc,
pkg_json_resolver: PackageJsonResolverRc<TSys>,
sys: TSys,
conditions_from_resolution_mode: ConditionsFromResolutionMode,
) -> Self {
Self {
in_npm_pkg_checker,
is_built_in_node_module_checker,
npm_pkg_folder_resolver,
pkg_json_resolver,
sys,
conditions_from_resolution_mode,
}
}

Expand Down Expand Up @@ -197,11 +230,14 @@ impl<
}
}

let conditions = self
.conditions_from_resolution_mode
.resolve(resolution_mode);
let url = self.module_resolve(
specifier,
referrer,
resolution_mode,
conditions_from_resolution_mode(resolution_mode),
conditions,
resolution_kind,
)?;

Expand All @@ -211,6 +247,7 @@ impl<
&file_path,
Some(referrer),
resolution_mode,
conditions,
)?
} else {
url
Expand Down Expand Up @@ -343,7 +380,9 @@ impl<
&package_subpath,
maybe_referrer,
resolution_mode,
conditions_from_resolution_mode(resolution_mode),
self
.conditions_from_resolution_mode
.resolve(resolution_mode),
resolution_kind,
)?;
// TODO(bartlomieju): skipped checking errors for commonJS resolution and
Expand Down Expand Up @@ -411,6 +450,7 @@ impl<
path: &Path,
maybe_referrer: Option<&Url>,
resolution_mode: ResolutionMode,
conditions: &[&str],
) -> Result<Url, TypesNotFoundError> {
fn probe_extensions<TSys: FsMetadata>(
sys: &TSys,
Expand Down Expand Up @@ -474,7 +514,7 @@ impl<
/* sub path */ ".",
maybe_referrer,
resolution_mode,
conditions_from_resolution_mode(resolution_mode),
conditions,
NodeResolutionKind::Types,
);
if let Ok(resolution) = resolution_result {
Expand Down Expand Up @@ -855,6 +895,7 @@ impl<
&path,
maybe_referrer,
resolution_mode,
conditions,
)?));
} else {
return Ok(Some(url));
Expand Down Expand Up @@ -1212,6 +1253,7 @@ impl<
package_subpath,
maybe_referrer,
resolution_mode,
conditions,
resolution_kind,
)
.map_err(|err| {
Expand Down Expand Up @@ -1249,6 +1291,7 @@ impl<
package_json,
referrer,
resolution_mode,
conditions,
resolution_kind,
)
.map_err(|err| {
Expand All @@ -1268,6 +1311,7 @@ impl<
package_json,
referrer,
resolution_mode,
conditions,
resolution_kind,
)
.map_err(|err| {
Expand All @@ -1281,6 +1325,7 @@ impl<
package_subpath,
referrer,
resolution_mode,
conditions,
resolution_kind,
)
.map_err(|err| {
Expand All @@ -1294,12 +1339,18 @@ impl<
package_subpath: &str,
referrer: Option<&Url>,
resolution_mode: ResolutionMode,
conditions: &[&str],
resolution_kind: NodeResolutionKind,
) -> Result<Url, TypesNotFoundError> {
assert_ne!(package_subpath, ".");
let file_path = directory.join(package_subpath);
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 {
Ok(url_from_file_path(&file_path).unwrap())
}
Expand All @@ -1311,6 +1362,7 @@ impl<
package_subpath: &str,
maybe_referrer: Option<&Url>,
resolution_mode: ResolutionMode,
conditions: &[&str],
resolution_kind: NodeResolutionKind,
) -> Result<Url, LegacyResolveError> {
if package_subpath == "." {
Expand All @@ -1327,6 +1379,7 @@ impl<
package_subpath,
maybe_referrer,
resolution_mode,
conditions,
resolution_kind,
)
.map_err(|err| err.into())
Expand All @@ -1338,6 +1391,7 @@ impl<
package_json: &PackageJson,
maybe_referrer: Option<&Url>,
resolution_mode: ResolutionMode,
conditions: &[&str],
resolution_kind: NodeResolutionKind,
) -> Result<Url, LegacyResolveError> {
let pkg_json_kind = match resolution_mode {
Expand All @@ -1356,6 +1410,7 @@ impl<
&main,
maybe_referrer,
resolution_mode,
conditions,
);
// don't surface errors, fallback to checking the index now
if let Ok(url) = decl_url_result {
Expand Down

0 comments on commit 4284566

Please sign in to comment.