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

Code before:
          resolution_mode,
          conditions,
          resolution_kind,
        )
        .map_err(|err| err.into())
    }
  }

  pub(crate) fn legacy_fallback_resolve<'a>(
    &self,
    package_json: &'a PackageJson,
    node_module_kind: NodeModuleKind,
  ) -> Option<&'a str> {
    if self.resolution_config.bundle_mode {
      let maybe_browser = if self.resolution_config.prefer_browser_field {
        package_json.browser.as_deref()
      } else {
        None
      };
      maybe_browser
        .or(package_json.module.as_deref())
        .or(package_json.main.as_deref())
    } else {
      match node_module_kind {
        NodeModuleKind::Esm => package_json
          .module
          .as_deref()
          .or(package_json.main.as_deref()),
        NodeModuleKind::Cjs => package_json.main.as_deref(),
      }
    }
  }

  fn legacy_main_resolve(
    &self,
    package_json: &PackageJson,
    maybe_referrer: Option<&UrlOrPathRef>,
    resolution_mode: ResolutionMode,
    conditions: &[Cow<'static, str>],
    resolution_kind: NodeResolutionKind,
  ) -> Result<MaybeTypesResolvedUrl, LegacyResolveError> {
    let pkg_json_kind = match resolution_mode {
      ResolutionMode::Require => NodeModuleKind::Cjs,
      ResolutionMode::Import => NodeModuleKind::Esm,
    };

    let maybe_main = if resolution_kind.is_types() {
      match package_json.types.as_ref() {
        Some(types) => {
          let types_versions =
            self.pkg_json_types_versions(package_json, resolution_kind);

Review comment (suggestion):
I don't think we should be using `module` here as Node.js doesn't respect this. I wonder how many packages acctually hit this in the past as it seems we only used to do this when the `type` was "module"? I think we should just have this use `package_json.main.as_deref()` here. If anything, probably better to revert this as a fix and then see what breaks.

Code after:
    }
  }

  pub(crate) fn legacy_fallback_resolve<'a>(
    &self,
    package_json: &'a PackageJson,
  ) -> Option<&'a str> {
    fn filter_empty(value: Option<&str>) -> Option<&str> {
      value.map(|v| v.trim()).filter(|v| !v.is_empty())
    }
    if self.resolution_config.bundle_mode {
      let maybe_browser = if self.resolution_config.prefer_browser_field {
        filter_empty(package_json.browser.as_deref())
      } else {
        None
      };
      maybe_browser
        .or(filter_empty(package_json.module.as_deref()))
        .or(filter_empty(package_json.main.as_deref()))
    } else {
      filter_empty(package_json.main.as_deref())
    }
  }

  fn legacy_main_resolve(
    &self,
    package_json: &PackageJson,
    maybe_referrer: Option<&UrlOrPathRef>,
    resolution_mode: ResolutionMode,
    conditions: &[Cow<'static, str>],
    resolution_kind: NodeResolutionKind,
  ) -> Result<MaybeTypesResolvedUrl, LegacyResolveError> {
    let maybe_main = if resolution_kind.is_types() {
      match package_json.types.as_ref() {
        Some(types) => {
          let types_versions =
            self.pkg_json_types_versions(package_json, resolution_kind);
          Some(
            types_versions
              .and_then(|v| v.map(types.as_ref()))
              .unwrap_or(Cow::Borrowed(types.as_str())),
          )
        }
        None => {
          // fallback to checking the main entrypoint for
          // a corresponding declaration file
          if let Some(main) = self.legacy_fallback_resolve(package_json) {
            let main = package_json.path.parent().unwrap().join(main).clean();
            let decl_path_result = self.path_to_declaration_path(
              LocalPath {
                path: main,