• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  RegExpPrototypeExec,
5} = primordials;
6const { getOptionValue } = require('internal/options');
7
8
9const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
10
11const extensionFormatMap = {
12  '__proto__': null,
13  '.cjs': 'commonjs',
14  '.js': 'module',
15  '.json': 'json',
16  '.mjs': 'module',
17};
18
19const legacyExtensionFormatMap = {
20  '__proto__': null,
21  '.cjs': 'commonjs',
22  '.js': 'commonjs',
23  '.json': 'commonjs',
24  '.mjs': 'module',
25  '.node': 'commonjs',
26};
27
28if (experimentalWasmModules) {
29  extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
30}
31
32/**
33 * @param {string} mime
34 * @returns {string | null}
35 */
36function mimeToFormat(mime) {
37  if (
38    RegExpPrototypeExec(
39      /\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i,
40      mime,
41    ) !== null
42  ) return 'module';
43  if (mime === 'application/json') return 'json';
44  if (experimentalWasmModules && mime === 'application/wasm') return 'wasm';
45  return null;
46}
47
48function getLegacyExtensionFormat(ext) {
49  return legacyExtensionFormatMap[ext];
50}
51
52module.exports = {
53  extensionFormatMap,
54  getLegacyExtensionFormat,
55  legacyExtensionFormatMap,
56  mimeToFormat,
57};
58