-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathresolvePath.js
More file actions
118 lines (91 loc) · 3.31 KB
/
resolvePath.js
File metadata and controls
118 lines (91 loc) · 3.31 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
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
import path from 'path';
import { warn } from './log';
import mapToRelative from './mapToRelative';
import normalizeOptions from './normalizeOptions';
import {
nodeResolvePath,
replaceExtension,
isRelativePath,
toLocalPath,
toPosixPath,
} from './utils';
function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
const realSourceFileExtension = path.extname(absFileInRoot);
const sourceFileExtension = path.extname(sourcePath);
let relativePath = mapToRelative(opts.cwd, currentFile, absFileInRoot);
if (realSourceFileExtension !== sourceFileExtension) {
relativePath = replaceExtension(relativePath, opts);
}
return toLocalPath(toPosixPath(relativePath));
}
function findPathInRoots(sourcePath, { extensions, root }) {
// Search the source path inside every custom root directory
let resolvedSourceFile;
root.some(basedir => {
resolvedSourceFile = nodeResolvePath(`./${sourcePath}`, basedir, extensions);
return resolvedSourceFile !== null;
});
return resolvedSourceFile;
}
function resolvePathFromRootConfig(sourcePath, currentFile, opts) {
const absFileInRoot = findPathInRoots(sourcePath, opts);
if (!absFileInRoot) {
return null;
}
return getRelativePath(sourcePath, currentFile, absFileInRoot, opts);
}
function checkIfPackageExists(modulePath, currentFile, extensions, loglevel) {
const resolvedPath = nodeResolvePath(modulePath, currentFile, extensions);
if (resolvedPath === null && loglevel !== 'silent') {
warn(`Could not resolve "${modulePath}" in file ${currentFile}.`);
}
}
function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
let aliasedSourceFile;
opts.alias.find(([regExp, substitute]) => {
const execResult = regExp.exec(sourcePath);
if (execResult === null) {
return false;
}
aliasedSourceFile = substitute(execResult);
return true;
});
if (!aliasedSourceFile) {
return null;
}
// Alias with array of paths
if (Array.isArray(aliasedSourceFile)) {
return aliasedSourceFile
.map(asf => {
if (isRelativePath(asf)) {
return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, asf)));
}
return asf;
})
.find(src => nodeResolvePath(src, path.dirname(currentFile), opts.extensions));
}
if (isRelativePath(aliasedSourceFile)) {
return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, aliasedSourceFile)));
}
if (process.env.NODE_ENV !== 'production') {
checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions, opts.loglevel);
}
return aliasedSourceFile;
}
const resolvers = [resolvePathFromAliasConfig, resolvePathFromRootConfig];
export default function resolvePath(sourcePath, currentFile, opts) {
// `.` and `./` are specific paths, we can't transfrom them
if (isRelativePath(sourcePath) || sourcePath === '.' || sourcePath === './') {
return sourcePath;
}
const normalizedOpts = normalizeOptions(currentFile, opts);
// File param is a relative path from the environment current working directory
// (not from cwd param)
const absoluteCurrentFile = path.resolve(currentFile);
let resolvedPath = null;
resolvers.some(resolver => {
resolvedPath = resolver(sourcePath, absoluteCurrentFile, normalizedOpts);
return resolvedPath !== null;
});
return resolvedPath;
}