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

Refs: https://github.com/nodejs/node/issues/33238 Refs: https://github.com/nodejs/node/pull/33282 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/37178 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
84 lines
2.5 KiB
JavaScript
84 lines
2.5 KiB
JavaScript
'use strict';
|
|
const {
|
|
RegExpPrototypeExec,
|
|
StringPrototypeStartsWith,
|
|
} = 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';
|
|
|
|
function defaultGetFormat(url, context, defaultGetFormatUnused) {
|
|
if (StringPrototypeStartsWith(url, 'node:')) {
|
|
return { format: 'builtin' };
|
|
}
|
|
const parsed = new URL(url);
|
|
if (parsed.protocol === 'data:') {
|
|
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 };
|
|
} else if (parsed.protocol === 'file:') {
|
|
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: format || null };
|
|
}
|
|
return { format: null };
|
|
}
|
|
|
|
module.exports = {
|
|
defaultGetFormat,
|
|
extensionFormatMap,
|
|
legacyExtensionFormatMap,
|
|
};
|