Skip to content

Commit f8de5d5

Browse files
loader: implementation cleanup
Co-authored-by: GeoffreyBooth <GeoffreyBooth@users.noreply.github.com>
1 parent 4a5953c commit f8de5d5

File tree

4 files changed

+25
-34
lines changed

4 files changed

+25
-34
lines changed

lib/internal/modules/esm/assert.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,13 @@ const formatTypeMap = {
3030
'commonjs': kImplicitTypeAttribute,
3131
'json': 'json',
3232
'module': kImplicitTypeAttribute,
33+
'text': 'text',
3334
'wasm': kImplicitTypeAttribute, // It's unclear whether the HTML spec will require an type attribute or not for Wasm; see https://github.com/WebAssembly/esm-integration/issues/42
3435
};
3536
// NOTE: Don't add bytes support yet as it requires Uint8Arrays backed by immutable ArrayBuffers,
3637
// which V8 does not support yet.
3738
// see: https://github.com/nodejs/node/pull/62300#issuecomment-4079163816
3839

39-
function getFormatType(format) {
40-
if (format === 'text' && getOptionValue('--experimental-import-text')) {
41-
return 'text';
42-
}
43-
44-
return formatTypeMap[format];
45-
}
4640

4741
/**
4842
* The HTML spec disallows the default type to be explicitly specified
@@ -71,7 +65,13 @@ function validateAttributes(url, format,
7165
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED(keys[i], importAttributes[keys[i]], url);
7266
}
7367
}
74-
const validType = getFormatType(format);
68+
const validType = formatTypeMap[format];
69+
70+
if (validType !== undefined &&
71+
importAttributes.type === 'text' &&
72+
!getOptionValue('--experimental-import-text')) {
73+
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', importAttributes.type, url);
74+
}
7575

7676
switch (validType) {
7777
case undefined:
@@ -112,8 +112,7 @@ function handleInvalidType(url, type) {
112112
validateString(type, 'type');
113113

114114
// `type` might not have been one of the types we understand.
115-
if (!ArrayPrototypeIncludes(supportedTypeAttributes, type) &&
116-
!(type === 'text' && getOptionValue('--experimental-import-text'))) {
115+
if (!ArrayPrototypeIncludes(supportedTypeAttributes, type)) {
117116
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type, url);
118117
}
119118

lib/internal/modules/esm/get_format.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,21 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
236236
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath);
237237
}
238238

239+
function isExperimentalTextImport(importAttributes) {
240+
return getOptionValue('--experimental-import-text') &&
241+
importAttributes?.type === 'text';
242+
}
243+
239244
/**
240245
* @param {URL} url
241-
* @param {{parentURL: string}} context
246+
* @param {{parentURL: string, importAttributes?: string}} context
242247
* @returns {Promise<string> | string | undefined} only works when enabled
243248
*/
244249
function defaultGetFormatWithoutErrors(url, context) {
250+
if (isExperimentalTextImport(context?.importAttributes)) {
251+
return 'text';
252+
}
253+
245254
const protocol = url.protocol;
246255
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
247256
return null;
@@ -255,6 +264,10 @@ function defaultGetFormatWithoutErrors(url, context) {
255264
* @returns {Promise<string> | string | undefined} only works when enabled
256265
*/
257266
function defaultGetFormat(url, context) {
267+
if (isExperimentalTextImport(context?.importAttributes)) {
268+
return 'text';
269+
}
270+
258271
const protocol = url.protocol;
259272
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
260273
return null;

lib/internal/modules/esm/load.js

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

1110
const { defaultGetFormat } = require('internal/modules/esm/get_format');
1211
const { validateAttributes, emitImportAssertionWarning } = require('internal/modules/esm/assert');
@@ -49,11 +48,6 @@ function getSourceSync(url, context) {
4948
return { __proto__: null, responseURL, source };
5049
}
5150

52-
function isExperimentalTextImport(importAttributes) {
53-
return getOptionValue('--experimental-import-text') &&
54-
importAttributes?.type === 'text';
55-
}
56-
5751
/**
5852
* Node.js default load hook.
5953
* @param {string} url
@@ -95,12 +89,8 @@ function defaultLoad(url, context = kEmptyObject) {
9589
}
9690

9791
if (format == null) {
98-
if (isExperimentalTextImport(importAttributes)) {
99-
format = 'text';
100-
}
101-
10292
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
103-
format ??= defaultGetFormat(urlInstance, context);
93+
format = defaultGetFormat(urlInstance, context);
10494

10595
if (format === 'commonjs') {
10696
// For backward compatibility reasons, we need to discard the source in
@@ -163,10 +153,6 @@ function defaultLoadSync(url, context = kEmptyObject) {
163153
context = { __proto__: context, source };
164154
}
165155

166-
if (format == null && isExperimentalTextImport(importAttributes)) {
167-
format = 'text';
168-
}
169-
170156
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
171157
format ??= defaultGetFormat(urlInstance, context);
172158

lib/internal/modules/esm/resolve.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,6 @@ function throwIfInvalidParentURL(parentURL) {
943943
*/
944944
function defaultResolve(specifier, context = {}) {
945945
let { parentURL, conditions } = context;
946-
const { importAttributes } = context;
947946
throwIfInvalidParentURL(parentURL);
948947

949948
let parsedParentURL;
@@ -1006,18 +1005,12 @@ function defaultResolve(specifier, context = {}) {
10061005
throw error;
10071006
}
10081007

1009-
let format = defaultGetFormatWithoutErrors(url, context);
1010-
if (getOptionValue('--experimental-import-text') &&
1011-
importAttributes?.type === 'text') {
1012-
format = 'text';
1013-
}
1014-
10151008
return {
10161009
__proto__: null,
10171010
// Do NOT cast `url` to a string: that will work even when there are real
10181011
// problems, silencing them
10191012
url: url.href,
1020-
format,
1013+
format: defaultGetFormatWithoutErrors(url, context),
10211014
};
10221015
}
10231016

0 commit comments

Comments
 (0)