Repository: denoland/deno
File: resolvers/node/resolution.rs (Rust)

Code before:
use crate::path::UrlOrPath;
use crate::path::UrlOrPathRef;
use crate::InNpmPackageChecker;
use crate::IsBuiltInNodeModuleChecker;
use crate::NpmPackageFolderResolver;
use crate::PackageJsonResolverRc;
use crate::PathClean;

pub static DEFAULT_CONDITIONS: &[Cow<'static, str>] = &[
  Cow::Borrowed("deno"),
  Cow::Borrowed("node"),
  Cow::Borrowed("import"),
];
pub static REQUIRE_CONDITIONS: &[Cow<'static, str>] =
  &[Cow::Borrowed("require"), Cow::Borrowed("node")];
static TYPES_ONLY_CONDITIONS: &[Cow<'static, str>] = &[Cow::Borrowed("types")];

#[allow(clippy::disallowed_types)]
type ConditionsFromResolutionModeFn = crate::sync::MaybeArc<
  dyn Fn(ResolutionMode) -> &'static [Cow<'static, str>] + Send + Sync,
>;

#[derive(Clone)]
pub enum ConditionsResolver {
  FromResolutionModeFn(ConditionsFromResolutionModeFn),
  List(Vec<Cow<'static, str>>),
}

impl Default for ConditionsResolver {
  fn default() -> Self {
    Self::List(DEFAULT_CONDITIONS.to_vec())
  }
}

impl ConditionsResolver {
  pub fn new(configured_conditions: &[String]) -> Self {
    Self::List(
      configured_conditions
        .iter()
        .cloned()
        .map(|c| Cow::Owned(c))
        .chain(DEFAULT_CONDITIONS.iter().cloned())
        .collect(),
    )
  }

  fn resolve(&self, resolution_mode: ResolutionMode) -> &[Cow<'static, str>] {
    match &self {
      ConditionsResolver::FromResolutionModeFn(func) => func(resolution_mode),
      ConditionsResolver::List(list) => list,
    }

Review comment (suggestion):
I feel like this still needs to take into account the resolution mode? We should probably still just have one variant here but make it work off non-`'static` references.

![image](https://github.com/user-attachments/assets/010eb57c-ccb9-42f7-aaeb-26cbe94e9d73)


Code after:
use crate::path::UrlOrPath;
use crate::path::UrlOrPathRef;
use crate::InNpmPackageChecker;
use crate::IsBuiltInNodeModuleChecker;
use crate::NpmPackageFolderResolver;
use crate::PackageJsonResolverRc;
use crate::PathClean;

pub static DEFAULT_CONDITIONS: &[Cow<'static, str>] = &[
  Cow::Borrowed("deno"),
  Cow::Borrowed("node"),
  Cow::Borrowed("import"),
];
pub static REQUIRE_CONDITIONS: &[Cow<'static, str>] =
  &[Cow::Borrowed("require"), Cow::Borrowed("node")];
static TYPES_ONLY_CONDITIONS: &[Cow<'static, str>] = &[Cow::Borrowed("types")];

#[derive(Debug, Clone)]
pub struct ConditionResolverOptions {
  pub conditions: Vec<Cow<'static, str>>,
  pub default_import_conditions: Vec<Cow<'static, str>>,
  pub default_require_conditions: Vec<Cow<'static, str>>,
}

impl Default for ConditionResolverOptions {
  fn default() -> Self {
    Self {
      conditions: Vec::new(),
      default_import_conditions: DEFAULT_CONDITIONS.to_vec(),
      default_require_conditions: REQUIRE_CONDITIONS.to_vec(),
    }
  }
}

#[derive(Debug, Clone)]
pub struct ConditionResolver {
  import_conditions: Vec<Cow<'static, str>>,
  require_conditions: Vec<Cow<'static, str>>,
}

impl Default for ConditionResolver {
  fn default() -> Self {
    Self::new(Default::default())
  }
}

impl ConditionResolver {
  pub fn new(options: ConditionResolverOptions) -> Self {
    if options.conditions.is_empty() {
      // Fast path for no custom conditions.
      return Self {