Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to also clear cache for children modules #4

Closed
wants to merge 6 commits into from

Conversation

jasonkhanlar
Copy link

Add option to also delete cache for all child modules of the specified module using { children: true } passed as 2nd argument

while (i--) {
delete require.cache[require.cache[filePath].children[i].id];
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it maybe be smart to run the children through clear(), making it recursive, instead of deleting them directly. That way the children too would be run through the code on line 26-35 that deletes the children from their parents, preventing memory leaks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was just working on a recursive implementation before I notice your comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mm, yes, it probably would have been easier to use clear() and comparing root directory to make sure files are nested inside.

@sindresorhus
Copy link
Owner

I'm ok with adding this option, but it needs to be documented and tested.

@jasonkhanlar
Copy link
Author

jasonkhanlar commented Oct 24, 2018

I'm ok with adding this option, but it needs to be documented and tested.

I'm not sure how to write a test in test.js for clearModule('modulename', { children: true });
but I'll try working from:

test('clearModule()', t => {
        const id = './fixture';
        t.is(require(id)(), 1);
        t.is(require(id)(), 2);
        m(id);
        t.is(require(id)(), 1);
});

I'm working on a script to add to test.js. It's not pretty, but I'm currently playing around with this, trying to pass some tests, and gather some debug data:

test('clearModule("fixture", {children: true})', t => {
	const id = './fixture';
	t.is(require(id)(), 1);
	t.is(require(id)(), 2);
	m(id);
	t.is(require(id)(), 1);

	const scriptsCachedBeforeRequire = Object.keys(require.cache).length;
	const path = require('path');
	const id2 = 'resolve-from';
	const resolveFrom = require(id2);
	const id3 = 'caller-path';
	const callerPath = require(id3);
	const id2Path = resolveFrom(path.dirname(callerPath()), id2);
	const id2Dir = path.dirname(id2Path);
	const id3Path = resolveFrom(path.dirname(callerPath()), id3);
	const id3Dir = path.dirname(id3Path);
	console.log(id2Dir);
	console.log(id3Dir);
	const scriptsCachedAfterRequire = Object.keys(require.cache).length;
	console.log(scriptsCachedBeforeRequire);
	console.log(scriptsCachedAfterRequire);
	t.is(require(id)(), 13); // Should fail intentionally
});

@sindresorhus
Copy link
Owner

Closing this in favor of #9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants