Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop import of extensionless #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 16 additions & 13 deletions doc/api/esm.md
Expand Up @@ -33,9 +33,9 @@ initial input, or when referenced by `import` statements within ES module code:

* Files ending in `.mjs`.

* Files ending in `.js`, or extensionless files, when the nearest parent
`package.json` file contains a top-level field `"type"` with a value of
`"module"`.
* Files ending in `.js`, or extensionless files when run as main entry points on
the command line, when the nearest parent `package.json` file contains a
top-level field `"type"` with a value of `"module"`.

* Strings passed in as an argument to `--eval` or `--print`, or piped to
`node` via `STDIN`, with the flag `--input-type=module`.
Expand All @@ -50,9 +50,9 @@ or when referenced by `import` statements within ES module code:

* Files ending in `.cjs`.

* Files ending in `.js`, or extensionless files, when the nearest parent
`package.json` file contains a top-level field `"type"` with a value of
`"commonjs"`.
* Files ending in `.js`, or extensionless files when run as main entry points on
the command line, when the nearest parent `package.json` file contains a
top-level field `"type"` with a value of `"commonjs"`.

* Strings passed in as an argument to `--eval` or `--print`, or piped to
`node` via `STDIN`, with the flag `--input-type=commonjs`.
Expand Down Expand Up @@ -1042,7 +1042,8 @@ a URL should be interpreted. This can be one of the following:
```js
/**
* @param {string} url
* @param {object} context (currently empty)
* @param {object} context
* @param {string} context.parentURL
* @param {function} defaultGetFormat
* @returns {object} response
* @returns {string} response.format
Expand Down Expand Up @@ -1366,13 +1367,15 @@ updates.
In the following algorithms, all subroutine errors are propagated as errors
of these top-level routines unless stated otherwise.

_isMain_ is **true** when resolving the Node.js application entry point.

_defaultEnv_ is the conditional environment name priority array,
`["node", "import"]`.

<details>
<summary>Resolver algorithm specification</summary>

**ESM_RESOLVE**(_specifier_, _parentURL_)
**ESM_RESOLVE**(_specifier_, _parentURL_, _isMain_)

> 1. Let _resolvedURL_ be **undefined**.
> 1. If _specifier_ is a valid URL, then
Expand All @@ -1393,7 +1396,7 @@ _defaultEnv_ is the conditional environment name priority array,
> 1. If the file at _resolvedURL_ does not exist, then
> 1. Throw a _Module Not Found_ error.
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_, _isMain_).
> 1. Load _resolvedURL_ as module format, _format_.

**PACKAGE_RESOLVE**(_packageSpecifier_, _parentURL_)
Expand Down Expand Up @@ -1546,20 +1549,20 @@ _defaultEnv_ is the conditional environment name priority array,
> 1. Return _resolved_.
> 1. Throw a _Module Not Found_ error.

**ESM_FORMAT**(_url_)
**ESM_FORMAT**(_url_, _isMain_)

> 1. Assert: _url_ corresponds to an existing file pathname.
> 1. Assert: _url_ corresponds to an existing file.
> 1. Let _pjson_ be the result of **READ_PACKAGE_SCOPE**(_url_).
> 1. If _url_ ends in _".mjs"_, then
> 1. Return _"module"_.
> 1. If _url_ ends in _".cjs"_, then
> 1. Return _"commonjs"_.
> 1. If _pjson?.type_ exists and is _"module"_, then
> 1. If _url_ ends in _".js"_ or lacks a file extension, then
> 1. If _isMain_ is **true** or _url_ ends in _".js"_, then
> 1. Return _"module"_.
> 1. Throw an _Unsupported File Extension_ error.
> 1. Otherwise,
> 1. If _url_ lacks a file extension, then
> 1. If _isMain_ is **true**, then
> 1. Return _"commonjs"_.
> 1. Throw an _Unsupported File Extension_ error.

Expand Down
8 changes: 3 additions & 5 deletions lib/internal/modules/esm/get_format.js
Expand Up @@ -54,13 +54,11 @@ function defaultGetFormat(url, context, defaultGetFormat) {
return { format };
} else if (parsed.protocol === 'file:') {
const ext = extname(parsed.pathname);
let format;
if (ext === '.js' || ext === '') {
let format = extensionFormatMap[ext];
const isMain = context.parentURL === undefined;
if (ext === '.js' || (!format && isMain))
format = getPackageType(parsed.href) === TYPE_MODULE ?
'module' : 'commonjs';
} else {
format = extensionFormatMap[ext];
}
if (!format) {
if (experimentalSpeciferResolution === 'node') {
process.emitWarning(
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/loader.js
Expand Up @@ -96,7 +96,7 @@ class Loader {
}

const getFormatResponse = await this._getFormat(
url, {}, defaultGetFormat);
url, { parentURL }, defaultGetFormat);
if (typeof getFormatResponse !== 'object') {
throw new ERR_INVALID_RETURN_VALUE(
'object', 'loader getFormat', getFormatResponse);
Expand Down
81 changes: 0 additions & 81 deletions test/es-module/test-esm-unknown-main.js

This file was deleted.

This file was deleted.

This file was deleted.