From d7a348ec6ab426126f02b3935ae9722fd03f8e8d Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 19 Apr 2023 13:40:29 +0000 Subject: [PATCH 1/2] feat(nextjs): Add `disableLogger` option that automatically tree shakes logger statements --- packages/nextjs/src/config/types.ts | 12 ++++++++++-- packages/nextjs/src/config/webpack.ts | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/nextjs/src/config/types.ts b/packages/nextjs/src/config/types.ts index 523a2f995837..f2156382e6f3 100644 --- a/packages/nextjs/src/config/types.ts +++ b/packages/nextjs/src/config/types.ts @@ -1,6 +1,6 @@ import type { GLOBAL_OBJ } from '@sentry/utils'; import type { SentryCliPluginOptions } from '@sentry/webpack-plugin'; -import type { WebpackPluginInstance } from 'webpack'; +import type { DefinePlugin, WebpackPluginInstance } from 'webpack'; export type SentryWebpackPluginOptions = SentryCliPluginOptions; export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpackPluginOptions }; @@ -128,6 +128,11 @@ export type UserSentryOptions = { * NOTE: This feature only works with Next.js 11+ */ tunnelRoute?: string; + + /** + * Tree shakes Sentry SDK logger statements from the bundle. + */ + disableLogger?: boolean; }; export type NextConfigFunction = (phase: string, defaults: { defaultConfig: NextConfigObject }) => NextConfigObject; @@ -167,7 +172,10 @@ export type BuildContext = { dir: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any config: any; - webpack: { version: string }; + webpack: { + version: string; + DefinePlugin: typeof DefinePlugin; + }; // eslint-disable-next-line @typescript-eslint/no-explicit-any defaultLoaders: any; totalPages: number; diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index e00ac0d6a659..9c68651112c0 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -295,6 +295,15 @@ export function constructWebpackConfigFunction( } } + if (userSentryOptions.disableLogger) { + newConfig.plugins = newConfig.plugins || []; + newConfig.plugins.push( + new buildContext.webpack.DefinePlugin({ + __SENTRY_DEBUG__: false, + }), + ); + } + return newConfig; }; } From c7b778b097a8d91c1da09a57604a79f3075c976b Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 19 Apr 2023 13:58:16 +0000 Subject: [PATCH 2/2] tests --- packages/nextjs/test/config/fixtures.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nextjs/test/config/fixtures.ts b/packages/nextjs/test/config/fixtures.ts index f747edbc2be9..69f15a18f088 100644 --- a/packages/nextjs/test/config/fixtures.ts +++ b/packages/nextjs/test/config/fixtures.ts @@ -99,7 +99,7 @@ export function getBuildContext( distDir: '.next', ...materializedNextConfig, } as NextConfigObject, - webpack: { version: webpackVersion }, + webpack: { version: webpackVersion, DefinePlugin: class {} as any }, defaultLoaders: true, totalPages: 2, isServer: buildTarget === 'server' || buildTarget === 'edge',