node/lib/internal/modules/esm/formats.js
Antoine du Hamel fe514bf960
lib: enforce use of trailing commas for functions
PR-URL: https://github.com/nodejs/node/pull/46629
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
2023-02-14 18:45:16 +01:00

42 lines
854 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,
};