node/lib/internal/modules/esm/formats.js
Bradley Farias ceadb473e6 esm: support https remotely and http locally under flag
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>
2022-02-09 19:47:12 -08:00

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,
};