Skip to content

Commit 15d9102

Browse files
author
Luca Forstner
authored
feat(nextjs): Add disableLogger option that automatically tree shakes logger statements (#7908)
1 parent 13cbb1d commit 15d9102

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GLOBAL_OBJ } from '@sentry/utils';
22
import type { SentryCliPluginOptions } from '@sentry/webpack-plugin';
3-
import type { WebpackPluginInstance } from 'webpack';
3+
import type { DefinePlugin, WebpackPluginInstance } from 'webpack';
44

55
export type SentryWebpackPluginOptions = SentryCliPluginOptions;
66
export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpackPluginOptions };
@@ -128,6 +128,11 @@ export type UserSentryOptions = {
128128
* NOTE: This feature only works with Next.js 11+
129129
*/
130130
tunnelRoute?: string;
131+
132+
/**
133+
* Tree shakes Sentry SDK logger statements from the bundle.
134+
*/
135+
disableLogger?: boolean;
131136
};
132137

133138
export type NextConfigFunction = (phase: string, defaults: { defaultConfig: NextConfigObject }) => NextConfigObject;
@@ -167,7 +172,10 @@ export type BuildContext = {
167172
dir: string;
168173
// eslint-disable-next-line @typescript-eslint/no-explicit-any
169174
config: any;
170-
webpack: { version: string };
175+
webpack: {
176+
version: string;
177+
DefinePlugin: typeof DefinePlugin;
178+
};
171179
// eslint-disable-next-line @typescript-eslint/no-explicit-any
172180
defaultLoaders: any;
173181
totalPages: number;

packages/nextjs/src/config/webpack.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ export function constructWebpackConfigFunction(
296296
}
297297
}
298298

299+
if (userSentryOptions.disableLogger) {
300+
newConfig.plugins = newConfig.plugins || [];
301+
newConfig.plugins.push(
302+
new buildContext.webpack.DefinePlugin({
303+
__SENTRY_DEBUG__: false,
304+
}),
305+
);
306+
}
307+
299308
return newConfig;
300309
};
301310
}

packages/nextjs/test/config/fixtures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function getBuildContext(
9999
distDir: '.next',
100100
...materializedNextConfig,
101101
} as NextConfigObject,
102-
webpack: { version: webpackVersion },
102+
webpack: { version: webpackVersion, DefinePlugin: class {} as any },
103103
defaultLoaders: true,
104104
totalPages: 2,
105105
isServer: buildTarget === 'server' || buildTarget === 'edge',

0 commit comments

Comments
 (0)