diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-07-28 11:24:22 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-28 11:24:22 -0400 |
commit | 279030f2b8564ceed76c0ed08b14ea97e2258215 (patch) | |
tree | 865327971df6007b3fdf99fd0135c675e8aa0b13 /ext/node | |
parent | bddf5acf895ed764074a1bb2a8ab124dd70fd313 (diff) |
fix(npm): improve declaration resolution for filename with different extensions (#19966)
postcss was importing `./index.js` from `./index.d.mts` where there also
existed a `./index.d.ts`.
Closes #19575
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/resolution.rs | 58 |
1 files changed, 44 insertions, 14 deletions
diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index ce02f31f2..bb25124bf 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -527,37 +527,67 @@ impl NodeResolver { fn probe_extensions( fs: &dyn deno_fs::FileSystem, path: &Path, + lowercase_path: &str, referrer_kind: NodeModuleKind, ) -> Option<PathBuf> { - let specific_dts_path = match referrer_kind { - NodeModuleKind::Cjs => with_known_extension(path, "d.cts"), - NodeModuleKind::Esm => with_known_extension(path, "d.mts"), - }; - if fs.exists(&specific_dts_path) { - return Some(specific_dts_path); + let mut searched_for_d_mts = false; + let mut searched_for_d_cts = false; + if lowercase_path.ends_with(".mjs") { + let d_mts_path = with_known_extension(path, "d.mts"); + if fs.exists(&d_mts_path) { + return Some(d_mts_path); + } + searched_for_d_mts = true; + } else if lowercase_path.ends_with(".cjs") { + let d_cts_path = with_known_extension(path, "d.cts"); + if fs.exists(&d_cts_path) { + return Some(d_cts_path); + } + searched_for_d_cts = true; } + let dts_path = with_known_extension(path, "d.ts"); if fs.exists(&dts_path) { - Some(dts_path) - } else { - None + return Some(dts_path); + } + + let specific_dts_path = match referrer_kind { + NodeModuleKind::Cjs if !searched_for_d_cts => { + Some(with_known_extension(path, "d.cts")) + } + NodeModuleKind::Esm if !searched_for_d_mts => { + Some(with_known_extension(path, "d.mts")) + } + _ => None, // already searched above + }; + if let Some(specific_dts_path) = specific_dts_path { + if fs.exists(&specific_dts_path) { + return Some(specific_dts_path); + } } + None } let lowercase_path = path.to_string_lossy().to_lowercase(); if lowercase_path.ends_with(".d.ts") || lowercase_path.ends_with(".d.cts") - || lowercase_path.ends_with(".d.ts") + || lowercase_path.ends_with(".d.mts") { return Some(path); } - if let Some(path) = probe_extensions(&*self.fs, &path, referrer_kind) { + if let Some(path) = + probe_extensions(&*self.fs, &path, &lowercase_path, referrer_kind) + { return Some(path); } if self.fs.is_dir(&path) { - if let Some(path) = - probe_extensions(&*self.fs, &path.join("index"), referrer_kind) - { + let index_path = path.join("index.js"); + if let Some(path) = probe_extensions( + &*self.fs, + &index_path, + &index_path.to_string_lossy().to_lowercase(), + referrer_kind, + ) { return Some(path); } } |