Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/internal/modules/esm/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypePush,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectValues,
Expand Down Expand Up @@ -30,7 +31,6 @@
'commonjs': kImplicitTypeAttribute,
'json': 'json',
'module': kImplicitTypeAttribute,
'text': 'text',
'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
};
// NOTE: Don't add bytes support yet as it requires Uint8Arrays backed by immutable ArrayBuffers,
Expand All @@ -48,6 +48,15 @@
ObjectValues(formatTypeMap),
(type) => type !== kImplicitTypeAttribute);

function initializeImportAttributes() {
if (getOptionValue('--experimental-import-text') &&
formatTypeMap['text'] === undefined

Check failure on line 53 in lib/internal/modules/esm/assert.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

["text"] is better written in dot notation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
formatTypeMap['text'] === undefined
formatTypeMap.text === undefined

) {
formatTypeMap['text'] = 'text';

Check failure on line 55 in lib/internal/modules/esm/assert.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

["text"] is better written in dot notation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
formatTypeMap['text'] = 'text';
formatTypeMap.text = 'text';

ArrayPrototypePush(supportedTypeAttributes, 'text');
}
}

/**
* Test a module's import attributes.
* @param {string} url The URL of the imported module, for error reporting.
Expand All @@ -67,12 +76,6 @@
}
const validType = formatTypeMap[format];

if (validType !== undefined &&
importAttributes.type === 'text' &&
!getOptionValue('--experimental-import-text')) {
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', importAttributes.type, url);
}

switch (validType) {
case undefined:
// Ignore attributes for module formats we don't recognize, to allow new
Expand Down Expand Up @@ -122,6 +125,7 @@


module.exports = {
initializeImportAttributes,
kImplicitTypeAttribute,
validateAttributes,
};
2 changes: 2 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ function prepareExecution(options) {

const { initializeExtensionFormatMap } = require('internal/modules/esm/get_format');
initializeExtensionFormatMap();
const { initializeImportAttributes } = require('internal/modules/esm/assert');
initializeImportAttributes();

setupVmModules();
if (initializeModules) {
Expand Down
23 changes: 23 additions & 0 deletions test/es-module/test-esm-loader-text-format.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import '../common/index.mjs';
import assert from 'node:assert';
import { registerHooks } from 'node:module';

// A user loader can use `text` with and without import attributes without the feature flag.

registerHooks({
load(url, context, nextLoad) {
if (url.endsWith('.txt')) {
return nextLoad(url, { ...context, format: 'text' });
}
return nextLoad(url, context);
},
});

const { default: text } = await import('../fixtures/file-to-read-without-bom.txt');
const { default: empty } = await import(
'../fixtures/empty.txt',
{ with: { type: 'text' } }
);

assert.strictEqual(text, 'abc\ndef\nghi\n');
assert.strictEqual(empty, '');
Loading