| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 262d88e commit b1770bc
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | const { | |
| 4 | 4 | ArrayPrototypeFilter, | |
| 5 | 5 | ArrayPrototypeIncludes, | |
| 6 | + ArrayPrototypePush, | ||
| 6 | 7 | ObjectKeys, | |
| 7 | 8 | ObjectPrototypeHasOwnProperty, | |
| 8 | 9 | ObjectValues, | |
@@ -30,7 +31,6 @@ const formatTypeMap = { | |||
| 30 | 31 | 'commonjs': kImplicitTypeAttribute, | |
| 31 | 32 | 'json': 'json', | |
| 32 | 33 | 'module': kImplicitTypeAttribute, | |
| 33 | - 'text': 'text', | ||
| 34 | 34 | '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 | |
| 35 | 35 | }; | |
| 36 | 36 | // NOTE: Don't add bytes support yet as it requires Uint8Arrays backed by immutable ArrayBuffers, | |
@@ -48,6 +48,21 @@ const supportedTypeAttributes = ArrayPrototypeFilter( | |||
| 48 | 48 | ObjectValues(formatTypeMap), | |
| 49 | 49 | (type) => type !== kImplicitTypeAttribute); | |
| 50 | 50 | ||
| 51 | + let importAttributesInitialized = false; | ||
| 52 | + | ||
| 53 | + function initializeImportAttributes() { | ||
| 54 | + if (importAttributesInitialized) { | ||
| 55 | + return; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + importAttributesInitialized = true; | ||
| 59 | + | ||
| 60 | + if (getOptionValue('--experimental-import-text')) { | ||
| 61 | + formatTypeMap.text = 'text'; | ||
| 62 | + ArrayPrototypePush(supportedTypeAttributes, 'text'); | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 51 | 66 | /** | |
| 52 | 67 | * Test a module's import attributes. | |
| 53 | 68 | * @param {string} url The URL of the imported module, for error reporting. | |
@@ -59,6 +74,8 @@ const supportedTypeAttributes = ArrayPrototypeFilter( | |||
| 59 | 74 | */ | |
| 60 | 75 | function validateAttributes(url, format, | |
| 61 | 76 | importAttributes = { __proto__: null }) { | |
| 77 | + initializeImportAttributes(); | ||
| 78 | + | ||
| 62 | 79 | const keys = ObjectKeys(importAttributes); | |
| 63 | 80 | for (let i = 0; i < keys.length; i++) { | |
| 64 | 81 | if (keys[i] !== 'type') { | |
@@ -67,12 +84,6 @@ function validateAttributes(url, format, | |||
| 67 | 84 | } | |
| 68 | 85 | const validType = formatTypeMap[format]; | |
| 69 | 86 | ||
| 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 | - } | ||
| 75 | - | ||
| 76 | 87 | switch (validType) { | |
| 77 | 88 | case undefined: | |
| 78 | 89 | // Ignore attributes for module formats we don't recognize, to allow new | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + import '../common/index.mjs'; | ||
| 2 | + import assert from 'node:assert'; | ||
| 3 | + import { registerHooks } from 'node:module'; | ||
| 4 | + | ||
| 5 | + // A user loader can use `text` with and without import attributes without the feature flag. | ||
| 6 | + | ||
| 7 | + registerHooks({ | ||
| 8 | + load(url, context, nextLoad) { | ||
| 9 | + if (url.endsWith('.txt')) { | ||
| 10 | + return nextLoad(url, { ...context, format: 'text' }); | ||
| 11 | + } | ||
| 12 | + return nextLoad(url, context); | ||
| 13 | + }, | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + const { default: text } = await import('../fixtures/file-to-read-without-bom.txt'); | ||
| 17 | + const { default: empty } = await import( | ||
| 18 | + '../fixtures/empty.txt', | ||
| 19 | + { with: { type: 'text' } } | ||
| 20 | + ); | ||
| 21 | + | ||
| 22 | + assert.strictEqual(text, 'abc\ndef\nghi\n'); | ||
| 23 | + assert.strictEqual(empty, ''); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments