1 |
|
2 |
|
3 | 'use strict';
|
4 | const {
|
5 | RegExpPrototypeExec,
|
6 | StringPrototypeStartsWith,
|
7 | } = require('./node-primordials');
|
8 | const { extname } = require('path');
|
9 | const { getOptionValue } = require('./node-options');
|
10 |
|
11 | const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(s => parseInt(s, 10));
|
12 | const experimentalJsonModules =
|
13 | nodeMajor > 17
|
14 | || (nodeMajor === 17 && nodeMinor >= 5)
|
15 | || (nodeMajor === 16 && nodeMinor >= 15)
|
16 | || getOptionValue('--experimental-json-modules');
|
17 | const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
|
18 | const { URL, fileURLToPath } = require('url');
|
19 | const { ERR_UNKNOWN_FILE_EXTENSION } = require('./node-internal-errors').codes;
|
20 |
|
21 | const extensionFormatMap = {
|
22 | '__proto__': null,
|
23 | '.cjs': 'commonjs',
|
24 | '.js': 'module',
|
25 | '.mjs': 'module'
|
26 | };
|
27 |
|
28 | const legacyExtensionFormatMap = {
|
29 | '__proto__': null,
|
30 | '.cjs': 'commonjs',
|
31 | '.js': 'commonjs',
|
32 | '.json': 'commonjs',
|
33 | '.mjs': 'module',
|
34 | '.node': 'commonjs'
|
35 | };
|
36 |
|
37 | if (experimentalWasmModules)
|
38 | extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
|
39 |
|
40 | if (experimentalJsonModules)
|
41 | extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function createGetFormat(tsNodeExperimentalSpecifierResolution, nodeEsmResolver) {
|
51 |
|
52 | let experimentalSpeciferResolution = tsNodeExperimentalSpecifierResolution != null ? tsNodeExperimentalSpecifierResolution : getOptionValue('--experimental-specifier-resolution');
|
53 | const { getPackageType } = nodeEsmResolver;
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function defaultGetFormat(url, context, defaultGetFormatUnused) {
|
62 | if (StringPrototypeStartsWith(url, 'node:')) {
|
63 | return { format: 'builtin' };
|
64 | }
|
65 | const parsed = new URL(url);
|
66 | if (parsed.protocol === 'data:') {
|
67 | const [ , mime ] = RegExpPrototypeExec(
|
68 | /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
|
69 | parsed.pathname,
|
70 | ) || [ null, null, null ];
|
71 | const format = ({
|
72 | '__proto__': null,
|
73 | 'text/javascript': 'module',
|
74 | 'application/json': experimentalJsonModules ? 'json' : null,
|
75 | 'application/wasm': experimentalWasmModules ? 'wasm' : null
|
76 | })[mime] || null;
|
77 | return { format };
|
78 | } else if (parsed.protocol === 'file:') {
|
79 | const ext = extname(parsed.pathname);
|
80 | let format;
|
81 | if (ext === '.js') {
|
82 | format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs';
|
83 | } else {
|
84 | format = extensionFormatMap[ext];
|
85 | }
|
86 | if (!format) {
|
87 | if (experimentalSpeciferResolution === 'node') {
|
88 | process.emitWarning(
|
89 | 'The Node.js specifier resolution in ESM is experimental.',
|
90 | 'ExperimentalWarning');
|
91 | format = legacyExtensionFormatMap[ext];
|
92 | } else {
|
93 | throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));
|
94 | }
|
95 | }
|
96 | return { format: format || null };
|
97 | }
|
98 | return { format: null };
|
99 | }
|
100 |
|
101 | return {defaultGetFormat};
|
102 | }
|
103 |
|
104 | module.exports = {
|
105 | createGetFormat
|
106 | };
|