dwyl / dwyl/decache

Decache removing all child dependencies of target module from cache

Open
#29 5 comments 0 reactions 0 assignees View on GitHub
documentation help wanted question T25m
Dominant language
JavaScript
Stars
153
Forks
12
PR merge metrics
No merged PRs in 30d

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!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.