This commit is contained in:
Simon Knott 2024-08-09 12:40:48 +02:00
parent 84690a5e94
commit ca1ad4d57e
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -319,20 +319,19 @@ export function resolveImportSpecifierExtension(resolved: string, isPathMapping:
break; // Do not try '' when a more specific extension like '.jsx' matched. break; // Do not try '' when a more specific extension like '.jsx' matched.
} }
if (dirExists(resolved)) { // Following TypeScript's path mapping logic, index files and package.json are not resolved in ESM.
// If we import a package, let Node.js figure out the correct import based on package.json.
// TypeScript does not interpret package.json for path mappings: https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-should-not-point-to-monorepo-packages-or-node_modules-packages // TypeScript does not interpret package.json for path mappings: https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-should-not-point-to-monorepo-packages-or-node_modules-packages
if (!isPathMapping && fileExists(path.join(resolved, 'package.json'))) const shouldNotResolveDirectory = isPathMapping && isESM;
if (!shouldNotResolveDirectory && dirExists(resolved)) {
// If we import a package, let Node.js figure out the correct import based on package.json.
if (fileExists(path.join(resolved, 'package.json')))
return resolved; return resolved;
// Following TypeScript's path mapping logic, index files are still resolved in CommonJS.
const shouldNotResolveIndex = isPathMapping && isESM;
if (!shouldNotResolveIndex) {
const dirImport = path.join(resolved, 'index'); const dirImport = path.join(resolved, 'index');
return resolveImportSpecifierExtension(dirImport, isPathMapping, isESM); return resolveImportSpecifierExtension(dirImport, isPathMapping, isESM);
} }
} }
}
function fileExists(resolved: string) { function fileExists(resolved: string) {
return fs.statSync(resolved, { throwIfNoEntry: false })?.isFile(); return fs.statSync(resolved, { throwIfNoEntry: false })?.isFile();