|
166 | 166 | // modules in thier own context.
|
167 | 167 | Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0);
|
168 | 168 | Module._cache = {};
|
| 169 | + Module._pathCache = {}; |
169 | 170 | Module._extensions = {};
|
170 | 171 | Module._paths = [];
|
171 | 172 |
|
|
216 | 217 | // given a path check a the file exists with any of the set extensions
|
217 | 218 | function tryExtensions(p, extension) {
|
218 | 219 | for (var i = 0, EL = exts.length; i < EL; i++) {
|
219 |
| - f = tryFile(p + exts[i]); |
220 |
| - if (f) { return f; } |
| 220 | + var filename = tryFile(p + exts[i]); |
| 221 | + |
| 222 | + if (filename) { |
| 223 | + return filename; |
| 224 | + } |
221 | 225 | }
|
222 | 226 | return false;
|
223 | 227 | };
|
224 | 228 |
|
| 229 | + var cacheKey = JSON.stringify({request: request, paths: paths}); |
| 230 | + if (Module._pathCache[cacheKey]) { |
| 231 | + return Module._pathCache[cacheKey]; |
| 232 | + } |
| 233 | + |
225 | 234 | // For each path
|
226 | 235 | for (var i = 0, PL = paths.length; i < PL; i++) {
|
227 |
| - var p = paths[i], |
228 |
| - // try to join the request to the path |
229 |
| - f = tryFile(path.resolve(p, request)) || |
230 |
| - // try it with each of the extensions |
231 |
| - tryExtensions(path.resolve(p, request)) || |
232 |
| - // try it with each of the extensions at "index" |
233 |
| - tryExtensions(path.resolve(p, request, 'index')); |
234 |
| - if (f) { return f; } |
| 236 | + var basePath = path.resolve(paths[i], request); |
| 237 | + |
| 238 | + // try to join the request to the path |
| 239 | + var filename = tryFile(basePath); |
| 240 | + |
| 241 | + if (!filename) { |
| 242 | + // try it with each of the extensions |
| 243 | + filename = tryExtensions(basePath) |
| 244 | + } |
| 245 | + |
| 246 | + if (!filename) { |
| 247 | + // try it with each of the extensions at "index" |
| 248 | + filename = tryExtensions(path.resolve(basePath, 'index')) |
| 249 | + } |
| 250 | + |
| 251 | + if (filename) { |
| 252 | + Module._pathCache[cacheKey] = filename; |
| 253 | + return filename; |
| 254 | + } |
235 | 255 | }
|
236 | 256 | return false;
|
237 | 257 | }
|
|
0 commit comments