-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
287 lines (249 loc) · 8.88 KB
/
index.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
import Path from 'path';
export type FuncNodeModulePaths = (from: string) => string[]
export type FuncResolveFileName = (request: string, parent: NodeModule, ...args: unknown[]) => string
export interface ModuleAliasDict {
[key: string]: ((from: string, request: string, alias: string) => string) | string
}
export type NodeModuleConstructor =
(new (...args: any[]) => NodeModule)
& {
_cache: { [key: string]: NodeModule }
_contextLoad: boolean
_debug: unknown
_extensions: { [key: string]: string }
_findPath: (request: string, paths: string[]) => string | false
_load: (request: string, parent: NodeModule, isMain: boolean) => unknown
_nodeModulePaths: FuncNodeModulePaths
_pathCache: { [key: string]: string }
_realPathCache: unknown
_resolveFilename: FuncResolveFileName
_resolveLookupPaths: (request: string, parent: NodeModule) => [string, string[]]
_moduleAlias?: ModuleAlias
globalPaths: string[]
wrapper: unknown
wrap: unknown
}
export interface ModuleAliasOptions {
base?: string
}
/**
* Implements the module-alias library, which allows programmatic configuration of module resolution rules,
* as a transient class which applies to the calling module.
*/
export class ModuleAlias {
/**
* The module used by the module resolver.
*/
private module: NodeModule
/**
* The module class used by the module resolver.
*/
private ModuleCtor: NodeModuleConstructor
/**
* Captured instance of the old _nodeModulePaths function.
*/
private oldNodeModulePaths: FuncNodeModulePaths
/**
* Captured instance of the old _resolveFilename function
*/
private oldResolveFilename: FuncResolveFileName
/**
* A list of paths for which modules are searched.
*/
private modulePaths: string[] = [];
/**
* A dictionary of aliases and their expanded path representations.
*/
private moduleAliases: ModuleAliasDict = {};
/**
* A list of aliases.
*/
private moduleAliasNames: string[] = [];
constructor (parent?: NodeModule, givenOptions?: ModuleAliasOptions | string) {
// Guard against poorly mocked module constructors
if (parent) {
this.module = parent;
this.ModuleCtor = parent.constructor as NodeModuleConstructor;
} else if (module.parent) {
this.module = module.parent;
this.ModuleCtor = module.parent.constructor as NodeModuleConstructor;
} else {
throw new Error('Failed to initialize ts-module-alias. `parent` was not defined and a suitable substitute could not be found.');
}
// Guard against initializing on the same module
if (this.ModuleCtor._moduleAlias) {
return this.ModuleCtor._moduleAlias;
}
this.oldNodeModulePaths = this.ModuleCtor._nodeModulePaths;
this.oldResolveFilename = this.ModuleCtor._resolveFilename;
const self = this;
this.ModuleCtor._nodeModulePaths = (from) => self.nodeModulePaths(self, from);
this.ModuleCtor._resolveFilename = (request, parent, ...args) => self.resolveFilename(self, request, parent, ...args);
let options: ModuleAliasOptions;
if (typeof givenOptions === 'string') {
options = { base: givenOptions };
} else {
options = givenOptions || {};
}
let candidatePackagePaths;
if (options.base) {
candidatePackagePaths = [Path.resolve(options.base.replace(/\/package\.json$/, ''))]
} else {
// There is probably 99% chance that the project root directory in located
// above the node_modules directory,
// Or that package.json is in the node process' current working directory (when
// running a package manager script, e.g. `yarn start` / `npm run start`)
candidatePackagePaths = [Path.join(__dirname, '../..'), process.cwd()];
}
let npmPackage;
let base: string;
for (let i in candidatePackagePaths) {
try {
base = candidatePackagePaths[i];
npmPackage = require(Path.join(base, 'package.json'));
break;
} catch (e) {
// noop
}
}
base = base || __dirname;
if (typeof npmPackage !== 'object') {
let pathString = candidatePackagePaths.join(',\n');
throw new Error(`Unable to find package.json in any of:\n[${pathString}]`);
}
//
// Import aliases
//
let aliases = {
...npmPackage._moduleAliases
}
for (let alias in aliases) {
if (Path.isAbsolute(aliases[alias])) {
aliases[alias] = Path.join(base, aliases[alias]);
}
}
this.addAliases(aliases);
//
// Register custom module directories (like node_modules)
//
if (npmPackage._moduleDirectories instanceof Array) {
npmPackage._moduleDirectories.forEach((dir: string) => {
if (dir === 'node_modules') return;
let modulePath = Path.join(base, dir);
this.addPath(modulePath);
});
}
this.ModuleCtor._moduleAlias = this;
}
/**
* Adds a path to the source directories to search for modules
*
* @param path The path which should be used as a source directory
*/
addPath (path: string): void {
path = Path.normalize(path);
if (this.modulePaths.indexOf(path) === -1) {
this.modulePaths.unshift(path);
this.addPathHelper(path, this.module.paths);
}
}
/**
* Adds an alias to a path
*
* @param alias Path alias
* @param target Actual path
*/
addAlias (alias: string, target: string) {
this.moduleAliases[alias] = target;
// Cost of sorting is lower here than during resolution
this.moduleAliasNames = Object.keys(this.moduleAliases);
this.moduleAliasNames.sort();
}
/**
* Adds many aliases to paths
*
* @param aliases An object containing paths keyed by their alias.
*/
addAliases (aliases: { [key: string]: string }) {
for (let alias in aliases) {
this.addAlias(alias, aliases[alias]);
}
}
/**
* Checks if the import request matches to an alias
* @param path Import request
* @param alias A path alias
*/
isPathMatchesAlias (path: string, alias: string): boolean {
// Matching /^alias(\/|$)/
return path.indexOf(alias) === 0
&& (path.length === alias.length || path[alias.length] === '/');
}
/**
* Normalizes a path and adds it to an array if it is not already present
* NOTE: feels redundant, idk, might delete later
*
* @param path The path to be added to an array
* @param targetArray The array to which the path should be added
*/
private addPathHelper (path: string, targetArray: string[]) {
path = Path.normalize(path);
if (targetArray && targetArray.indexOf(path) === -1) {
targetArray.unshift(path);
}
}
/**
* Overloads the _nodeModulePaths function on the module system
*
* @param this unknown
* @param self Instance of current ModuleAlias class
* @param from The path of the requesting module
*/
private nodeModulePaths(this: unknown, self: this, from: string): string[] {
let paths = self.oldNodeModulePaths.call(this, from);
// Only include the module path for top-level modules
// that were not installed:
if (from.indexOf('node_modules') === -1) {
paths = self.modulePaths.concat(paths);
}
return paths;
}
/**
* Overloads the _resolveFilename function on the module system
*
* @param this unknown
* @param self Instance of current ModuleAlias class
* @param request Import request string e.g. 'path', 'fs' or 'src/path/to/file'
* @param parent The module making this request
* @param args Further arguments, irrelevant to what this is doing
*/
private resolveFilename(this: unknown, self: this, request: string, parent: NodeModule, ...args: unknown[]): string {
for (let i = self.moduleAliasNames.length; i-- > 0;) {
let alias = self.moduleAliasNames[i];
if (self.isPathMatchesAlias(request, alias)) {
let aliasTarget = self.moduleAliases[alias];
// Custom function handler
if (typeof self.moduleAliases[alias] === 'function') {
let fromPath = parent.filename;
aliasTarget = (self.moduleAliases[alias] as any)(fromPath, request, alias);
if (!aliasTarget || typeof aliasTarget !== 'string') {
throw new Error('[module-alias] Expecting custom handler function to return path.');
}
}
request = Path.join(aliasTarget as string, request.substr(alias.length));
// Only use the first match
break;
}
}
// Modify parent before passing to guarantee that module paths are included.
// This is kind of a hack to fix an issue with parent paths not populating correctly after hot-module reload.
// Cause of actual bug is unknown, but this makes things work.
const paths: { [key: string]: boolean } = {};
for (let path of [...self.modulePaths, ...parent.paths]) {
paths[path] = true;
}
parent.paths = Object.keys(paths);
return self.oldResolveFilename.call(this, request, parent, ...args);
}
}
export default ModuleAlias;