The old import assertions proposal has been
renamed to "import attributes" with the follwing major changes:
1. The keyword is now `with` instead of `assert`.
2. Unknown assertions cause an error rather than being ignored,
This commit updates the documentation to encourage folks to use the new
syntax, and add aliases for module customization hooks.
PR-URL: https://github.com/nodejs/node/pull/50140
Fixes: https://github.com/nodejs/node/issues/50134
Refs: 159c82c5e6
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
const SOURCES = {
|
|
__proto__: null,
|
|
'test:Array': ['1', '2'], // both `1,2` and `12` are valid ESM
|
|
'test:ArrayBuffer': new ArrayBuffer(0),
|
|
'test:BigInt64Array': new BigInt64Array(0),
|
|
'test:BigUint64Array': new BigUint64Array(0),
|
|
'test:Float32Array': new Float32Array(0),
|
|
'test:Float64Array': new Float64Array(0),
|
|
'test:Int8Array': new Int8Array(0),
|
|
'test:Int16Array': new Int16Array(0),
|
|
'test:Int32Array': new Int32Array(0),
|
|
'test:null': null,
|
|
'test:Object': {},
|
|
'test:SharedArrayBuffer': new SharedArrayBuffer(0),
|
|
'test:string': '',
|
|
'test:String': new String(''),
|
|
'test:Uint8Array': new Uint8Array(0),
|
|
'test:Uint8ClampedArray': new Uint8ClampedArray(0),
|
|
'test:Uint16Array': new Uint16Array(0),
|
|
'test:Uint32Array': new Uint32Array(0),
|
|
'test:undefined': undefined,
|
|
}
|
|
export function resolve(specifier, context, next) {
|
|
if (specifier.startsWith('test:')) {
|
|
return {
|
|
importAttributes: context.importAttributes,
|
|
shortCircuit: true,
|
|
url: specifier,
|
|
};
|
|
}
|
|
return next(specifier);
|
|
}
|
|
|
|
export function load(href, context, next) {
|
|
if (href.startsWith('test:')) {
|
|
return {
|
|
format: 'module',
|
|
shortCircuit: true,
|
|
source: SOURCES[href],
|
|
};
|
|
}
|
|
return next(href);
|
|
}
|