-
Notifications
You must be signed in to change notification settings - Fork 13
Description
Currently when a module with child dependencies is removed from the require cache via decache, all of its child dependencies are also removed from the cache.
For example:
When requiring a module that requires several other modules.
a.js
-- b.js
-- c.js
-- d.js
The require.cache will then contain references to each module.
e.g. Object.keys(require.cache) will look like:
[ '/Users/my-user/Documents/git/decache/my-test-file.js',
'/Users/my-user/Documents/git/decache/decache.js',
'/Users/my-user/Documents/git/decache/node_modules/callsite/index.js',
'/Users/my-user/Documents/git/decache/a.js',
'/Users/my-user/Documents/git/decache/b.js',
'/Users/my-user/Documents/git/decache/c.js',
'/Users/my-user/Documents/git/decache/d.js',
'/Users/my-user/Documents/git/decache/e.js' ]
after calling decache on the a.js module
decache('./a')
The require cache will no longer contain references to the a.js module specified or any of its dependencies
e.g. Object.keys(require.cache) will look like:
[ '/Users/my-user/Documents/git/decache/my-test-file.js',
'/Users/my-user/Documents/git/decache/decache.js',
'/Users/my-user/Documents/git/decache/node_modules/callsite/index.js' ]
This is due to the fact that the decache module recursively DFS the target modules dependency tree. Deleting each module passed by the callback iterator.
// Run over the cache looking for the files
// loaded by the specified module name
require.searchCache(moduleName, function (mod) {
delete require.cache[mod.id];
});
....
require.searchCache = function (moduleName, callback) {
// Resolve the module identified by the specified name
var mod = require.resolve(moduleName);
// Check if the module has been resolved and found within
// the cache no else so #ignore else http://git.io/vtgMI
/* istanbul ignore else */
if (mod && ((mod = require.cache[mod]) !== undefined)) {
// Recursively go over the results
(function run(mod) {
// Go over each of the module's children and
// run over it
mod.children.forEach(function (child) {
run(child);
});
// Call the specified callback providing the
// found module
callback(mod);
})(mod);
}
};
Is this behavior intended? Should the DFS instead be removing all references to child.parent? I would assume that some people using this module are depending on this behavior or have worked around it. Curious as to thoughts on this.
Thanks!