Skip to content

Commit 551c70b

Browse files
committed
feat: added webpack treeshaking flags
1 parent d4301fd commit 551c70b

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ export type SentryBuildWebpackOptions = {
112112
* Removes Sentry SDK logger statements from the bundle. Note that this doesn't affect Sentry Logs.
113113
*/
114114
removeDebugLogging?: boolean;
115+
116+
/**
117+
* Setting this to true will tree-shake any SDK code that is related to tracing and performance monitoring.
118+
*/
119+
tracing?: boolean;
120+
121+
/**
122+
* Replacing this flag with true will tree shake any SDK code related to capturing iframe content with Session Replay.
123+
* It's only relevant when using Session Replay. Enable this flag if you don't want to record any iframes.
124+
* This has no effect if you did not add `replayIntegration`.
125+
*/
126+
excludeReplayIframe?: boolean;
127+
128+
/**
129+
* Replacing this flag with true will tree shake any SDK code related to capturing shadow dom elements with Session Replay.
130+
* It's only relevant when using Session Replay.
131+
* Enable this flag if you don't want to record any shadow dom elements.
132+
* This has no effect if you did not add `replayIntegration`.
133+
*/
134+
excludeReplayShadowDOM?: boolean;
135+
136+
/**
137+
* Replacing this flag with true will tree shake any SDK code that's related to the included compression web worker for Session Replay.
138+
* It's only relevant when using Session Replay.
139+
* Enable this flag if you want to host a compression worker yourself.
140+
* See Using a Custom Compression Worker for details.
141+
* We don't recommend enabling this flag unless you provide a custom worker URL.
142+
* This has no effect if you did not add `replayIntegration`.
143+
*/
144+
excludeReplayCompressionWorker?: boolean;
115145
};
116146

117147
/**

packages/nextjs/src/config/webpack.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,8 @@ export function constructWebpackConfigFunction({
428428
}
429429
}
430430

431-
if (userSentryOptions.webpack?.treeshake?.removeDebugLogging) {
432-
newConfig.plugins = newConfig.plugins || [];
433-
newConfig.plugins.push(
434-
new buildContext.webpack.DefinePlugin({
435-
__SENTRY_DEBUG__: false,
436-
}),
437-
);
431+
if (userSentryOptions.webpack?.treeshake) {
432+
setupTreeshakingFromConfig(userSentryOptions, newConfig, buildContext);
438433
}
439434

440435
// We inject a map of dependencies that the nextjs app has, as we cannot reliably extract them at runtime, sadly
@@ -912,3 +907,39 @@ function _getModules(projectDir: string): Record<string, string> {
912907
return {};
913908
}
914909
}
910+
911+
/**
912+
* Sets up the tree-shaking flags based on the user's configuration.
913+
* https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/
914+
*/
915+
function setupTreeshakingFromConfig(
916+
userSentryOptions: SentryBuildOptions,
917+
newConfig: WebpackConfigObjectWithModuleRules,
918+
buildContext: BuildContext,
919+
): void {
920+
const defines: Record<string, boolean> = {};
921+
922+
newConfig.plugins = newConfig.plugins || [];
923+
924+
if (userSentryOptions.webpack?.treeshake?.removeDebugLogging) {
925+
defines.__SENTRY_DEBUG__ = false;
926+
}
927+
928+
if (userSentryOptions.webpack?.treeshake?.tracing) {
929+
defines.__SENTRY_TRACING__ = false;
930+
}
931+
932+
if (userSentryOptions.webpack?.treeshake?.excludeReplayIframe) {
933+
defines.__RRWEB_EXCLUDE_IFRAME__ = true;
934+
}
935+
936+
if (userSentryOptions.webpack?.treeshake?.excludeReplayShadowDOM) {
937+
defines.__RRWEB_EXCLUDE_SHADOW_DOM__ = true;
938+
}
939+
940+
if (userSentryOptions.webpack?.treeshake?.excludeReplayCompressionWorker) {
941+
defines.__SENTRY_EXCLUDE_REPLAY_WORKER__ = true;
942+
}
943+
944+
newConfig.plugins.push(new buildContext.webpack.DefinePlugin(defines));
945+
}

0 commit comments

Comments
 (0)