mirror of
https://github.com/nodejs/node.git
synced 2025-05-15 06:18:09 +00:00

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: https://github.com/nodejs/node/pull/37468 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
'use strict';
|
|
const {
|
|
ObjectAssign,
|
|
ObjectCreate,
|
|
ObjectPrototypeHasOwnProperty,
|
|
RegExpPrototypeExec,
|
|
} = primordials;
|
|
const { extname } = require('path');
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
|
|
const experimentalSpecifierResolution =
|
|
getOptionValue('--experimental-specifier-resolution');
|
|
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
|
|
const { getPackageType } = require('internal/modules/esm/resolve');
|
|
const { URL, fileURLToPath } = require('internal/url');
|
|
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
|
|
|
|
const extensionFormatMap = {
|
|
'__proto__': null,
|
|
'.cjs': 'commonjs',
|
|
'.js': 'module',
|
|
'.mjs': 'module'
|
|
};
|
|
|
|
const legacyExtensionFormatMap = {
|
|
'__proto__': null,
|
|
'.cjs': 'commonjs',
|
|
'.js': 'commonjs',
|
|
'.json': 'commonjs',
|
|
'.mjs': 'module',
|
|
'.node': 'commonjs'
|
|
};
|
|
|
|
if (experimentalWasmModules)
|
|
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
|
|
|
|
if (experimentalJsonModules)
|
|
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
|
|
|
|
const protocolHandlers = ObjectAssign(ObjectCreate(null), {
|
|
'data:'(parsed) {
|
|
const { 1: mime } = RegExpPrototypeExec(
|
|
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
|
|
parsed.pathname,
|
|
) || [, null];
|
|
const format = ({
|
|
'__proto__': null,
|
|
'text/javascript': 'module',
|
|
'application/json': experimentalJsonModules ? 'json' : null,
|
|
'application/wasm': experimentalWasmModules ? 'wasm' : null
|
|
})[mime] || null;
|
|
|
|
return format;
|
|
},
|
|
'file:'(parsed, url) {
|
|
const ext = extname(parsed.pathname);
|
|
let format;
|
|
|
|
if (ext === '.js') {
|
|
format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs';
|
|
} else {
|
|
format = extensionFormatMap[ext];
|
|
}
|
|
if (!format) {
|
|
if (experimentalSpecifierResolution === 'node') {
|
|
process.emitWarning(
|
|
'The Node.js specifier resolution in ESM is experimental.',
|
|
'ExperimentalWarning');
|
|
format = legacyExtensionFormatMap[ext];
|
|
} else {
|
|
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));
|
|
}
|
|
}
|
|
|
|
return format || null;
|
|
},
|
|
'node:'() { return 'builtin'; },
|
|
});
|
|
|
|
function defaultGetFormat(url, context) {
|
|
const parsed = new URL(url);
|
|
|
|
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ?
|
|
protocolHandlers[parsed.protocol](parsed, url) :
|
|
null;
|
|
}
|
|
|
|
module.exports = {
|
|
defaultGetFormat,
|
|
extensionFormatMap,
|
|
legacyExtensionFormatMap,
|
|
};
|