From 587ef7824509b3243d8bec7c209e27f005bd74fb Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 24 Jan 2025 11:03:56 +0100 Subject: [PATCH] feat(webpack): Gate forced process exit behind experimental flag (#663) Revert the default behaviour of the webpack plugin to no longer exit the process. Instead, users can set an experimental flag to force exiting the process. --- packages/bundler-plugin-core/src/index.ts | 12 +++++++++-- packages/bundler-plugin-core/src/types.ts | 2 +- packages/webpack-plugin/src/index.ts | 25 +++++++++++++++++++---- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index b623f53a..06ffb373 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -35,7 +35,8 @@ interface SentryUnpluginFactoryOptions { debugIdInjectionPlugin: (logger: Logger) => UnpluginOptions; debugIdUploadPlugin: ( upload: (buildArtifacts: string[]) => Promise, - logger: Logger + logger: Logger, + webpack_forceExitOnBuildComplete?: boolean ) => UnpluginOptions; bundleSizeOptimizationsPlugin: (buildFlags: SentrySDKBuildFlags) => UnpluginOptions; } @@ -379,6 +380,12 @@ export function sentryUnpluginFactory({ "No project provided. Will not upload source maps. Please set the `project` option to your Sentry project slug." ); } else { + // This option is only strongly typed for the webpack plugin, where it is used. It has no effect on other plugins + const webpack_forceExitOnBuildComplete = + typeof options._experiments["forceExitOnBuildCompletion"] === "boolean" + ? options._experiments["forceExitOnBuildCompletion"] + : undefined; + plugins.push( debugIdUploadPlugin( createDebugIdUploadFunction({ @@ -402,7 +409,8 @@ export function sentryUnpluginFactory({ headers: options.headers, }, }), - logger + logger, + webpack_forceExitOnBuildComplete ) ); } diff --git a/packages/bundler-plugin-core/src/types.ts b/packages/bundler-plugin-core/src/types.ts index 33a03e58..d23cb480 100644 --- a/packages/bundler-plugin-core/src/types.ts +++ b/packages/bundler-plugin-core/src/types.ts @@ -324,7 +324,7 @@ export interface Options { * Defaults to `false`. */ injectBuildInformation?: boolean; - }; + } & Record; /** * Options that are useful for building wrappers around the plugin. You likely don't need these options unless you diff --git a/packages/webpack-plugin/src/index.ts b/packages/webpack-plugin/src/index.ts index 0389cc1c..c69a2b3e 100644 --- a/packages/webpack-plugin/src/index.ts +++ b/packages/webpack-plugin/src/index.ts @@ -119,7 +119,8 @@ function webpackDebugIdInjectionPlugin(): UnpluginOptions { function webpackDebugIdUploadPlugin( upload: (buildArtifacts: string[]) => Promise, - logger: Logger + logger: Logger, + forceExitOnBuildCompletion?: boolean ): UnpluginOptions { const pluginName = "sentry-webpack-debug-id-upload-plugin"; return { @@ -136,7 +137,7 @@ function webpackDebugIdUploadPlugin( }); }); - if (compiler.options.mode === "production") { + if (forceExitOnBuildCompletion && compiler.options.mode === "production") { compiler.hooks.done.tap(pluginName, () => { setTimeout(() => { logger.debug("Exiting process after debug file upload"); @@ -184,8 +185,24 @@ const sentryUnplugin = sentryUnpluginFactory({ bundleSizeOptimizationsPlugin: webpackBundleSizeOptimizationsPlugin, }); +type SentryWebpackPluginOptions = Options & { + _experiments?: Options["_experiments"] & { + /** + * If enabled, the webpack plugin will exit the build process after the build completes. + * Use this with caution, as it will terminate the process. + * + * More information: https://github.com/getsentry/sentry-javascript-bundler-plugins/issues/345 + * + * @default false + */ + forceExitOnBuildCompletion?: boolean; + }; +}; + // eslint-disable-next-line @typescript-eslint/no-explicit-any -export const sentryWebpackPlugin: (options?: Options) => any = sentryUnplugin.webpack; +export const sentryWebpackPlugin: (options?: SentryWebpackPluginOptions) => any = + sentryUnplugin.webpack; export { sentryCliBinaryExists } from "@sentry/bundler-plugin-core"; -export type { Options as SentryWebpackPluginOptions } from "@sentry/bundler-plugin-core"; + +export type { SentryWebpackPluginOptions };