Skip to content

Commit 04d07ed

Browse files
committed
module: revert #31021
reverses baa3621 PR-URL: #31415 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent dc90f92 commit 04d07ed

File tree

6 files changed

+14
-96
lines changed

6 files changed

+14
-96
lines changed

doc/api/esm.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,8 @@ a URL should be interpreted. This can be one of the following:
10421042
```js
10431043
/**
10441044
* @param {string} url
1045-
* @param {object} context (currently empty)
1045+
* @param {object} context
1046+
* @param {string} context.parentURL
10461047
* @param {function} defaultGetFormat
10471048
* @returns {object} response
10481049
* @returns {string} response.format
@@ -1366,13 +1367,15 @@ updates.
13661367
In the following algorithms, all subroutine errors are propagated as errors
13671368
of these top-level routines unless stated otherwise.
13681369
1370+
_isMain_ is **true** when resolving the Node.js application entry point.
1371+
13691372
_defaultEnv_ is the conditional environment name priority array,
13701373
`["node", "import"]`.
13711374
13721375
<details>
13731376
<summary>Resolver algorithm specification</summary>
13741377
1375-
**ESM_RESOLVE**(_specifier_, _parentURL_)
1378+
**ESM_RESOLVE**(_specifier_, _parentURL_, _isMain_)
13761379
13771380
> 1. Let _resolvedURL_ be **undefined**.
13781381
> 1. If _specifier_ is a valid URL, then
@@ -1393,7 +1396,7 @@ _defaultEnv_ is the conditional environment name priority array,
13931396
> 1. If the file at _resolvedURL_ does not exist, then
13941397
> 1. Throw a _Module Not Found_ error.
13951398
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
1396-
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
1399+
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_, _isMain_).
13971400
> 1. Load _resolvedURL_ as module format, _format_.
13981401
13991402
**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
@@ -1546,20 +1549,20 @@ _defaultEnv_ is the conditional environment name priority array,
15461549
> 1. Return _resolved_.
15471550
> 1. Throw a _Module Not Found_ error.
15481551
1549-
**ESM_FORMAT**(_url_)
1552+
**ESM_FORMAT**(_url_, _isMain_)
15501553
1551-
> 1. Assert: _url_ corresponds to an existing file pathname.
1554+
> 1. Assert: _url_ corresponds to an existing file.
15521555
> 1. Let _pjson_ be the result of **READ_PACKAGE_SCOPE**(_url_).
15531556
> 1. If _url_ ends in _".mjs"_, then
15541557
> 1. Return _"module"_.
15551558
> 1. If _url_ ends in _".cjs"_, then
15561559
> 1. Return _"commonjs"_.
15571560
> 1. If _pjson?.type_ exists and is _"module"_, then
1558-
> 1. If _url_ ends in _".js"_ or lacks a file extension, then
1561+
> 1. If _isMain_ is **true** or _url_ ends in _".js"_, then
15591562
> 1. Return _"module"_.
15601563
> 1. Throw an _Unsupported File Extension_ error.
15611564
> 1. Otherwise,
1562-
> 1. If _url_ lacks a file extension, then
1565+
> 1. If _isMain_ is **true**, then
15631566
> 1. Return _"commonjs"_.
15641567
> 1. Throw an _Unsupported File Extension_ error.
15651568

lib/internal/modules/esm/get_format.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ function defaultGetFormat(url, context, defaultGetFormat) {
5454
return { format };
5555
} else if (parsed.protocol === 'file:') {
5656
const ext = extname(parsed.pathname);
57-
let format;
58-
if (ext === '.js' || ext === '') {
57+
let format = extensionFormatMap[ext];
58+
const isMain = context.parentURL === undefined;
59+
if (ext === '.js' || (!format && isMain))
5960
format = getPackageType(parsed.href) === TYPE_MODULE ?
6061
'module' : 'commonjs';
61-
} else {
62-
format = extensionFormatMap[ext];
63-
}
6462
if (!format) {
6563
if (experimentalSpeciferResolution === 'node') {
6664
process.emitWarning(

lib/internal/modules/esm/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class Loader {
9696
}
9797

9898
const getFormatResponse = await this._getFormat(
99-
url, {}, defaultGetFormat);
99+
url, { parentURL }, defaultGetFormat);
100100
if (typeof getFormatResponse !== 'object') {
101101
throw new ERR_INVALID_RETURN_VALUE(
102102
'object', 'loader getFormat', getFormatResponse);

test/es-module/test-esm-unknown-main.js

-81
This file was deleted.

test/fixtures/es-modules/package-type-module/imports-noext.mjs

-1
This file was deleted.

test/fixtures/es-modules/package-type-module/imports-unknownext.mjs

-1
This file was deleted.

0 commit comments

Comments
 (0)