From 4fa73fd34a53b9cf416410f4cd2863182c7a1123 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 12 Aug 2024 16:18:11 +0200 Subject: [PATCH] feat(nextjs): Add `bundleSizeOptimizations` to build options --- packages/nextjs/package.json | 2 +- packages/nextjs/src/config/types.ts | 47 +++++++++++++++++++ packages/nextjs/src/config/webpack.ts | 2 +- .../nextjs/src/config/webpackPluginOptions.ts | 3 ++ .../webpack/webpackPluginOptions.test.ts | 8 ++++ yarn.lock | 10 ++-- 6 files changed, 65 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index e6ccca4162a4..d82d641a7059 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -78,7 +78,7 @@ "@sentry/types": "8.25.0", "@sentry/utils": "8.25.0", "@sentry/vercel-edge": "8.25.0", - "@sentry/webpack-plugin": "2.20.1", + "@sentry/webpack-plugin": "2.22.0", "chalk": "3.0.0", "resolve": "1.22.8", "rollup": "3.29.4", diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 883ba3c26d41..a1f27cdeb513 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -307,6 +307,53 @@ export type SentryBuildOptions = { }; }; + /** + * Options for the Sentry Webpack plugin to customize bundle size optimizations. + * + * These options are always read from the `sentryAstro` integration. + * Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files. + */ + bundleSizeOptimizations?: { + /** + * If set to `true`, the plugin will attempt to tree-shake (remove) any debugging code within the Sentry SDK. + * 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 plugin will try to tree-shake tracing statements out. + * Note that the success of this depends on tree shaking generally being enabled in your build. + * Attention: DO NOT enable this when you're using any performance monitoring-related SDK features (e.g. Sentry.startSpan()). + */ + excludeTracing?: boolean; + + /** + * If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry 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 plugin will attempt to tree-shake (remove) code related to the Sentry 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 plugin will attempt to tree-shake (remove) code related to the Sentry 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 do 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..e63ce04031b4 100644 --- a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts +++ b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts @@ -52,6 +52,10 @@ describe('getWebpackPluginOptions()', () => { env: 'my-env', }, }, + bundleSizeOptimizations: { + excludeTracing: true, + excludeReplayShadowDom: false, + }, }); expect(generatedPluginOptions.authToken).toBe('my-auth-token'); @@ -105,6 +109,10 @@ describe('getWebpackPluginOptions()', () => { }, telemetry: false, url: 'my-url', + bundleSizeOptimizations: { + excludeTracing: true, + excludeReplayShadowDom: false, + }, }); }); diff --git a/yarn.lock b/yarn.lock index 5603f142ea95..61f181dc2eaf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8400,12 +8400,12 @@ unplugin "1.0.1" uuid "^9.0.0" -"@sentry/webpack-plugin@2.20.1": - version "2.20.1" - resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-2.20.1.tgz#285d325a0a1bd0a534126b97e0190da9486ff7f6" - integrity sha512-U6LzoE09Ndt0OCWROoRaZqqIHGxyMRdKpBhbqoBqyyfVwXN/zGW3I/cWZ1e8rreiKFj+2+c7+X0kOS+NGMTUrg== +"@sentry/webpack-plugin@2.22.0": + version "2.22.0" + resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-2.22.0.tgz#f02e087fe9b318fefdb5441149ceadfc60407ae3" + integrity sha512-u2brctki0AMCoZksdAConQSYE6PokRVeZ4YYsbnJYkAi0KuaQnczsRwS9e2L0bK2CmZ7QdyYcrjaXHNlXaFDbQ== dependencies: - "@sentry/bundler-plugin-core" "2.20.1" + "@sentry/bundler-plugin-core" "2.22.0" unplugin "1.0.1" uuid "^9.0.0"