From 318c1635a4f528a10c3af4014b1f8bf2185890c0 Mon Sep 17 00:00:00 2001 From: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com> Date: Mon, 2 Sep 2024 10:20:09 +0200 Subject: [PATCH] feat(nextjs): Add `bundleSizeOptimizations` to build options (#13323) Extend options that can be added to `withSentryConfig` part of https://github.com/getsentry/sentry-javascript/issues/13012 --------- Co-authored-by: Luca Forstner --- packages/nextjs/src/config/types.ts | 44 +++++++++++++++++++ packages/nextjs/src/config/webpack.ts | 2 +- .../nextjs/src/config/webpackPluginOptions.ts | 3 ++ .../webpack/webpackPluginOptions.test.ts | 17 +++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 883ba3c26d41..d2e78d87f4ae 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -307,6 +307,50 @@ export type SentryBuildOptions = { }; }; + /** + * Options to configure various bundle size optimizations related to the Sentry SDK. + */ + bundleSizeOptimizations?: { + /** + * If set to `true`, the Sentry SDK will attempt to tree-shake (remove) any debugging code within itself during the build. + * Note that the success of this depends on tree shaking being enabled in your build tooling. + * + * Setting this option to `true` will disable features like the SDK's `debug` option. + */ + excludeDebugStatements?: boolean; + + /** + * If set to `true`, the Sentry SDK will attempt to tree-shake (remove) code within itself that is related to tracing and performance monitoring. + * Note that the success of this depends on tree shaking being enabled in your build tooling. + * **Notice:** Do not enable this when you're using any performance monitoring-related SDK features (e.g. `Sentry.startTransaction()`). + */ + excludeTracing?: boolean; + + /** + * If set to `true`, the Sentry SDK will attempt to tree-shake (remove) code related to the SDK's Session Replay Shadow DOM recording functionality. + * Note that the success of this depends on tree shaking being enabled in your build tooling. + * + * This option is safe to be used when you do not want to capture any Shadow DOM activity via Sentry Session Replay. + */ + excludeReplayShadowDom?: boolean; + + /** + * If set to `true`, the Sentry SDK will attempt to tree-shake (remove) code related to the SDK's Session Replay `iframe` recording functionality. + * Note that the success of this depends on tree shaking being enabled in your build tooling. + * + * You can safely do this when you do not want to capture any `iframe` activity via Sentry Session Replay. + */ + excludeReplayIframe?: boolean; + + /** + * If set to `true`, the Sentry SDK will attempt to tree-shake (remove) code related to the SDK's Session Replay's Compression Web Worker. + * Note that the success of this depends on tree shaking being enabled in your build tooling. + * + * **Notice:** You should only use this option if you manually host a compression worker and configure it in your Sentry Session Replay integration config via the `workerUrl` option. + */ + excludeReplayWorker?: boolean; + }; + /** * Options related to react component name annotations. * Disabled by default, unless a value is set for this option. diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index ecc39f7372dd..8fbc94b42195 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -37,7 +37,7 @@ let showedMissingGlobalErrorWarningMsg = false; * - `plugins`, to add SentryWebpackPlugin * * @param userNextConfig The user's existing nextjs config, as passed to `withSentryConfig` - * @param userSentryWebpackPluginOptions The user's SentryWebpackPlugin config, as passed to `withSentryConfig` + * @param userSentryOptions The user's SentryWebpackPlugin config, as passed to `withSentryConfig` * @returns The function to set as the nextjs config's `webpack` value */ export function constructWebpackConfigFunction( diff --git a/packages/nextjs/src/config/webpackPluginOptions.ts b/packages/nextjs/src/config/webpackPluginOptions.ts index f70862bfe484..1bca9bff49b6 100644 --- a/packages/nextjs/src/config/webpackPluginOptions.ts +++ b/packages/nextjs/src/config/webpackPluginOptions.ts @@ -97,6 +97,9 @@ export function getWebpackPluginOptions( deploy: sentryBuildOptions.release?.deploy, ...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.release, }, + bundleSizeOptimizations: { + ...sentryBuildOptions.bundleSizeOptimizations, + }, _metaOptions: { loggerPrefixOverride: `[@sentry/nextjs - ${prefixInsert}]`, telemetry: { diff --git a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts index 54d55a179e28..557859b2a7e1 100644 --- a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts +++ b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts @@ -108,6 +108,23 @@ describe('getWebpackPluginOptions()', () => { }); }); + it('forwards bundleSizeOptimization options', () => { + const buildContext = generateBuildContext({ isServer: false }); + const generatedPluginOptions = getWebpackPluginOptions(buildContext, { + bundleSizeOptimizations: { + excludeTracing: true, + excludeReplayShadowDom: false, + }, + }); + + expect(generatedPluginOptions).toMatchObject({ + bundleSizeOptimizations: { + excludeTracing: true, + excludeReplayShadowDom: false, + }, + }); + }); + it('returns the right `assets` and `ignore` values during the server build', () => { const buildContext = generateBuildContext({ isServer: true }); const generatedPluginOptions = getWebpackPluginOptions(buildContext, {});