Resolver.js
5.87 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
218
219
220
221
222
223
224
225
226
227
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var Tapable = require("tapable");
var createInnerCallback = require("./createInnerCallback");
function Resolver(fileSystem) {
Tapable.call(this);
this.fileSystem = fileSystem;
}
module.exports = Resolver;
Resolver.prototype = Object.create(Tapable.prototype);
Resolver.prototype.constructor = Resolver;
Resolver.prototype.resolveSync = function resolveSync(context, path, request) {
var err, result, sync = false;
this.resolve(context, path, request, function(e, r) {
err = e;
result = r;
sync = true;
});
if(!sync) throw new Error("Cannot 'resolveSync' because the fileSystem is not sync. Use 'resolve'!");
if(err) throw err;
return result;
};
Resolver.prototype.resolve = function resolve(context, path, request, callback) {
if(arguments.length === 3) {
throw new Error("Signature changed: context parameter added");
}
var resolver = this;
var obj = {
context: context,
path: path,
request: request
};
var localMissing;
var log;
var message = "resolve '" + request + "' in '" + path + "'";
function writeLog(msg) {
log.push(msg);
}
function logAsString() {
return log.join("\n");
}
function onError(err, result) {
if(callback.log) {
for(var i = 0; i < log.length; i++)
callback.log(log[i]);
}
if(err) return callback(err);
var error = new Error("Can't " + message);
error.details = logAsString();
error.missing = localMissing;
resolver.applyPlugins("no-resolve", obj, error);
return callback(error);
}
function onResolve(err, result) {
if(!err && result) {
return callback(null, result.path === false ? false : result.path + (result.query || ""), result);
}
localMissing = [];
log = [];
return resolver.doResolve("resolve", obj, message, createInnerCallback(onError, {
log: writeLog,
missing: localMissing,
stack: callback.stack
}));
}
onResolve.missing = callback.missing;
onResolve.stack = callback.stack;
return this.doResolve("resolve", obj, message, onResolve);
};
Resolver.prototype.doResolve = function doResolve(type, request, message, callback) {
var resolver = this;
var stackLine = type + ": (" + request.path + ") " +
(request.request || "") + (request.query || "") +
(request.directory ? " directory" : "") +
(request.module ? " module" : "");
var newStack = [stackLine];
if(callback.stack) {
newStack = callback.stack.concat(newStack);
if(callback.stack.indexOf(stackLine) >= 0) {
// Prevent recursion
var recursionError = new Error("Recursion in resolving\nStack:\n " + newStack.join("\n "));
recursionError.recursion = true;
if(callback.log) callback.log("abort resolving because of recursion");
return callback(recursionError);
}
}
resolver.applyPlugins("resolve-step", type, request);
var beforePluginName = "before-" + type;
if(resolver.hasPlugins(beforePluginName)) {
resolver.applyPluginsAsyncSeriesBailResult1(beforePluginName, request, createInnerCallback(beforeInnerCallback, {
log: callback.log,
missing: callback.missing,
stack: newStack
}, message && ("before " + message), true));
} else {
runNormal();
}
function beforeInnerCallback(err, result) {
if(arguments.length > 0) {
if(err) return callback(err);
if(result) return callback(null, result);
return callback();
}
runNormal();
}
function runNormal() {
if(resolver.hasPlugins(type)) {
return resolver.applyPluginsAsyncSeriesBailResult1(type, request, createInnerCallback(innerCallback, {
log: callback.log,
missing: callback.missing,
stack: newStack
}, message));
} else {
runAfter();
}
}
function innerCallback(err, result) {
if(arguments.length > 0) {
if(err) return callback(err);
if(result) return callback(null, result);
return callback();
}
runAfter();
}
function runAfter() {
var afterPluginName = "after-" + type;
if(resolver.hasPlugins(afterPluginName)) {
return resolver.applyPluginsAsyncSeriesBailResult1(afterPluginName, request, createInnerCallback(afterInnerCallback, {
log: callback.log,
missing: callback.missing,
stack: newStack
}, message && ("after " + message), true));
} else {
callback();
}
}
function afterInnerCallback(err, result) {
if(arguments.length > 0) {
if(err) return callback(err);
if(result) return callback(null, result);
return callback();
}
return callback();
}
};
Resolver.prototype.parse = function parse(identifier) {
if(identifier === "") return null;
var part = {
request: "",
query: "",
module: false,
directory: false,
file: false
};
var idxQuery = identifier.indexOf("?");
if(idxQuery === 0) {
part.query = identifier;
} else if(idxQuery > 0) {
part.request = identifier.slice(0, idxQuery);
part.query = identifier.slice(idxQuery);
} else {
part.request = identifier;
}
if(part.request) {
part.module = this.isModule(part.request);
part.directory = this.isDirectory(part.request);
if(part.directory) {
part.request = part.request.substr(0, part.request.length - 1);
}
}
return part;
};
var notModuleRegExp = /^\.$|^\.[\\\/]|^\.\.$|^\.\.[\/\\]|^\/|^[A-Z]:[\\\/]/i;
Resolver.prototype.isModule = function isModule(path) {
return !notModuleRegExp.test(path);
};
var directoryRegExp = /[\/\\]$/i;
Resolver.prototype.isDirectory = function isDirectory(path) {
return directoryRegExp.test(path);
};
var memoryFsJoin = require("memory-fs/lib/join");
var memoizedJoin = new Map();
Resolver.prototype.join = function(path, request) {
var cacheEntry;
var pathCache = memoizedJoin.get(path);
if(typeof pathCache === "undefined") {
memoizedJoin.set(path, pathCache = new Map());
} else {
cacheEntry = pathCache.get(request);
if(typeof cacheEntry !== "undefined")
return cacheEntry;
}
cacheEntry = memoryFsJoin(path, request);
pathCache.set(request, cacheEntry);
return cacheEntry;
};
Resolver.prototype.normalize = require("memory-fs/lib/normalize");