-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (52 loc) · 1.23 KB
/
index.js
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
const resolve = require('resolve');
const { resolvePath } = require('babel-plugin-module-resolver');
// https://github.com/dividab/tsconfig-paths/blob/f42003925f4d56458d41daed80013c8ad23c88ea/src/register.ts#L9-L37
const builtinModules = [
'assert',
'buffer',
'child_process',
'cluster',
'crypto',
'dgram',
'dns',
'domain',
'events',
'fs',
'http',
'https',
'net',
'os',
'path',
'punycode',
'querystring',
'readline',
'stream',
'string_decoder',
'tls',
'tty',
'url',
'util',
'v8',
'vm',
'zlib',
].reduce((r, m) => ({ ...r, [m]: true }), {});
/**
* Leaves built-in modules and node_modules untouched.
*/
module.exports = function betterResolvePath(sourcePath, currentFile, opts) {
// keep built-in modules, `resolve.sync` will handle other styles of built-in modules import (ex. require('fs/promises'))
if (builtinModules[sourcePath]) {
return sourcePath;
}
try {
const realPath = resolve.sync(sourcePath);
// detect node modules or
if (
realPath === sourcePath || // require('fs/promises')
realPath.includes(`node_modules/${sourcePath}`) // dependencies
) {
return sourcePath;
}
} catch (error) {}
return resolvePath(sourcePath, currentFile, opts);
};