|
| 1 | +var Module = require('module'), |
| 2 | + assert = require('assert').ok, |
| 3 | + fs = require('fs'), |
| 4 | + path = require('path'); |
| 5 | + |
| 6 | +var moduleCache = {}, |
| 7 | + options; |
| 8 | + |
| 9 | +function hasOwnProperty(obj, prop) { |
| 10 | + return Object.prototype.hasOwnProperty.call(obj, prop); |
| 11 | +} |
| 12 | + |
| 13 | +function statPath(path) { |
| 14 | + try { |
| 15 | + return fs.statSync(path); |
| 16 | + } catch (ex) {} |
| 17 | + return false; |
| 18 | +} |
| 19 | + |
| 20 | +// check if the directory is a package.json dir |
| 21 | +var packageMainCache = {}; |
| 22 | + |
| 23 | +function readPackage(requestPath) { |
| 24 | + if (hasOwnProperty(packageMainCache, requestPath)) { |
| 25 | + return packageMainCache[requestPath]; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + var jsonPath = path.resolve(requestPath, 'package.json'); |
| 30 | + var json = fs.readFileSync(jsonPath, 'utf8'); |
| 31 | + } catch (e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + var pkg = packageMainCache[requestPath] = JSON.parse(json); |
| 37 | + } catch (e) { |
| 38 | + e.path = jsonPath; |
| 39 | + e.message = 'Error parsing ' + jsonPath + ': ' + e.message; |
| 40 | + throw e; |
| 41 | + } |
| 42 | + return pkg; |
| 43 | +} |
| 44 | + |
| 45 | +function tryPackage(requestPath, exts) { |
| 46 | + var pkg = readPackage(requestPath); |
| 47 | + |
| 48 | + if (!(pkg && pkg.main)) return false; |
| 49 | + |
| 50 | + var name = pkg.name, |
| 51 | + version = pkg.version, |
| 52 | + main = pkg.main; |
| 53 | + |
| 54 | + var cache = moduleCache[name]; |
| 55 | + if(!cache){ |
| 56 | + cache = moduleCache[name] = {}; |
| 57 | + } |
| 58 | + |
| 59 | + var filename = cache[version]; |
| 60 | + |
| 61 | + if(!filename) { |
| 62 | + filename = path.resolve(requestPath, main); |
| 63 | + cache[version] = filename; |
| 64 | + } |
| 65 | + |
| 66 | + return tryFile(filename) || tryExtensions(filename, exts) || |
| 67 | + tryExtensions(path.resolve(filename, 'index'), exts); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +// check if the file exists and is not a directory |
| 72 | +function tryFile(requestPath) { |
| 73 | + var stats = statPath(requestPath); |
| 74 | + if (stats && !stats.isDirectory()) { |
| 75 | + return fs.realpathSync(requestPath, Module._realpathCache); |
| 76 | + } |
| 77 | + return false; |
| 78 | +} |
| 79 | + |
| 80 | +// given a path check a the file exists with any of the set extensions |
| 81 | +function tryExtensions(p, exts) { |
| 82 | + for (var i = 0, EL = exts.length; i < EL; i++) { |
| 83 | + var filename = tryFile(p + exts[i]); |
| 84 | + |
| 85 | + if (filename) { |
| 86 | + return filename; |
| 87 | + } |
| 88 | + } |
| 89 | + return false; |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = { |
| 93 | + init: function(_options){ |
| 94 | + if(options) { |
| 95 | + return; |
| 96 | + } |
| 97 | + options = _options||{}; |
| 98 | + Module._findPath = function(request, paths) { |
| 99 | + var exts = Object.keys(Module._extensions); |
| 100 | + |
| 101 | + if (request.charAt(0) === '/') { |
| 102 | + paths = ['']; |
| 103 | + } |
| 104 | + |
| 105 | + var trailingSlash = (request.slice(-1) === '/'); |
| 106 | + |
| 107 | + var cacheKey = JSON.stringify({request: request, paths: paths}); |
| 108 | + if (Module._pathCache[cacheKey]) { |
| 109 | + return Module._pathCache[cacheKey]; |
| 110 | + } |
| 111 | + |
| 112 | + // For each path |
| 113 | + for (var i = 0, PL = paths.length; i < PL; i++) { |
| 114 | + var basePath = path.resolve(paths[i], request); |
| 115 | + var filename; |
| 116 | + |
| 117 | + if (!trailingSlash) { |
| 118 | + // try to join the request to the path |
| 119 | + filename = tryFile(basePath); |
| 120 | + |
| 121 | + if (!filename && !trailingSlash) { |
| 122 | + // try it with each of the extensions |
| 123 | + filename = tryExtensions(basePath, exts); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (!filename) { |
| 128 | + filename = tryPackage(basePath, exts); |
| 129 | + } |
| 130 | + |
| 131 | + if (!filename) { |
| 132 | + filename = tryExtensions(path.resolve(basePath, 'index'), exts); |
| 133 | + } |
| 134 | + |
| 135 | + if (filename) { |
| 136 | + Module._pathCache[cacheKey] = filename; |
| 137 | + return filename; |
| 138 | + } |
| 139 | + } |
| 140 | + return false; |
| 141 | + }; |
| 142 | + }, |
| 143 | + moduleCache: moduleCache |
| 144 | +} |
0 commit comments