Skip to content

Commit 57d407d

Browse files
committed
feat: added webpack treeshaking flags
1 parent bdb4885 commit 57d407d

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
* Tree shakes Sentry SDK logger statements from the bundle. Note that this doesn't affect Sentry Logs.
113113
*/
114114
debugLogging?: 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
@@ -431,13 +431,8 @@ export function constructWebpackConfigFunction({
431431
}
432432
}
433433

434-
if (userSentryOptions.webpack?.treeshake?.debugLogging) {
435-
newConfig.plugins = newConfig.plugins || [];
436-
newConfig.plugins.push(
437-
new buildContext.webpack.DefinePlugin({
438-
__SENTRY_DEBUG__: false,
439-
}),
440-
);
434+
if (userSentryOptions.webpack?.treeshake) {
435+
setupTreeshakingFromConfig(userSentryOptions, newConfig, buildContext);
441436
}
442437

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

0 commit comments

Comments
 (0)