mirror of
https://github.com/nodejs/node.git
synced 2025-05-09 01:30:25 +00:00

Co-authored-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Co-authored-by: James M Snell <jasnell@gmail.com> Co-authored-by: Jordan Harband <ljharb@gmail.com> Co-authored-by: James Sumners <james@sumners.email> PR-URL: https://github.com/nodejs/node/pull/36328 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
RegExpPrototypeTest,
|
|
} = primordials;
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
|
|
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
|
|
const experimentalSpecifierResolution =
|
|
getOptionValue('--experimental-specifier-resolution');
|
|
|
|
const extensionFormatMap = {
|
|
'__proto__': null,
|
|
'.cjs': 'commonjs',
|
|
'.js': 'module',
|
|
'.json': 'json',
|
|
'.mjs': 'module',
|
|
};
|
|
|
|
const legacyExtensionFormatMap = {
|
|
'__proto__': null,
|
|
'.cjs': 'commonjs',
|
|
'.js': 'commonjs',
|
|
'.json': 'commonjs',
|
|
'.mjs': 'module',
|
|
'.node': 'commonjs',
|
|
};
|
|
|
|
if (experimentalWasmModules) {
|
|
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
|
|
}
|
|
|
|
function mimeToFormat(mime) {
|
|
if (
|
|
RegExpPrototypeTest(
|
|
/\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i,
|
|
mime
|
|
)
|
|
) return 'module';
|
|
if (mime === 'application/json') return 'json';
|
|
if (experimentalWasmModules && mime === 'application/wasm') return 'wasm';
|
|
return null;
|
|
}
|
|
|
|
let experimentalSpecifierResolutionWarned = false;
|
|
function getLegacyExtensionFormat(ext) {
|
|
if (
|
|
experimentalSpecifierResolution === 'node' &&
|
|
!experimentalSpecifierResolutionWarned
|
|
) {
|
|
process.emitWarning(
|
|
'The Node.js specifier resolution in ESM is experimental.',
|
|
'ExperimentalWarning');
|
|
experimentalSpecifierResolutionWarned = true;
|
|
}
|
|
return legacyExtensionFormatMap[ext];
|
|
}
|
|
|
|
module.exports = {
|
|
extensionFormatMap,
|
|
getLegacyExtensionFormat,
|
|
legacyExtensionFormatMap,
|
|
mimeToFormat,
|
|
};
|