From ce88eed3ba33889e05ddd263941fa344185e57a2 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Tue, 25 Nov 2025 15:06:24 +0000 Subject: [PATCH 1/7] Webpack: Improve performance of module-mocking plugins --- .../webpack-inject-mocker-runtime-plugin.ts | 10 +++-- .../src/plugins/webpack-mock-plugin.ts | 42 +++++++++++++++---- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/code/builders/builder-webpack5/src/plugins/webpack-inject-mocker-runtime-plugin.ts b/code/builders/builder-webpack5/src/plugins/webpack-inject-mocker-runtime-plugin.ts index da843d1cd782..b3eaef73fe07 100644 --- a/code/builders/builder-webpack5/src/plugins/webpack-inject-mocker-runtime-plugin.ts +++ b/code/builders/builder-webpack5/src/plugins/webpack-inject-mocker-runtime-plugin.ts @@ -13,6 +13,7 @@ const PLUGIN_NAME = 'WebpackInjectMockerRuntimePlugin'; * Storybook preview bundle, are executed. */ export class WebpackInjectMockerRuntimePlugin { + private cachedRuntime: string | null = null; // We need to lazy-require HtmlWebpackPlugin because it's an optional peer dependency. private getHtmlWebpackPlugin(compiler: Compiler): typeof HtmlWebpackPlugin | null { try { @@ -52,7 +53,8 @@ export class WebpackInjectMockerRuntimePlugin { PLUGIN_NAME, (data, cb) => { try { - const runtimeScriptContent = getMockerRuntime(); + const runtimeScriptContent = + this.cachedRuntime ?? (this.cachedRuntime = getMockerRuntime()); const runtimeAssetName = 'mocker-runtime-injected.js'; // Use the documented `emitAsset` method to add the pre-bundled runtime script @@ -62,10 +64,12 @@ export class WebpackInjectMockerRuntimePlugin { new compiler.webpack.sources.RawSource(runtimeScriptContent) ); - // Prepend the name of our new asset to the list of JavaScript files. + // Prepend the name of our new asset to the list of JavaScript files, once. // HtmlWebpackPlugin will automatically create a