|
| 1 | +import * as path from 'path' // if module is locally defined we path.resolve it |
| 2 | +import callsite from 'callsite' |
| 3 | +import Module from 'module' |
| 4 | + |
| 5 | +function find(moduleName) { |
| 6 | + if (moduleName[0] === '.') { |
| 7 | + var stack = callsite() |
| 8 | + for (var i in stack) { |
| 9 | + var filename = stack[i].getFileName() |
| 10 | + // if (filename !== module.filename) { |
| 11 | + moduleName = path.resolve(path.dirname(filename), moduleName) |
| 12 | + break |
| 13 | + // } |
| 14 | + } |
| 15 | + } |
| 16 | + try { |
| 17 | + return __non_webpack_require__.resolve(moduleName) |
| 18 | + } catch (e) { |
| 19 | + return |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Removes a module from the cache. We need this to re-load our http_request ! |
| 25 | + * see: http://stackoverflow.com/a/14801711/1148249 |
| 26 | + */ |
| 27 | +function decache(moduleName) { |
| 28 | + moduleName = find(moduleName) |
| 29 | + |
| 30 | + if (!moduleName) { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Run over the cache looking for the files |
| 35 | + // loaded by the specified module name |
| 36 | + searchCache(moduleName, function(mod) { |
| 37 | + delete __non_webpack_require__.cache[mod.id] |
| 38 | + }) |
| 39 | + |
| 40 | + // Remove cached paths to the module. |
| 41 | + // Thanks to @bentael for pointing this out. |
| 42 | + Object.keys(Module.prototype.constructor._pathCache).forEach(function( |
| 43 | + cacheKey |
| 44 | + ) { |
| 45 | + if (cacheKey.indexOf(moduleName) > -1) { |
| 46 | + delete Module.prototype.constructor._pathCache[cacheKey] |
| 47 | + } |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Runs over the cache to search for all the cached |
| 53 | + * files |
| 54 | + */ |
| 55 | +function searchCache(moduleName, callback) { |
| 56 | + // Resolve the module identified by the specified name |
| 57 | + var mod = __non_webpack_require__.resolve(moduleName) |
| 58 | + var visited = {} |
| 59 | + |
| 60 | + // Check if the module has been resolved and found within |
| 61 | + // the cache no else so #ignore else http://git.io/vtgMI |
| 62 | + /* istanbul ignore else */ |
| 63 | + if (mod && (mod = __non_webpack_require__.cache[mod]) !== undefined) { |
| 64 | + // Recursively go over the results |
| 65 | + ;(function run(current) { |
| 66 | + visited[current.id] = true |
| 67 | + // Go over each of the module's children and |
| 68 | + // run over it |
| 69 | + current.children.forEach(function(child) { |
| 70 | + // ignore .node files, decachine native modules throws a |
| 71 | + // "module did not self-register" error on second require |
| 72 | + if (path.extname(child.filename) !== '.node' && !visited[child.id]) { |
| 73 | + run(child) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + // Call the specified callback providing the |
| 78 | + // found module |
| 79 | + callback(current) |
| 80 | + })(mod) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export default decache |
0 commit comments