From 4fa73fd34a53b9cf416410f4cd2863182c7a1123 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 12 Aug 2024 16:18:11 +0200 Subject: [PATCH 1/5] 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" From ee09a027d3d226440a294cb3bd88936cfae1eac4 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Wed, 14 Aug 2024 10:01:36 +0200 Subject: [PATCH 2/5] delete astro comment --- packages/nextjs/src/config/types.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index a1f27cdeb513..36d18198609f 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -309,9 +309,6 @@ 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?: { /** From ecef80c2eceb0c24aa5a80480e0daacf1d3b1378 Mon Sep 17 00:00:00 2001 From: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com> Date: Wed, 14 Aug 2024 17:57:05 +0200 Subject: [PATCH 3/5] Update packages/nextjs/src/config/types.ts Co-authored-by: Luca Forstner --- packages/nextjs/src/config/types.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 36d18198609f..d2e78d87f4ae 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -308,11 +308,11 @@ export type SentryBuildOptions = { }; /** - * Options for the Sentry Webpack plugin to customize bundle size optimizations. + * Options to configure various bundle size optimizations related to the Sentry SDK. */ bundleSizeOptimizations?: { /** - * If set to `true`, the plugin will attempt to tree-shake (remove) any debugging code within the Sentry SDK. + * 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. @@ -320,14 +320,14 @@ export type SentryBuildOptions = { 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()). + * 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 plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay Shadow DOM recording functionality. + * 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. @@ -335,7 +335,7 @@ export type SentryBuildOptions = { 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. + * 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. @@ -343,10 +343,10 @@ export type SentryBuildOptions = { 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. + * 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 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. + * **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; }; From d927f72863bd3637e0e48881b4128596fdec5e2f Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Wed, 14 Aug 2024 18:00:20 +0200 Subject: [PATCH 4/5] create separate test --- .../config/webpack/webpackPluginOptions.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts index e63ce04031b4..8082a3c2a7f1 100644 --- a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts +++ b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts @@ -116,6 +116,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, {}); From 653c3415a2f572c87ddb1dec39fe0bd962b9e82e Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Wed, 14 Aug 2024 18:02:01 +0200 Subject: [PATCH 5/5] delete from old test --- .../test/config/webpack/webpackPluginOptions.test.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts index 8082a3c2a7f1..557859b2a7e1 100644 --- a/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts +++ b/packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts @@ -52,10 +52,6 @@ describe('getWebpackPluginOptions()', () => { env: 'my-env', }, }, - bundleSizeOptimizations: { - excludeTracing: true, - excludeReplayShadowDom: false, - }, }); expect(generatedPluginOptions.authToken).toBe('my-auth-token'); @@ -109,10 +105,6 @@ describe('getWebpackPluginOptions()', () => { }, telemetry: false, url: 'my-url', - bundleSizeOptimizations: { - excludeTracing: true, - excludeReplayShadowDom: false, - }, }); });