-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathindex.js
229 lines (201 loc) · 6.73 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
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
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
import type {Path} from 'types/Config';
import type ModuleMap from '../../jest-haste-map/src/ModuleMap';
const nodeModulesPaths = require('resolve/lib/node-modules-paths');
const path = require('path');
const resolve = require('resolve');
const browserResolve = require('browser-resolve');
type ResolverConfig = {
browser?: boolean,
defaultPlatform: ?string,
extensions: Array<string>,
hasCoreModules: boolean,
moduleDirectories: Array<string>,
moduleNameMapper: ?{[key: string]: RegExp},
modulePaths: Array<Path>,
platforms: Array<string>,
};
type FindNodeModuleConfig = {
basedir: Path,
browser?: boolean,
extensions: Array<string>,
moduleDirectory: Array<string>,
paths?: Array<Path>,
};
export type ResolveModuleConfig = {skipNodeResolution?: boolean};
const NATIVE_PLATFORM = 'native';
const nodePaths =
(process.env.NODE_PATH ? process.env.NODE_PATH.split(path.delimiter) : null);
class Resolver {
_options: ResolverConfig;
_moduleMap: ModuleMap;
_moduleNameCache: {[name: string]: Path};
_modulePathCache: {[path: Path]: Array<Path>};
constructor(moduleMap: ModuleMap, options: ResolverConfig) {
this._options = {
browser: options.browser,
defaultPlatform: options.defaultPlatform,
extensions: options.extensions,
hasCoreModules:
options.hasCoreModules === undefined ? true : options.hasCoreModules,
moduleDirectories: options.moduleDirectories || ['node_modules'],
moduleNameMapper: options.moduleNameMapper,
modulePaths: options.modulePaths,
platforms: options.platforms,
};
this._moduleMap = moduleMap;
this._moduleNameCache = Object.create(null);
this._modulePathCache = Object.create(null);
}
static findNodeModule(path: Path, options: FindNodeModuleConfig): ?Path {
const paths = options.paths;
try {
const resv = options.browser ? browserResolve : resolve;
return resv.sync(
path,
{
basedir: options.basedir,
extensions: options.extensions,
moduleDirectory: options.moduleDirectory,
paths: paths ? (nodePaths || []).concat(paths) : nodePaths,
},
);
} catch (e) {}
return null;
}
resolveModule(
from: Path,
moduleName: string,
options?: ResolveModuleConfig,
): Path {
const dirname = path.dirname(from);
const paths = this._options.modulePaths;
const extensions = this._options.extensions;
const moduleDirectory = this._options.moduleDirectories;
const key = dirname + path.delimiter + moduleName;
// 0. If we have already resolved this module for this directory name,
// return a value from the cache.
if (this._moduleNameCache[key]) {
return this._moduleNameCache[key];
}
// 1. Check if the module is a haste module.
let module = this.getModule(moduleName);
if (module) {
return this._moduleNameCache[key] = module;
}
// 2. Check if the module is a node module and resolve it based on
// the node module resolution algorithm.
if (!options || !options.skipNodeResolution) {
module = Resolver.findNodeModule(moduleName, {
basedir: dirname,
browser: this._options.browser,
extensions,
moduleDirectory,
paths,
});
if (module) {
return this._moduleNameCache[key] = module;
}
}
// 3. Resolve "haste packages" which are `package.json` files outside of
// `node_modules` folders anywhere in the file system.
const parts = moduleName.split('/');
module = this.getPackage(parts.shift());
if (module) {
try {
return this._moduleNameCache[key] = require.resolve(
path.join.apply(path, [path.dirname(module)].concat(parts)),
);
} catch (ignoredError) {}
}
// 4. Throw an error if the module could not be found. `resolve.sync`
// only produces an error based on the dirname but we have the actual
// current module name available.
const relativePath = path.relative(dirname, from);
const err = new Error(
`Cannot find module '${moduleName}' from '${relativePath || '.'}'`,
);
(err: any).code = 'MODULE_NOT_FOUND';
throw err;
}
isCoreModule(moduleName: string): boolean {
return this._options.hasCoreModules && resolve.isCore(moduleName);
}
getModule(name: string): ?Path {
return this._moduleMap.getModule(
name,
this._options.defaultPlatform,
this._supportsNativePlatform(),
);
}
getPackage(name: string): ?Path {
return this._moduleMap.getPackage(
name,
this._options.defaultPlatform,
this._supportsNativePlatform(),
);
}
getMockModule(from: Path, name: string): ?Path {
const mock = this._moduleMap.getMockModule(name);
if (mock) {
return mock;
} else {
const moduleName = this._resolveStubModuleName(from, name);
if (moduleName) {
return this.getModule(moduleName) || moduleName;
}
}
return null;
}
getModulePaths(from: Path): Array<Path> {
if (!this._modulePathCache[from]) {
const moduleDirectory = this._options.moduleDirectories;
const paths = nodeModulesPaths(from, {moduleDirectory});
if (paths[paths.length - 1] === undefined) {
// circumvent node-resolve bug that adds `undefined` as last item.
paths.pop();
}
this._modulePathCache[from] = paths;
}
return this._modulePathCache[from];
}
_resolveStubModuleName(from: Path, moduleName: string): ?Path {
const dirname = path.dirname(from);
const paths = this._options.modulePaths;
const extensions = this._options.extensions;
const moduleDirectory = this._options.moduleDirectories;
const moduleNameMapper = this._options.moduleNameMapper;
if (moduleNameMapper) {
for (const mappedModuleName in moduleNameMapper) {
const regex = moduleNameMapper[mappedModuleName];
if (regex.test(moduleName)) {
moduleName = moduleName.replace(regex, mappedModuleName);
return this.getModule(moduleName) || Resolver.findNodeModule(
moduleName,
{
basedir: dirname,
browser: this._options.browser,
extensions,
moduleDirectory,
paths,
},
);
}
}
}
return null;
}
_supportsNativePlatform() {
return (this._options.platforms || []).indexOf(NATIVE_PLATFORM) !== -1;
}
}
module.exports = Resolver;