Skip to content

Commit 63428bb

Browse files
committed
loader: remove alternative format mapping
1 parent 5c3addd commit 63428bb

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

lib/internal/modules/esm/get_format.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,6 @@ const protocolHandlers = {
7878
'node:'() { return 'builtin'; },
7979
};
8080

81-
function getFormatFromImportAttributes(importAttributes) {
82-
if (
83-
!importAttributes ||
84-
!ObjectPrototypeHasOwnProperty(importAttributes, 'type') ||
85-
typeof importAttributes.type !== 'string'
86-
) {
87-
return undefined;
88-
}
89-
90-
if (
91-
getOptionValue('--experimental-import-text') &&
92-
importAttributes.type === 'text'
93-
) {
94-
return 'text';
95-
}
96-
97-
return undefined;
98-
}
99-
10081
/**
10182
* Determine whether the given ambiguous source contains CommonJS or ES module syntax.
10283
* @param {string | Buffer | undefined} [source]
@@ -261,11 +242,6 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
261242
* @returns {Promise<string> | string | undefined} only works when enabled
262243
*/
263244
function defaultGetFormatWithoutErrors(url, context) {
264-
const format = getFormatFromImportAttributes(context?.importAttributes);
265-
if (format !== undefined) {
266-
return format;
267-
}
268-
269245
const protocol = url.protocol;
270246
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
271247
return null;
@@ -279,11 +255,6 @@ function defaultGetFormatWithoutErrors(url, context) {
279255
* @returns {Promise<string> | string | undefined} only works when enabled
280256
*/
281257
function defaultGetFormat(url, context) {
282-
const format = getFormatFromImportAttributes(context?.importAttributes);
283-
if (format !== undefined) {
284-
return format;
285-
}
286-
287258
const protocol = url.protocol;
288259
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
289260
return null;

lib/internal/modules/esm/load.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
const {
77
kEmptyObject,
88
} = require('internal/util');
9+
const { getOptionValue } = require('internal/options');
910

1011
const { defaultGetFormat } = require('internal/modules/esm/get_format');
1112
const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert');
@@ -48,6 +49,10 @@ function getSourceSync(url, context) {
4849
return { __proto__: null, responseURL, source };
4950
}
5051

52+
function isExperimentalTextImport(importAttributes) {
53+
return getOptionValue('--experimental-import-text') &&
54+
importAttributes?.type === 'text';
55+
}
5156

5257
/**
5358
* Node.js default load hook.
@@ -90,8 +95,12 @@ function defaultLoad(url, context = kEmptyObject) {
9095
}
9196

9297
if (format == null) {
98+
if (isExperimentalTextImport(importAttributes)) {
99+
format = 'text';
100+
}
101+
93102
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
94-
format = defaultGetFormat(urlInstance, {
103+
format ??= defaultGetFormat(urlInstance, {
95104
__proto__: context,
96105
importAttributes,
97106
});
@@ -157,6 +166,10 @@ function defaultLoadSync(url, context = kEmptyObject) {
157166
context = { __proto__: context, source };
158167
}
159168

169+
if (format == null && isExperimentalTextImport(importAttributes)) {
170+
format = 'text';
171+
}
172+
160173
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
161174
format ??= defaultGetFormat(urlInstance, {
162175
__proto__: context,

lib/internal/modules/esm/resolve.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,13 @@ function throwIfInvalidParentURL(parentURL) {
937937
* @param {string} specifier - The specifier to resolve.
938938
* @param {object} [context] - The context object containing the parent URL and conditions.
939939
* @param {string} [context.parentURL] - The URL of the parent module.
940+
* @param {object} [context.importAttributes] - The import attributes for resolving the specifier.
940941
* @param {string[]} [context.conditions] - The conditions for resolving the specifier.
941942
* @returns {{url: string, format?: string}}
942943
*/
943944
function defaultResolve(specifier, context = {}) {
944945
let { parentURL, conditions } = context;
946+
const { importAttributes } = context;
945947
throwIfInvalidParentURL(parentURL);
946948

947949
let parsedParentURL;
@@ -1004,12 +1006,18 @@ function defaultResolve(specifier, context = {}) {
10041006
throw error;
10051007
}
10061008

1009+
let format = defaultGetFormatWithoutErrors(url, context);
1010+
if (getOptionValue('--experimental-import-text') &&
1011+
importAttributes?.type === 'text') {
1012+
format = 'text';
1013+
}
1014+
10071015
return {
10081016
__proto__: null,
10091017
// Do NOT cast `url` to a string: that will work even when there are real
10101018
// problems, silencing them
10111019
url: url.href,
1012-
format: defaultGetFormatWithoutErrors(url, context),
1020+
format,
10131021
};
10141022
}
10151023

0 commit comments

Comments
 (0)