forked from ilearnio/module-alias
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
217 lines (182 loc) · 6.25 KB
/
index.js
File metadata and controls
217 lines (182 loc) · 6.25 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
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
"use strict"
let BuiltinModule = require("module");
let nodePath = require("path");
// Guard against poorly mocked module constructors
let Module = module.letructor.length > 1 ? module.letructor : BuiltinModule;
let modulePaths = [];
let moduleAliases = {};
let moduleAliasNames = [];
let oldNodeModulePaths = Module._nodeModulePaths;
Module._nodeModulePaths = (from) => {
let paths = oldNodeModulePaths.call(this, from)
// Only include the module path for top-level modules
// that were not installed:
if (from.indexOf("node_modules") === -1) {
paths = modulePaths.concat(paths)
}
return paths;
}
let oldResolveFilename = Module._resolveFilename;
Module._resolveFilename = (request, parentModule, isMain, options) => {
for (let i = moduleAliasNames.length; i-- > 0;) {
let alias = moduleAliasNames[i]
if (isPathMatchesAlias(request, alias)) {
let aliasTarget = moduleAliases[alias]
// Custom function handler
if (typeof moduleAliases[alias] === "function") {
let fromPath = parentModule.filename
aliasTarget = moduleAliases[alias](fromPath, request, alias)
if (!aliasTarget || typeof aliasTarget !== "string") {
throw new Error("[module-alias] Expecting custom handler function to return path.")
}
}
request = nodePath.join(aliasTarget, request.substr(alias.length));
// Only use the first match
break
}
}
return oldResolveFilename.call(this, request, parentModule, isMain, options)
}
let isPathMatchesAlias = (path, alias) => {
// Matching /^alias(\/|$)/
if (path.indexOf(alias) === 0) {
if (path.length === alias.length) return true
if (path[alias.length] === "/") return true
}
return false;
}
let addPathHelper = (path, targetArray) => {
path = nodePath.normalize(path)
if (targetArray && targetArray.indexOf(path) === -1) {
targetArray.unshift(path)
}
}
let removePathHelper = (path, targetArray) => {
if (targetArray) {
let index = targetArray.indexOf(path)
if (index !== -1) {
targetArray.splice(index, 1)
}
}
}
let addPath = (path) => {
let parent
path = nodePath.normalize(path)
if (modulePaths.indexOf(path) === -1) {
modulePaths.push(path)
// Enable the search path for the current top-level module
let mainModule = getMainModule()
if (mainModule) {
addPathHelper(path, mainModule.paths)
}
parent = module.parent;
// Also modify the paths of the module that was used to load the
// App-module-paths module and all of it's parents
while (parent && parent !== mainModule) {
addPathHelper(path, parent.paths)
parent = parent.parent
}
}
}
let addAliases = (aliases) => {
for (let alias in aliases) {
addAlias(alias, aliases[alias])
}
}
let addAlias = (alias, target) => {
moduleAliases[alias] = target
// Cost of sorting is lower here than during resolution
moduleAliasNames = Object.keys(moduleAliases)
moduleAliasNames.sort()
}
/**
* Reset any changes maded (resets all registered aliases
* and custom module directories)
* The function is undocumented and for testing purposes only
*/
let reset = () => {
let mainModule = getMainModule()
// Reset all changes in paths caused by addPath function
modulePaths.forEach(function(path) {
if (mainModule) {
removePathHelper(path, mainModule.paths)
}
// Delete from require.cache if the module has been required before.
// This is required for node >= 11
Object.getOwnPropertyNames(require.cache).forEach(function(name) {
if (name.indexOf(path) !== -1) {
delete require.cache[name]
}
})
let parent = module.parent
while (parent && parent !== mainModule) {
removePathHelper(path, parent.paths)
parent = parent.parent
}
})
modulePaths = [];
moduleAliases = {};
moduleAliasNames = [];
}
/**
* Import aliases from package.json
* @param {object} options
*/
let init = (options) => {
if (typeof options === "string") {
options = { base: options }
}
options = options || {};
let candidatePackagePaths;
if (options.base) {
candidatePackagePaths = [nodePath.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 = [nodePath.join(__dirname, "../.."), process.cwd()]
}
let npmPackage;
let base;
for (let i in candidatePackagePaths) {
try {
base = candidatePackagePaths[i]
npmPackage = require(nodePath.join(base, "package.json"))
break
} catch (e) {}
}
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 (aliases[alias][0] !== "/") {
aliases[alias] = nodePath.join(base, aliases[alias])
}
}
addAliases(aliases);
//
// Register custom module directories (like node_modules)
//
if (npmPackage._moduleDirectories instanceof Array) {
npmPackage._moduleDirectories.forEach(function(dir) {
if (dir === "node_modules") return
let modulePath = nodePath.join(base, dir)
addPath(modulePath)
})
}
}
let getMainModule = () => {
return require.main._simulateRepl ? undefined : require.main;
}
module.exports = init;
module.exports.addPath = addPath;
module.exports.addAlias = addAlias;
module.exports.addAliases = addAliases;
module.exports.isPathMatchesAlias = isPathMatchesAlias;
module.exports.reset = reset;