Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine du Hamel <duhamelantoine1995@gmail.com>2021-06-09 13:51:41 +0300
committerRichard Lau <rlau@redhat.com>2021-07-29 01:25:49 +0300
commit3e4bc1b0d351cb12d6c355e9ae9df40316f03199 (patch)
treeff1ee0424c9980d7c90a29c42bbbaaf4821d4076
parentddc8dde1508d035df4928f6bfe099100271c5517 (diff)
module: fix legacy `node` specifier resolution to resolve `"main"` field
PR-URL: https://github.com/nodejs/node/pull/38979 Backport-PR-URL: https://github.com/nodejs/node/pull/39497 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>
-rw-r--r--lib/internal/modules/esm/resolve.js26
-rw-r--r--test/es-module/test-esm-specifiers-legacy-flag.mjs3
-rw-r--r--test/fixtures/es-module-specifiers/dir-with-main/main.js1
-rw-r--r--test/fixtures/es-module-specifiers/dir-with-main/package.json3
4 files changed, 27 insertions, 6 deletions
diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js
index 4576cfebf4d..bd07bc5b236 100644
--- a/lib/internal/modules/esm/resolve.js
+++ b/lib/internal/modules/esm/resolve.js
@@ -30,7 +30,7 @@ const {
Stats,
} = require('fs');
const { getOptionValue } = require('internal/options');
-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');
@@ -160,16 +160,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 tryStatSync(url).isFile();
}
function legacyMainResolve(packageJSONUrl, packageConfig, base) {
@@ -236,7 +238,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));
}
@@ -252,10 +266,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');
diff --git a/test/es-module/test-esm-specifiers-legacy-flag.mjs b/test/es-module/test-esm-specifiers-legacy-flag.mjs
index fcf0c915b64..5351846c6a8 100644
--- a/test/es-module/test-esm-specifiers-legacy-flag.mjs
+++ b/test/es-module/test-esm-specifiers-legacy-flag.mjs
@@ -6,12 +6,15 @@ import assert from 'assert';
import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs';
// esm index.js
import module from '../fixtures/es-module-specifiers/package-type-module';
+// Directory entry with main.js
+import main from '../fixtures/es-module-specifiers/dir-with-main';
// Notice the trailing slash
import success, { explicit, implicit, implicitModule }
from '../fixtures/es-module-specifiers/';
assert.strictEqual(commonjs, 'commonjs');
assert.strictEqual(module, 'module');
+assert.strictEqual(main, 'main');
assert.strictEqual(success, 'success');
assert.strictEqual(explicit, 'esm');
assert.strictEqual(implicit, 'cjs');
diff --git a/test/fixtures/es-module-specifiers/dir-with-main/main.js b/test/fixtures/es-module-specifiers/dir-with-main/main.js
new file mode 100644
index 00000000000..dfdd47b8773
--- /dev/null
+++ b/test/fixtures/es-module-specifiers/dir-with-main/main.js
@@ -0,0 +1 @@
+module.exports = 'main';
diff --git a/test/fixtures/es-module-specifiers/dir-with-main/package.json b/test/fixtures/es-module-specifiers/dir-with-main/package.json
new file mode 100644
index 00000000000..2a4fe363081
--- /dev/null
+++ b/test/fixtures/es-module-specifiers/dir-with-main/package.json
@@ -0,0 +1,3 @@
+{
+ "main": "./main.js"
+}