-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: update ESM hook examples esm: fix unsafe primordial doc: fix ESM example linting esm: allow source of type ArrayBuffer doc: update ESM hook changelog to include resolve format esm: allow all ArrayBuffers and TypedArrays for load hook source doc: tidy code & API docs doc: convert ESM source table header from Title Case to Sentence case doc: add detailed explanation for getPackageType esm: add caveat that ESMLoader::import() must NOT be renamed esm: tidy code declaration of getFormat protocolHandlers doc: correct ESM doc link (bad conflict resolution) doc: update ESM hook limitation for CJS esm: tweak preload description doc: update ESM getPackageType() example explanation PR-URL: #37468 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
- v23.7.0
- v23.6.1
- v23.6.0
- v23.5.0
- v23.4.0
- v23.3.0
- v23.2.0
- v23.1.0
- v23.0.0
- v22.13.1
- v22.13.0
- v22.12.0
- v22.11.0
- v22.10.0
- v22.9.0
- v22.8.0
- v22.7.0
- v22.6.0
- v22.5.1
- v22.5.0
- v22.4.1
- v22.4.0
- v22.3.0
- v22.2.0
- v22.1.0
- v22.0.0
- v21.7.3
- v21.7.2
- v21.7.1
- v21.7.0
- v21.6.2
- v21.6.1
- v21.6.0
- v21.5.0
- v21.4.0
- v21.3.0
- v21.2.0
- v21.1.0
- v21.0.0
- v20.18.2
- v20.18.1
- v20.18.0
- v20.17.0
- v20.16.0
- v20.15.1
- v20.15.0
- v20.14.0
- v20.13.1
- v20.13.0
- v20.12.2
- v20.12.1
- v20.12.0
- v20.11.1
- v20.11.0
- v20.10.0
- v20.9.0
- v20.8.1
- v20.8.0
- v20.7.0
- v20.6.1
- v20.6.0
- v20.5.1
- v20.5.0
- v20.4.0
- v20.3.1
- v20.3.0
- v20.2.0
- v20.1.0
- v20.0.0
- v19.9.0
- v19.8.1
- v19.8.0
- v19.7.0
- v19.6.1
- v19.6.0
- v19.5.0
- v19.4.0
- v19.3.0
- v19.2.0
- v19.1.0
- v19.0.1
- v19.0.0
- v18.20.6
- v18.20.5
- v18.20.4
- v18.20.3
- v18.20.2
- v18.20.1
- v18.20.0
- v18.19.1
- v18.19.0
- v18.18.2
- v18.18.1
- v18.18.0
- v18.17.1
- v18.17.0
- v18.16.1
- v18.16.0
- v18.15.0
- v18.14.2
- v18.14.1
- v18.14.0
- v18.13.0
- v18.12.1
- v18.12.0
- v18.11.0
- v18.10.0
- v18.9.1
- v18.9.0
- v18.8.0
- v18.7.0
- v18.6.0
- v18.5.0
- v18.4.0
- v18.3.0
- v18.2.0
- v18.1.0
- v18.0.0
- v17.9.1
- v17.9.0
- v17.8.0
- v17.7.2
- v17.7.1
- v17.7.0
- v17.6.0
- v17.5.0
- v17.4.0
- v17.3.1
- v17.3.0
- v17.2.0
- v17.1.0
- v17.0.1
- v17.0.0
1 parent
540f9d9
commit df22736
Showing
46 changed files
with
972 additions
and
545 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const { defaultGetFormat } = require('internal/modules/esm/get_format'); | ||
const { defaultGetSource } = require('internal/modules/esm/get_source'); | ||
const { translators } = require('internal/modules/esm/translators'); | ||
|
||
async function defaultLoad(url, context) { | ||
let { | ||
format, | ||
source, | ||
} = context; | ||
|
||
if (!translators.has(format)) format = defaultGetFormat(url); | ||
|
||
if ( | ||
format === 'builtin' || | ||
format === 'commonjs' | ||
) { | ||
source = null; | ||
} else if (source == null) { | ||
source = await defaultGetSource(url, { format }); | ||
} | ||
|
||
return { | ||
format, | ||
source, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
defaultLoad, | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/hooks-custom.mjs | ||
import '../common/index.mjs'; | ||
import assert from 'assert'; | ||
|
||
|
||
await assert.rejects( | ||
import('nonexistent/file.mjs'), | ||
{ code: 'ERR_MODULE_NOT_FOUND' }, | ||
); | ||
|
||
await assert.rejects( | ||
import('../fixtures/es-modules/file.unknown'), | ||
{ code: 'ERR_UNKNOWN_FILE_EXTENSION' }, | ||
); | ||
|
||
await assert.rejects( | ||
import('esmHook/badReturnVal.mjs'), | ||
{ code: 'ERR_INVALID_RETURN_VALUE' }, | ||
); | ||
|
||
// Nullish values are allowed; booleans are not | ||
await assert.rejects( | ||
import('esmHook/format.false'), | ||
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, | ||
); | ||
await assert.rejects( | ||
import('esmHook/format.true'), | ||
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, | ||
); | ||
|
||
await assert.rejects( | ||
import('esmHook/badReturnFormatVal.mjs'), | ||
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, | ||
); | ||
|
||
await assert.rejects( | ||
import('esmHook/unsupportedReturnFormatVal.mjs'), | ||
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' }, | ||
); | ||
|
||
await assert.rejects( | ||
import('esmHook/badReturnSourceVal.mjs'), | ||
{ code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }, | ||
); | ||
|
||
await import('../fixtures/es-module-loaders/js-as-esm.js') | ||
.then((parsedModule) => { | ||
assert.strictEqual(typeof parsedModule, 'object'); | ||
assert.strictEqual(parsedModule.namedExport, 'named-export'); | ||
}); | ||
|
||
// The custom loader tells node .ext files are MJS | ||
await import('../fixtures/es-modules/file.ext') | ||
.then((parsedModule) => { | ||
assert.strictEqual(typeof parsedModule, 'object'); | ||
const { default: defaultExport } = parsedModule; | ||
assert.strictEqual(typeof defaultExport, 'function'); | ||
assert.strictEqual(defaultExport.name, 'iAmReal'); | ||
assert.strictEqual(defaultExport(), true); | ||
}); | ||
|
||
// The custom loader's resolve hook predetermines the format | ||
await import('esmHook/preknownFormat.pre') | ||
.then((parsedModule) => { | ||
assert.strictEqual(typeof parsedModule, 'object'); | ||
assert.strictEqual(parsedModule.default, 'hello world'); | ||
}); | ||
|
||
// The custom loader provides source even though file does not actually exist | ||
await import('esmHook/virtual.mjs') | ||
.then((parsedModule) => { | ||
assert.strictEqual(typeof parsedModule, 'object'); | ||
assert.strictEqual(typeof parsedModule.default, 'undefined'); | ||
assert.strictEqual(parsedModule.message, 'WOOHOO!'); | ||
}); | ||
|
||
// Ensure custom loaders have separate context from userland | ||
// hooks-custom.mjs also increments count (which starts at 0) | ||
// if both share context, count here would be 2 | ||
await import('../fixtures/es-modules/stateful.mjs') | ||
.then(({ default: count }) => { | ||
assert.strictEqual(count(), 1); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import count from '../es-modules/stateful.mjs'; | ||
|
||
|
||
// Arbitrary instance of manipulating a module's internal state | ||
// used to assert node-land and user-land have different contexts | ||
count(); | ||
|
||
/** | ||
* @param {string} url A fully resolved file url. | ||
* @param {object} context Additional info. | ||
* @param {function} next for now, next is defaultLoad a wrapper for | ||
* defaultGetFormat + defaultGetSource | ||
* @returns {{ format: string, source: (string|SharedArrayBuffer|Uint8Array) }} | ||
*/ | ||
export function load(url, context, next) { | ||
// Load all .js files as ESM, regardless of package scope | ||
if (url.endsWith('.js')) return next(url, { | ||
...context, | ||
format: 'module', | ||
}); | ||
|
||
if (url.endsWith('.ext')) return next(url, { | ||
...context, | ||
format: 'module', | ||
}); | ||
|
||
if (url === 'esmHook/badReturnVal.mjs') return 'export function returnShouldBeObject() {}'; | ||
|
||
if (url === 'esmHook/badReturnFormatVal.mjs') return { | ||
format: Array(0), | ||
source: '', | ||
} | ||
if (url === 'esmHook/unsupportedReturnFormatVal.mjs') return { | ||
format: 'foo', // Not one of the allowable inputs: no translator named 'foo' | ||
source: '', | ||
} | ||
|
||
if (url === 'esmHook/badReturnSourceVal.mjs') return { | ||
format: 'module', | ||
source: Array(0), | ||
} | ||
|
||
if (url === 'esmHook/preknownFormat.pre') return { | ||
format: context.format, | ||
source: `const msg = 'hello world'; export default msg;` | ||
}; | ||
|
||
if (url === 'esmHook/virtual.mjs') return { | ||
format: 'module', | ||
source: `export const message = 'Woohoo!'.toUpperCase();`, | ||
}; | ||
|
||
return next(url, context, next); | ||
} | ||
|
||
export function resolve(specifier, context, next) { | ||
let format = ''; | ||
|
||
if (specifier === 'esmHook/format.false') format = false; | ||
if (specifier === 'esmHook/format.true') format = true; | ||
if (specifier === 'esmHook/preknownFormat.pre') format = 'module'; | ||
|
||
if (specifier.startsWith('esmHook')) return { | ||
format, | ||
url: specifier, | ||
}; | ||
|
||
return next(specifier, context, next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function dynamicInstantiate() {} | ||
export function getFormat() {} | ||
export function getSource() {} | ||
export function transformSource() {} | ||
|
||
|
||
export function load(url, context, next) { | ||
if (url === 'whatever') return { | ||
format: 'module', | ||
source: '', | ||
}; | ||
|
||
return next(url, context, next); | ||
} | ||
|
||
export function resolve(specifier, context, next) { | ||
if (specifier === 'whatever') return { | ||
url: specifier, | ||
}; | ||
|
||
return next(specifier, context, next); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 5 additions & 15 deletions
20
test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
export async function resolve(specifier, { parentURL }, defaultResolve) { | ||
if (specifier === 'unknown-builtin-module') { | ||
return { | ||
url: 'node:unknown-builtin-module' | ||
}; | ||
} | ||
return defaultResolve(specifier, {parentURL}, defaultResolve); | ||
} | ||
export function resolve(specifier, context, next) { | ||
if (specifier === 'unknown-builtin-module') return { | ||
url: 'node:unknown-builtin-module' | ||
}; | ||
|
||
export async function getFormat(url, context, defaultGetFormat) { | ||
if (url === 'node:unknown-builtin-module') { | ||
return { | ||
format: 'builtin' | ||
}; | ||
} | ||
return defaultGetFormat(url, context, defaultGetFormat); | ||
return next(specifier, context, next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// fixtures/es-module-loader.mjs tells node to treat this file like ESM | ||
|
||
export default function iAmReal() { return true }; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let counter = 0; | ||
|
||
export default function count() { | ||
return ++counter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Flags: --no-warnings --throw-deprecation --experimental-loader ./test/fixtures/es-module-loaders/hooks-obsolete.mjs | ||
/* eslint-disable node-core/require-common-first, node-core/required-modules */ | ||
|
||
await import('whatever'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
node:internal/process/warning:* | ||
throw warning; | ||
^ | ||
|
||
DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: dynamicInstantiate, getFormat, getSource, transformSource | ||
at Function.pluckHooks (node:internal/modules/esm/loader:*:*) | ||
at ESMLoader.addCustomLoaders (node:internal/modules/esm/loader:*:*) | ||
at initializeLoader (node:internal/process/esm_loader:*:*) | ||
at async loadESM (node:internal/process/esm_loader:*:*) | ||
at async handleMainPromise (node:internal/modules/run_main:*:*) | ||
Node.js * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters