diff --git a/lib/utils.ts b/lib/utils.ts index b117ede0c2..fbdc2986ae 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -306,18 +306,25 @@ export function clearRequireCache(options: {exclude?: string[]} = {}) { excludes.push('/node_modules/'); for (const path in require.cache) { - let skip = false; - for (const exclude of excludes) { - if (path.includes(exclude)) { - skip = true; - break; - } - } - - if (!skip) delete require.cache[path]; + if (excludes.some(p => path.includes(p))) continue; + const mod = require.cache[path]; // have to ref to appease ts + if (!mod) continue; + uncacheModuleTree(mod, excludes); + delete require.cache[path]; } } +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) { + if (excludes.some(p => child.filename.includes(p))) continue; + uncacheModuleTree(child, excludes, depth); + } + delete (mod as any).children; +} + export function deepClone(obj: any): any { if (obj === null || typeof obj !== 'object') return obj; if (Array.isArray(obj)) return obj.map(prop => deepClone(prop));