This repository was archived by the owner on Apr 16, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdefault_resolve.js
More file actions
88 lines (74 loc) · 2.39 KB
/
default_resolve.js
File metadata and controls
88 lines (74 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const internalFS = require('internal/fs/utils');
const { NativeModule } = require('internal/bootstrap/loaders');
const { extname } = require('path');
const { realpathSync } = require('fs');
const { getOptionValue } = require('internal/options');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
const { resolve: moduleWrapResolve } = internalBinding('module_wrap');
const { pathToFileURL, fileURLToPath } = require('internal/url');
const { typeFlag } = require('internal/process/esm_loader');
const realpathCache = new Map();
const extensionFormatMap = {
'__proto__': null,
'.cjs': 'cjs',
'.js': 'esm',
'.mjs': 'esm'
};
const legacyExtensionFormatMap = {
'__proto__': null,
'.cjs': 'cjs',
'.js': 'cjs',
'.json': 'cjs',
'.mjs': 'esm',
'.node': 'cjs'
};
if (experimentalWasmModules) {
// This is a total hack
Object.assign(extensionFormatMap, {
'.wasm': 'wasm'
});
Object.assign(legacyExtensionFormatMap, {
'.wasm': 'wasm'
});
}
function resolve(specifier, parentURL) {
if (NativeModule.canBeRequiredByUsers(specifier)) {
return {
url: specifier,
format: 'builtin'
};
}
const isMain = parentURL === undefined;
if (isMain)
parentURL = pathToFileURL(`${process.cwd()}/`).href;
const resolved = moduleWrapResolve(specifier,
parentURL,
isMain,
typeFlag === 'module');
let url = resolved.url;
const legacy = resolved.legacy;
if (isMain ? !preserveSymlinksMain : !preserveSymlinks) {
const real = realpathSync(fileURLToPath(url), {
[internalFS.realpathCacheKey]: realpathCache
});
const old = url;
url = pathToFileURL(real);
url.search = old.search;
url.hash = old.hash;
}
const ext = extname(url.pathname);
let format = (legacy ? legacyExtensionFormatMap : extensionFormatMap)[ext];
if (!format) {
if (isMain)
format = legacy ? 'cjs' : 'esm';
else
throw new ERR_UNKNOWN_FILE_EXTENSION(fileURLToPath(url),
fileURLToPath(parentURL));
}
return { url: `${url}`, format };
}
module.exports = resolve;