From a671c5a702667973bf50b467f3dbb928af647e38 Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Fri, 5 Jun 2026 10:06:49 +0200 Subject: [PATCH] fix(webpack): Make `webpack` import lazy to support rspack-only projects --- packages/bundler-plugins/src/webpack/index.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/bundler-plugins/src/webpack/index.ts b/packages/bundler-plugins/src/webpack/index.ts index a63ffaa2..8f3fc523 100644 --- a/packages/bundler-plugins/src/webpack/index.ts +++ b/packages/bundler-plugins/src/webpack/index.ts @@ -1,10 +1,32 @@ import type { SentryWebpackPluginOptions } from "./webpack4and5"; import { sentryWebpackPluginFactory } from "./webpack4and5"; -import * as webpack4or5 from "webpack"; +import { createRequire } from "node:module"; -const BannerPlugin = webpack4or5?.BannerPlugin || webpack4or5?.default?.BannerPlugin; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type PluginClass = new (options: any) => unknown; + +type WebpackModule = { + BannerPlugin?: PluginClass; + DefinePlugin?: PluginClass; + default?: WebpackModule; +}; + +// `webpack` is an optional peer dependency. We require it lazily so the plugin doesn't +// crash on load in bundlers that don't ship `webpack` (e.g. rspack) — those provide +// the plugin classes via `compiler.webpack` at runtime instead. +function loadWebpack(): WebpackModule { + try { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore Rollup transpiles import.meta for CJS + return createRequire(import.meta.url)("webpack") as WebpackModule; + } catch { + return {}; + } +} -const DefinePlugin = webpack4or5?.DefinePlugin || webpack4or5?.default?.DefinePlugin; +const webpack = loadWebpack(); +const BannerPlugin = webpack.BannerPlugin ?? webpack.default?.BannerPlugin; +const DefinePlugin = webpack.DefinePlugin ?? webpack.default?.DefinePlugin; // eslint-disable-next-line @typescript-eslint/no-explicit-any export const sentryWebpackPlugin: (options?: SentryWebpackPluginOptions) => any =