From 1fcf768d0a4266a031ad3f407b4f512755c23100 Mon Sep 17 00:00:00 2001 From: Daniil Gaponov Date: Thu, 10 Jul 2025 21:19:07 +0200 Subject: [PATCH] fix: memory leaks from worker recompilation with HMR Add alreadyCompiled flag to prevent unnecessary worker rebuilds during development with Hot Module Replacement, which was causing memory leaks in both webpack and rspack. --- .../src/plugins/AddWorkerEntryPointPlugin.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/webpack-plugin/src/plugins/AddWorkerEntryPointPlugin.ts b/webpack-plugin/src/plugins/AddWorkerEntryPointPlugin.ts index 68ad845c..2bd4ff60 100644 --- a/webpack-plugin/src/plugins/AddWorkerEntryPointPlugin.ts +++ b/webpack-plugin/src/plugins/AddWorkerEntryPointPlugin.ts @@ -13,11 +13,17 @@ function getCompilerHook( { id, entry, filename, chunkFilename, plugins }: IAddWorkerEntryPointPluginOptions ) { const webpack = compiler.webpack ?? require('webpack'); + let alreadyCompiled = false; return function ( compilation: webpack.Compilation, callback: (error?: Error | null | false) => void ) { + if (alreadyCompiled) { + callback(); + return; + } + const outputOptions = { filename, chunkFilename, @@ -33,7 +39,14 @@ function getCompilerHook( new SingleEntryPlugin(compiler.context, entry, 'main').apply(childCompiler); plugins.forEach((plugin) => plugin.apply(childCompiler)); - childCompiler.runAsChild((err?: Error | null) => callback(err)); + childCompiler.runAsChild((err?: Error | null) => { + if (err) { + callback(err); + } else { + alreadyCompiled = true; + callback(); + } + }); }; }