diff options
author | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2021-06-09 13:51:41 +0300 |
---|---|---|
committer | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2021-06-13 17:22:34 +0300 |
commit | 4e17ffc793a2a484f376f8f696a2ea80eabb437d (patch) | |
tree | eeda649008d3aecf224e455dd099dfb53f6c20fc /lib | |
parent | 5b3587dbc64cc298701b62f8767d37976d57a451 (diff) |
module: fix legacy `node` specifier resolution to resolve `"main"` field
PR-URL: https://github.com/nodejs/node/pull/38979
Fixes: https://github.com/nodejs/node/issues/32103
Fixes: https://github.com/nodejs/node/issues/38739
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/modules/esm/resolve.js | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 7b503376af0..8b6f23bb485 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -34,7 +34,7 @@ const { getOptionValue } = require('internal/options'); const policy = getOptionValue('--experimental-policy') ? require('internal/process/policy') : null; -const { sep, relative } = require('path'); +const { sep, relative, resolve } = require('path'); const preserveSymlinks = getOptionValue('--preserve-symlinks'); const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const typeFlag = getOptionValue('--input-type'); @@ -204,16 +204,18 @@ function getPackageScopeConfig(resolved) { return packageConfig; } -/* +/** * Legacy CommonJS main resolution: * 1. let M = pkg_url + (json main field) * 2. TRY(M, M.js, M.json, M.node) * 3. TRY(M/index.js, M/index.json, M/index.node) * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node) * 5. NOT_FOUND + * @param {string | URL} url + * @returns {boolean} */ function fileExists(url) { - return tryStatSync(fileURLToPath(url)).isFile(); + return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false; } function legacyMainResolve(packageJSONUrl, packageConfig, base) { @@ -272,7 +274,19 @@ function resolveExtensions(search) { return undefined; } -function resolveIndex(search) { +function resolveDirectoryEntry(search) { + const dirPath = fileURLToPath(search); + const pkgJsonPath = resolve(dirPath, 'package.json'); + if (fileExists(pkgJsonPath)) { + const pkgJson = packageJsonReader.read(pkgJsonPath); + if (pkgJson.containsKeys) { + const { main } = JSONParse(pkgJson.string); + if (main != null) { + const mainUrl = pathToFileURL(resolve(dirPath, main)); + return resolveExtensionsWithTryExactName(mainUrl); + } + } + } return resolveExtensions(new URL('index', search)); } @@ -288,10 +302,10 @@ function finalizeResolution(resolved, base) { let file = resolveExtensionsWithTryExactName(resolved); if (file !== undefined) return file; if (!StringPrototypeEndsWith(path, '/')) { - file = resolveIndex(new URL(`${resolved}/`)); + file = resolveDirectoryEntry(new URL(`${resolved}/`)); if (file !== undefined) return file; } else { - return resolveIndex(resolved) || resolved; + return resolveDirectoryEntry(resolved) || resolved; } throw new ERR_MODULE_NOT_FOUND( resolved.pathname, fileURLToPath(base), 'module'); |