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

module: prevent format from being set to null internaly #53015

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ register('./path-to-my-hooks.js', {

<!-- YAML
changes:
- version:
- REPLACEME
pr-url: https://github.com/nodejs/node/pull/53015
description: The property `context.format` will no longer return `null`
in place of `undefined`.
- version:
- v21.0.0
- v20.10.0
Expand Down Expand Up @@ -509,7 +514,7 @@ changes:
* `specifier` {string}
* `context` {Object}
* Returns: {Object|Promise}
* `format` {string|null|undefined} A hint to the load hook (it might be
* `format` {string|undefined} A hint to the load hook (it might be
RedYetiDev marked this conversation as resolved.
Show resolved Hide resolved
ignored)
`'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'`
* `importAttributes` {Object|undefined} The import attributes to use when
Expand Down Expand Up @@ -579,6 +584,11 @@ export async function resolve(specifier, context, nextResolve) {

<!-- YAML
changes:
- version:
- REPLACEME
pr-url: https://github.com/nodejs/node/pull/53015
description: The property `context.format` will no longer return `null`
in place of `undefined`.
- version: v20.6.0
pr-url: https://github.com/nodejs/node/pull/47999
description: Add support for `source` with format `commonjs`.
Expand All @@ -596,7 +606,7 @@ changes:
* `url` {string} The URL returned by the `resolve` chain
* `context` {Object}
* `conditions` {string\[]} Export conditions of the relevant `package.json`
* `format` {string|null|undefined} The format optionally supplied by the
* `format` {string|undefined} The format optionally supplied by the
RedYetiDev marked this conversation as resolved.
Show resolved Hide resolved
`resolve` hook chain
* `importAttributes` {Object}
* `nextLoad` {Function} The subsequent `load` hook in the chain, or the
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if (experimentalWasmModules) {

/**
* @param {string} mime
* @returns {string | null}
* @returns {string | undefined}
*/
function mimeToFormat(mime) {
if (
Expand All @@ -37,7 +37,7 @@ function mimeToFormat(mime) {
) { return 'module'; }
if (mime === 'application/json') { return 'json'; }
if (experimentalWasmModules && mime === 'application/wasm') { return 'wasm'; }
return null;
return undefined;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const protocolHandlers = {

/**
* @param {URL} parsed
* @returns {string | null}
* @returns {string | undefined}
*/
function getDataProtocolModuleFormat(parsed) {
const { 1: mime } = RegExpPrototypeExec(
Expand Down Expand Up @@ -115,7 +115,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
if (getOptionValue('--experimental-detect-module')) {
const format = source ?
(containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs') :
null;
undefined;
if (format === 'module') {
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
Expand Down Expand Up @@ -155,7 +155,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
}
default: { // The user did not pass `--experimental-default-type`.
if (getOptionValue('--experimental-detect-module')) {
if (!source) { return null; }
if (!source) { return; }
const format = getFormatOfExtensionlessFile(url);
if (format === 'module') {
return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';
Expand Down Expand Up @@ -201,7 +201,7 @@ function getHttpProtocolModuleFormat(url, context) {
function defaultGetFormatWithoutErrors(url, context) {
const protocol = url.protocol;
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
return null;
return;
}
return protocolHandlers[protocol](url, context, true);
}
Expand All @@ -214,7 +214,7 @@ function defaultGetFormatWithoutErrors(url, context) {
function defaultGetFormat(url, context) {
const protocol = url.protocol;
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
return null;
return;
}
return protocolHandlers[protocol](url, context, false);
}
Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('--experimental-detect-module', { concurrency: !process.env.TEST_PARALL
]);

strictEqual(stderr, '');
strictEqual(stdout, 'null\nexecuted\n');
strictEqual(stdout, 'undefined\nexecuted\n');
strictEqual(code, 0);
strictEqual(signal, null);

Expand Down