From 094114b45740c954fd31ea6960945fc7aa5d645a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 24 Jan 2025 10:08:25 +0100 Subject: [PATCH 1/3] feat(webpack): Gate forced process exit behind experimental flag --- 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..360bba83 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"]) ?? + false; + 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..7b2d46fd 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 }; From 1996c0357eaa97ec27e0bfced9c36d5109f1c6f2 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 24 Jan 2025 10:13:55 +0100 Subject: [PATCH 2/3] keep _experiments optional --- packages/webpack-plugin/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-plugin/src/index.ts b/packages/webpack-plugin/src/index.ts index 7b2d46fd..c69a2b3e 100644 --- a/packages/webpack-plugin/src/index.ts +++ b/packages/webpack-plugin/src/index.ts @@ -186,7 +186,7 @@ const sentryUnplugin = sentryUnpluginFactory({ }); type SentryWebpackPluginOptions = Options & { - _experiments: Options["_experiments"] & { + _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. From 1874e5be37b9ead5b71e0de48646379871ec802f Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 24 Jan 2025 10:19:41 +0100 Subject: [PATCH 3/3] fix incorrect check --- packages/bundler-plugin-core/src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 360bba83..06ffb373 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -382,9 +382,9 @@ export function sentryUnpluginFactory({ } 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"]) ?? - false; + typeof options._experiments["forceExitOnBuildCompletion"] === "boolean" + ? options._experiments["forceExitOnBuildCompletion"] + : undefined; plugins.push( debugIdUploadPlugin(