diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-11-13 10:10:09 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-13 15:10:09 +0000 |
commit | f091d1ad69b4e5217ae3272b641171781a372c4f (patch) | |
tree | 4ef4f90ec8a6b5c977efb187449f8c59c45de5e1 /resolvers/node | |
parent | 6a4c6d83bacf5f03628a494778a30bce970f7cbc (diff) |
feat(node): stabilize detecting if CJS via `"type": "commonjs"` in a package.json (#26439)
This will respect `"type": "commonjs"` in a package.json to determine if
`.js`/`.jsx`/`.ts`/.tsx` files are CJS or ESM. If the file is found to
be ESM it will be loaded as ESM though.
Diffstat (limited to 'resolvers/node')
-rw-r--r-- | resolvers/node/package_json.rs | 4 | ||||
-rw-r--r-- | resolvers/node/resolution.rs | 23 |
2 files changed, 16 insertions, 11 deletions
diff --git a/resolvers/node/package_json.rs b/resolvers/node/package_json.rs index 6967779e5..ae016ebe3 100644 --- a/resolvers/node/package_json.rs +++ b/resolvers/node/package_json.rs @@ -15,8 +15,8 @@ use crate::errors::CanonicalizingPkgJsonDirError; use crate::errors::ClosestPkgJsonError; use crate::errors::PackageJsonLoadError; -// todo(dsherret): this isn't exactly correct and we should change it to instead -// be created per worker and passed down as a ctor arg to the pkg json resolver +// it would be nice if this was passed down as a ctor arg to the package.json resolver, +// but it's a little bit complicated to do that, so we just maintain a thread local cache thread_local! { static CACHE: RefCell<HashMap<PathBuf, PackageJsonRc>> = RefCell::new(HashMap::new()); } diff --git a/resolvers/node/resolution.rs b/resolvers/node/resolution.rs index d44539e97..fcff29242 100644 --- a/resolvers/node/resolution.rs +++ b/resolvers/node/resolution.rs @@ -50,6 +50,15 @@ pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"]; pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"]; static TYPES_ONLY_CONDITIONS: &[&str] = &["types"]; +fn conditions_from_module_kind( + kind: NodeModuleKind, +) -> &'static [&'static str] { + match kind { + NodeModuleKind::Esm => DEFAULT_CONDITIONS, + NodeModuleKind::Cjs => REQUIRE_CONDITIONS, + } +} + pub type NodeModuleKind = deno_package_json::NodeModuleKind; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -166,8 +175,7 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> { specifier, referrer, referrer_kind, - // even though the referrer may be CJS, if we're here that means we're doing ESM resolution - DEFAULT_CONDITIONS, + conditions_from_module_kind(referrer_kind), mode, )?; @@ -299,9 +307,9 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> { package_dir: &Path, package_subpath: Option<&str>, maybe_referrer: Option<&Url>, + referrer_kind: NodeModuleKind, mode: NodeResolutionMode, ) -> Result<Url, PackageSubpathResolveError> { - let node_module_kind = NodeModuleKind::Esm; let package_subpath = package_subpath .map(|s| format!("./{s}")) .unwrap_or_else(|| ".".to_string()); @@ -309,8 +317,8 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> { package_dir, &package_subpath, maybe_referrer, - node_module_kind, - DEFAULT_CONDITIONS, + referrer_kind, + conditions_from_module_kind(referrer_kind), mode, )?; // TODO(bartlomieju): skipped checking errors for commonJS resolution and @@ -441,10 +449,7 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> { /* sub path */ ".", maybe_referrer, referrer_kind, - match referrer_kind { - NodeModuleKind::Esm => DEFAULT_CONDITIONS, - NodeModuleKind::Cjs => REQUIRE_CONDITIONS, - }, + conditions_from_module_kind(referrer_kind), NodeResolutionMode::Types, ); if let Ok(resolution) = resolution_result { |