-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (40 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
'use strict';
const path = require('path');
const localResolve = module.constructor._findPath;
const defaultExtension = ['.js', 'jsx', '.json'];
function findConfiguredPath(modulePath, alias) {
const __pathes = modulePath.split('/');
let aliasPath = null;
if (__pathes.length === 1) {
aliasPath = alias[__pathes[0]] || alias[__pathes[0] + '$'];
} else {
aliasPath = alias[__pathes[0]];
}
return aliasPath;
}
function fileWithExtension(path, extensions) {
return extensions.some(ext => path.endsWith(ext));
}
exports.interfaceVersion = 2;
exports.resolve = function(modulePath, sourceFile, config) {
if (config && config.resolve && config.resolve.alias) {
const __pathes = modulePath.split('/');
const aliasPath = findConfiguredPath(modulePath, config.resolve.alias);
if (aliasPath) {
let filename = null;
if (fileWithExtension(aliasPath, defaultExtension)) {
filename = localResolve(aliasPath, []);
} else {
const filePath = aliasPath.startsWith('.')
? path.join(__dirname, aliasPath, ...__pathes.slice(1))
: path.join(aliasPath, ...__pathes.slice(1));
filename = localResolve(filePath, []);
}
return {
found: !!filename,
path: filename || null,
};
}
}
return { found: false };
};