node/lib/internal/modules/esm/formats.js
Geoffrey Booth f594cc85b7
esm: remove specifier resolution flag
PR-URL: https://github.com/nodejs/node/pull/44859
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2022-10-04 09:44:08 +00:00

42 lines
853 B
JavaScript

'use strict';
const {
RegExpPrototypeExec,
} = primordials;
const { getOptionValue } = require('internal/options');
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
const extensionFormatMap = {
'__proto__': null,
'.cjs': 'commonjs',
'.js': 'module',
'.json': 'json',
'.mjs': 'module',
};
if (experimentalWasmModules) {
extensionFormatMap['.wasm'] = 'wasm';
}
/**
* @param {string} mime
* @returns {string | null}
*/
function mimeToFormat(mime) {
if (
RegExpPrototypeExec(
/\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i,
mime
) !== null
) return 'module';
if (mime === 'application/json') return 'json';
if (experimentalWasmModules && mime === 'application/wasm') return 'wasm';
return null;
}
module.exports = {
extensionFormatMap,
mimeToFormat,
};