-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathindex.js
139 lines (116 loc) · 3.29 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const path = require('path');
const resolve = require('resolve');
const tsconfigPaths = require('tsconfig-paths');
const debug = require('debug');
const log = debug('eslint-import-resolver-typescript');
const extensions = Object.keys(require.extensions).concat(
'.ts',
'.tsx',
'.d.ts',
);
/**
* @param {string} source the module to resolve; i.e './some-module'
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
*/
function resolveFile(source, file, options = {}) {
log('looking for:', source);
// don't worry about core node modules
if (resolve.isCore(source)) {
log('matched core:', source);
return {
found: true,
path: null,
};
}
initMappers(options);
const mappedPath = getMappedPath(source, file);
if (mappedPath) {
log('matched ts path:', mappedPath);
}
// note that even if we map the path, we still need to do a final resolve
let foundNodePath;
try {
foundNodePath = resolve.sync(mappedPath || source, {
extensions,
basedir: path.dirname(path.resolve(file)),
packageFilter,
});
} catch (err) {
foundNodePath = null;
}
if (foundNodePath) {
log('matched node path:', foundNodePath);
return {
found: true,
path: foundNodePath,
};
}
log('didnt find', source);
return {
found: false,
};
}
function packageFilter(pkg) {
if (pkg['jsnext:main']) {
pkg['main'] = pkg['jsnext:main'];
}
return pkg;
}
/**
* @param {string} source the module to resolve; i.e './some-module'
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
* @returns The mapped path of the module or undefined
*/
function getMappedPath(source, file) {
const paths = mappers
.map(mapper => mapper(source, file))
.filter(path => !!path);
if (paths.length > 1) {
log('found multiple matching ts paths:', paths);
}
return paths[0];
}
let mappers;
function initMappers(options) {
if (mappers) {
return;
}
const isArrayOfStrings = array =>
Array.isArray(array) && array.every(o => typeof o === 'string');
const configPaths =
typeof options.directory === 'string'
? [options.directory]
: isArrayOfStrings(options.directory)
? options.directory
: [process.cwd()];
mappers = configPaths
.map(path => tsconfigPaths.loadConfig(path))
.filter(configLoaderResult => {
const success = configLoaderResult.resultType === 'success';
if (!success) {
// this can happen if the user has problems with their tsconfig
// or if it's valid, but they don't have baseUrl set
log('failed to init tsconfig-paths:', configLoaderResult.message);
}
return success;
})
.map(configLoaderResult => {
const matchPath = tsconfigPaths.createMatchPath(
configLoaderResult.absoluteBaseUrl,
configLoaderResult.paths,
);
return (source, file) => {
// exclude files that are not part of the config base url
if (!file.includes(configLoaderResult.absoluteBaseUrl)) {
return undefined;
}
// look for files based on setup tsconfig "paths"
return matchPath(source, undefined, undefined, extensions);
};
});
}
module.exports = {
interfaceVersion: 2,
resolve: resolveFile,
};