Utils: Ensure require cache clearing is exhaustive

Previously, this was limited by a depth check that always got hit, meaning it never fully properly cleaned out the module tree (most likely due to the fact the uncacheModuleTree call would loop around to the same module through recursion, meaning mod.children never got deleted, so it never stopped looping). This commit fixes that.
This commit is contained in:
Mia
2024-04-22 15:41:54 -05:00
parent c1b1c86c73
commit 56c3b69fcd

View File

@@ -314,13 +314,12 @@ export function clearRequireCache(options: {exclude?: string[]} = {}) {
}
}
export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[], depth = 0) {
depth++;
if (depth >= 10) return;
if (!mod.children || excludes.some(p => mod.filename.includes(p))) return;
for (const child of mod.children) {
export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[]) {
if (!mod.children?.length || excludes.some(p => mod.filename.includes(p))) return;
for (const [i, child] of mod.children.entries()) {
if (excludes.some(p => child.filename.includes(p))) continue;
uncacheModuleTree(child, excludes, depth);
mod.children?.splice(i, 1);
uncacheModuleTree(child, excludes);
}
delete (mod as any).children;
}