-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathdefault_resolver.js
172 lines (145 loc) · 3.76 KB
/
default_resolver.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
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Path} from 'types/Config';
import browserResolve from 'browser-resolve';
type ResolverOptions = {|
basedir: Path,
browser?: boolean,
extensions?: Array<string>,
moduleDirectory?: Array<string>,
paths?: ?Array<Path>,
rootDir: ?Path,
|};
export default function defaultResolver(
path: Path,
options: ResolverOptions,
): Path {
const resolve = options.browser ? browserResolve.sync : resolveSync;
return resolve(path, {
basedir: options.basedir,
extensions: options.extensions,
moduleDirectory: options.moduleDirectory,
paths: options.paths,
rootDir: options.rootDir,
});
}
/*
* Adapted from: https://github.com/substack/node-resolve
*/
type ErrorWithCode = Error & {code?: string};
import fs from 'fs';
import path from 'path';
import isBuiltinModule from './is_builtin_module';
import nodeModulesPaths from './node_modules_paths';
const REGEX_RELATIVE_IMPORT = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/;
function resolveSync(target: Path, options: ResolverOptions): Path {
const basedir = options.basedir;
const extensions = options.extensions || ['.js'];
const paths = options.paths || [];
if (REGEX_RELATIVE_IMPORT.test(target)) {
// resolve relative import
const resolveTarget = path.resolve(basedir, target);
const result = tryResolve(resolveTarget);
if (result) {
return result;
}
} else {
// otherwise search for node_modules
const dirs = nodeModulesPaths(basedir, {
moduleDirectory: options.moduleDirectory,
paths,
});
for (let i = 0; i < dirs.length; i++) {
const resolveTarget = path.join(dirs[i], target);
const result = tryResolve(resolveTarget);
if (result) {
return result;
}
}
}
if (isBuiltinModule(target)) {
return target;
}
const err: ErrorWithCode = new Error(
"Cannot find module '" + target + "' from '" + basedir + "'",
);
err.code = 'MODULE_NOT_FOUND';
throw err;
/*
* contextual helper functions
*/
function tryResolve(name: Path): ?Path {
const dir = path.dirname(name);
let result;
if (isDirectory(dir)) {
result = resolveAsFile(name) || resolveAsDirectory(name);
}
return result;
}
function resolveAsFile(name: Path): ?Path {
if (isFile(name)) {
return name;
}
for (let i = 0; i < extensions.length; i++) {
const file = name + extensions[i];
if (isFile(file)) {
return file;
}
}
return undefined;
}
function resolveAsDirectory(name: Path): ?Path {
if (!isDirectory(name)) {
return undefined;
}
const pkgfile = path.join(name, 'package.json');
let pkgmain;
try {
const body = fs.readFileSync(pkgfile, 'utf8');
pkgmain = JSON.parse(body).main;
} catch (e) {}
if (pkgmain) {
const resolveTarget = path.resolve(name, pkgmain);
const result = tryResolve(resolveTarget);
if (result) {
return result;
}
}
return resolveAsFile(path.join(name, 'index'));
}
}
/*
* helper functions
*/
function isFile(file: Path): boolean {
let result;
try {
const stat = fs.statSync(file);
result = stat.isFile() || stat.isFIFO();
} catch (e) {
if (!(e && e.code === 'ENOENT')) {
throw e;
}
result = false;
}
return result;
}
function isDirectory(dir: Path): boolean {
let result;
try {
const stat = fs.statSync(dir);
result = stat.isDirectory();
} catch (e) {
if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
throw e;
}
result = false;
}
return result;
}