-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathresolve.ts
316 lines (270 loc) · 9.02 KB
/
resolve.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import * as path from 'path';
import { BaseException } from '../src';
import { isFile } from './fs';
/**
* Exception thrown when a module could not be resolved.
* @deprecated since version 8. Use `MODULE_NOT_FOUND` Node error code instead.
*/
export class ModuleNotFoundException extends BaseException {
public readonly code: string;
constructor(public readonly moduleName: string, public readonly basePath: string) {
super(`Could not find module ${JSON.stringify(moduleName)} from ${JSON.stringify(basePath)}.`);
this.code = 'MODULE_NOT_FOUND';
}
}
/**
* Returns a list of all the callers from the resolve() call.
* @returns {string[]}
* @private
*/
function _caller(): string[] {
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
const error = Error as {} as { prepareStackTrace: (x: {}, stack: {}) => {} };
const origPrepareStackTrace = error.prepareStackTrace;
error.prepareStackTrace = (_, stack) => stack;
const stack = (new Error()).stack as {} | undefined as { getFileName(): string }[] | undefined;
error.prepareStackTrace = origPrepareStackTrace;
return stack ? stack.map(x => x.getFileName()).filter(x => !!x) : [];
}
/**
* Get the global directory for node_modules. This is based on NPM code itself, and may be subject
* to change, but is relatively stable.
* @returns {string} The path to node_modules itself.
* @private
*/
function _getGlobalNodeModules() {
let globalPrefix;
if (process.env.PREFIX) {
globalPrefix = process.env.PREFIX;
} else if (process.platform === 'win32') {
// c:\node\node.exe --> prefix=c:\node\
globalPrefix = path.dirname(process.execPath);
} else {
// /usr/local/bin/node --> prefix=/usr/local
globalPrefix = path.dirname(path.dirname(process.execPath));
// destdir only is respected on Unix
const destdir = process.env.DESTDIR;
if (destdir) {
globalPrefix = path.join(destdir, globalPrefix);
}
}
return (process.platform !== 'win32')
? path.resolve(globalPrefix || '', 'lib', 'node_modules')
: path.resolve(globalPrefix || '', 'node_modules');
}
/** @deprecated since version 8. Use `require.resolve` instead. */
export interface ResolveOptions {
/**
* The basedir to use from which to resolve.
*/
basedir: string;
/**
* The list of extensions to resolve. By default uses Object.keys(require.extensions).
*/
extensions?: string[];
/**
* An additional list of paths to look into.
*/
paths?: string[];
/**
* Whether or not to preserve symbolic links. If false, the actual paths pointed by
* the symbolic links will be used. This defaults to true.
*/
preserveSymlinks?: boolean;
/**
* Whether to fallback to a global lookup if the basedir one failed.
*/
checkGlobal?: boolean;
/**
* Whether to fallback to using the local caller's directory if the basedir failed.
*/
checkLocal?: boolean;
/**
* Whether to only resolve and return the first package.json file found. By default,
* resolves the main field or the index of the package.
*/
resolvePackageJson?: boolean;
}
let _resolveHook: ((x: string, options: ResolveOptions) => string | null) | null = null;
/** @deprecated since version 8. Use `require.resolve` instead. */
export function setResolveHook(
hook: ((x: string, options: ResolveOptions) => string | null) | null,
) {
_resolveHook = hook;
}
/**
* Resolve a package using a logic similar to npm require.resolve, but with more options.
* @param packageName The package name to resolve.
* @param options A list of options. See documentation of those options.
* @returns {string} Path to the index to include, or if `resolvePackageJson` option was
* passed, a path to that file.
* @throws {ModuleNotFoundException} If no module with that name was found anywhere.
* @deprecated since version 8. Use `require.resolve` instead.
*/
export function resolve(packageName: string, options: ResolveOptions): string {
if (_resolveHook) {
const maybe = _resolveHook(packageName, options);
if (maybe) {
return maybe;
}
}
const readFileSync = fs.readFileSync;
const extensions: string[] = options.extensions || Object.keys(require.extensions);
const basePath = options.basedir;
options.paths = options.paths || [];
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\/\\])/.test(packageName)) {
let res = path.resolve(basePath, packageName);
if (packageName === '..' || packageName.slice(-1) === '/') {
res += '/';
}
const m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) {
return m;
}
} else {
const n = loadNodeModulesSync(packageName, basePath);
if (n) {
return n;
}
}
// Fallback to checking the local (callee) node modules.
if (options.checkLocal) {
const callers = _caller();
for (const caller of callers) {
const localDir = path.dirname(caller);
if (localDir !== options.basedir) {
try {
return resolve(packageName, {
...options,
checkLocal: false,
checkGlobal: false,
basedir: localDir,
});
} catch (e) {
// Just swap the basePath with the original call one.
if (!(e instanceof ModuleNotFoundException)) {
throw e;
}
}
}
}
}
// Fallback to checking the global node modules.
if (options.checkGlobal) {
const globalDir = path.dirname(_getGlobalNodeModules());
if (globalDir !== options.basedir) {
try {
return resolve(packageName, {
...options,
checkLocal: false,
checkGlobal: false,
basedir: globalDir,
});
} catch (e) {
// Just swap the basePath with the original call one.
if (!(e instanceof ModuleNotFoundException)) {
throw e;
}
}
}
}
throw new ModuleNotFoundException(packageName, basePath);
function loadAsFileSync(x: string): string | null {
if (isFile(x)) {
return x;
}
return extensions.map(ex => x + ex).find(f => isFile(f)) || null;
}
function loadAsDirectorySync(x: string): string | null {
const pkgfile = path.join(x, 'package.json');
if (isFile(pkgfile)) {
if (options.resolvePackageJson) {
return pkgfile;
}
try {
const body = readFileSync(pkgfile, 'UTF8');
const pkg = JSON.parse(body);
if (pkg['main']) {
if (pkg['main'] === '.' || pkg['main'] === './') {
pkg['main'] = 'index';
}
const m = loadAsFileSync(path.resolve(x, pkg['main']));
if (m) {
return m;
}
const n = loadAsDirectorySync(path.resolve(x, pkg['main']));
if (n) {
return n;
}
}
} catch {}
}
return loadAsFileSync(path.join(x, '/index'));
}
function loadNodeModulesSync(x: string, start: string): string | null {
const dirs = nodeModulesPaths(start, options);
for (const dir of dirs) {
const m = loadAsFileSync(path.join(dir, x));
if (m) {
return m;
}
const n = loadAsDirectorySync(path.join(dir, x));
if (n) {
return n;
}
}
return null;
}
function nodeModulesPaths(start: string, opts: ResolveOptions) {
const modules = ['node_modules'];
if (process.env.BAZEL_TARGET) {
// When running test under Bazel, node_modules have to be resolved
// differently. node_modules are installed in the `npm` workspace.
// For more info, see `yarn_install` rule in WORKSPACE file.
modules.push(path.join('npm', 'node_modules'));
}
// ensure that `start` is an absolute path at this point,
// resolving against the process' current working directory
let absoluteStart = path.resolve(start);
if (opts && opts.preserveSymlinks === false) {
try {
absoluteStart = fs.realpathSync(absoluteStart);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}
let prefix = '/';
if (/^([A-Za-z]:)/.test(absoluteStart)) {
prefix = '';
} else if (/^\\\\/.test(absoluteStart)) {
prefix = '\\\\';
}
const paths = [absoluteStart];
let parsed = path.parse(absoluteStart);
while (parsed.dir !== paths[paths.length - 1]) {
paths.push(parsed.dir);
parsed = path.parse(parsed.dir);
}
const dirs = paths.reduce((dirs: string[], aPath: string) => {
return dirs.concat(modules.map(function (moduleDir) {
return path.join(prefix, aPath, moduleDir);
}));
}, []);
if (process.env.NG_TEMP_MODULES_DIR) {
// When running from a temporary installations, node_modules have to be resolved
// differently and they should be prefered over others.
dirs.unshift(process.env.NG_TEMP_MODULES_DIR);
}
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
}
}