From 33b306b07271338e7700dd19af22de0234b47d4b Mon Sep 17 00:00:00 2001 From: Sergey Tatarintsev Date: Mon, 30 Jul 2018 14:10:11 +0200 Subject: [PATCH] fix: memory leak in TransformNormalModuleFactoryPlugin (#414) Problem: plugin wraps createLoaderContext without checking if it was already done before. As a result, in a watch mode more and more wrappers get added on top of the function and eat more and more memory. Fixed by saving original function on module instance itself and wrapping saved function instead. --- lib/TransformNormalModuleFactoryPlugin.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/TransformNormalModuleFactoryPlugin.js b/lib/TransformNormalModuleFactoryPlugin.js index ca10c950..836abd5f 100644 --- a/lib/TransformNormalModuleFactoryPlugin.js +++ b/lib/TransformNormalModuleFactoryPlugin.js @@ -38,11 +38,12 @@ class NormalModuleFactoryPlugin { 'HardSource - TransformNormalModuleFactoryPlugin', module => { if (module.constructor.name === 'NormalModule') { - const _createLoaderContext = module.createLoaderContext; + module.__originalCreateLoaderContext = + module.__originalCreateLoaderContext || + module.createLoaderContext; module.__hardSource_resolved = {}; module.createLoaderContext = (...args) => { - const loaderContext = _createLoaderContext.call( - module, + const loaderContext = module.__originalCreateLoaderContext( ...args, ); const _resolve = loaderContext.resolve;