Skip to content

Commit

Permalink
fix: memory leak in module cache management, fixes #560
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed May 13, 2020
1 parent 5a9c33b commit 6c11703
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/server/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
export const clearModuleCache = key => delete require.cache[key]
export const clearModuleCache = (moduleName) => {
const m = require.cache[moduleName];
if (m) {
// remove self from own parents
if (m.parent && m.parent.children) {
m.parent.children = m.parent.children.filter(x => x !== m);
}
// remove self from own children
if (m.children) {
m.children.forEach(child => {
if (child.parent && child.parent === m) {
child.parent = null;
}
})
}
delete require.cache[moduleName]
}
}

export const smartRequire = modulePath => {
if (process.env.NODE_ENV !== 'production') {
Expand Down

0 comments on commit 6c11703

Please sign in to comment.