Skip to content

Commit 99183a7

Browse files
logaretmCopilot
andauthored
feat(nextjs): added webpack treeshaking flags as config (#18359)
This PR updates the Sentry Next.js integration to improve tree-shaking configuration and add new options for finer control over what SDK code is included in the final bundle. The most significant changes are: - Added several new tree-shaking flags for better user DX. Closes #18417 --------- Co-authored-by: Copilot <[email protected]>
1 parent 9a016ca commit 99183a7

File tree

5 files changed

+487
-13
lines changed

5 files changed

+487
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66

7+
- feat(nextjs): Add tree-shaking configuration to `webpack` build config ([#18359](https://github.com/getsentry/sentry-javascript/pull/18359))
8+
79
## 10.31.0
810

911
### Important Changes

packages/nextjs/src/config/types.ts

Lines changed: 35 additions & 5 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 treeshake any SDK code that is related to tracing and performance monitoring.
118+
*/
119+
removeTracing?: boolean;
120+
121+
/**
122+
* Setting this flag to `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+
* Setting this flag to `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+
* Setting this flag to `true` will tree shake any SDK code that is 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
/**
@@ -403,38 +433,38 @@ export type SentryBuildOptions = {
403433
*/
404434
bundleSizeOptimizations?: {
405435
/**
406-
* If set to `true`, the Sentry SDK will attempt to tree-shake (remove) any debugging code within itself during the build.
436+
* If set to `true`, the Sentry SDK will attempt to treeshake (remove) any debugging code within itself during the build.
407437
* Note that the success of this depends on tree shaking being enabled in your build tooling.
408438
*
409439
* Setting this option to `true` will disable features like the SDK's `debug` option.
410440
*/
411441
excludeDebugStatements?: boolean;
412442

413443
/**
414-
* If set to `true`, the Sentry SDK will attempt to tree-shake (remove) code within itself that is related to tracing and performance monitoring.
444+
* If set to `true`, the Sentry SDK will attempt to treeshake (remove) code within itself that is related to tracing and performance monitoring.
415445
* Note that the success of this depends on tree shaking being enabled in your build tooling.
416446
* **Notice:** Do not enable this when you're using any performance monitoring-related SDK features (e.g. `Sentry.startTransaction()`).
417447
*/
418448
excludeTracing?: boolean;
419449

420450
/**
421-
* 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.
451+
* If set to `true`, the Sentry SDK will attempt to treeshake (remove) code related to the SDK's Session Replay Shadow DOM recording functionality.
422452
* Note that the success of this depends on tree shaking being enabled in your build tooling.
423453
*
424454
* This option is safe to be used when you do not want to capture any Shadow DOM activity via Sentry Session Replay.
425455
*/
426456
excludeReplayShadowDom?: boolean;
427457

428458
/**
429-
* If set to `true`, the Sentry SDK will attempt to tree-shake (remove) code related to the SDK's Session Replay `iframe` recording functionality.
459+
* If set to `true`, the Sentry SDK will attempt to treeshake (remove) code related to the SDK's Session Replay `iframe` recording functionality.
430460
* Note that the success of this depends on tree shaking being enabled in your build tooling.
431461
*
432462
* You can safely do this when you do not want to capture any `iframe` activity via Sentry Session Replay.
433463
*/
434464
excludeReplayIframe?: boolean;
435465

436466
/**
437-
* 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.
467+
* If set to `true`, the Sentry SDK will attempt to treeshake (remove) code related to the SDK's Session Replay's Compression Web Worker.
438468
* Note that the success of this depends on tree shaking being enabled in your build tooling.
439469
*
440470
* **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.

packages/nextjs/src/config/webpack.ts

Lines changed: 41 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,42 @@ 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?.removeTracing) {
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+
// Only add DefinePlugin if there are actual defines to set
945+
if (Object.keys(defines).length > 0) {
946+
newConfig.plugins.push(new buildContext.webpack.DefinePlugin(defines));
947+
}
948+
}

packages/nextjs/test/config/fixtures.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ export function getBuildContext(
101101
} as NextConfigObject,
102102
webpack: {
103103
version: webpackVersion,
104-
DefinePlugin: class {} as any,
104+
DefinePlugin: class {
105+
constructor(public definitions: Record<string, any>) {}
106+
} as any,
105107
ProvidePlugin: class {
106108
constructor(public definitions: Record<string, any>) {}
107109
} as any,

0 commit comments

Comments
 (0)