Skip to content

Commit

Permalink
feat(nextjs): Add bundleSizeOptimizations to build options (#13323)
Browse files Browse the repository at this point in the history
Extend options that can be added to `withSentryConfig`

part of #13012

---------

Co-authored-by: Luca Forstner <[email protected]>
  • Loading branch information
s1gr1d and lforst authored Sep 2, 2024
1 parent 01165db commit 318c163
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
44 changes: 44 additions & 0 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,50 @@ export type SentryBuildOptions = {
};
};

/**
* Options to configure various bundle size optimizations related to the Sentry SDK.
*/
bundleSizeOptimizations?: {
/**
* If set to `true`, the Sentry SDK will attempt to tree-shake (remove) any debugging code within itself during the build.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* Setting this option to `true` will disable features like the SDK's `debug` option.
*/
excludeDebugStatements?: boolean;

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

/**
* 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.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* This option is safe to be used when you do not want to capture any Shadow DOM activity via Sentry Session Replay.
*/
excludeReplayShadowDom?: boolean;

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

/**
* 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.
* Note that the success of this depends on tree shaking being enabled in your build tooling.
*
* **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.
*/
excludeReplayWorker?: boolean;
};

/**
* Options related to react component name annotations.
* Disabled by default, unless a value is set for this option.
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let showedMissingGlobalErrorWarningMsg = false;
* - `plugins`, to add SentryWebpackPlugin
*
* @param userNextConfig The user's existing nextjs config, as passed to `withSentryConfig`
* @param userSentryWebpackPluginOptions The user's SentryWebpackPlugin config, as passed to `withSentryConfig`
* @param userSentryOptions The user's SentryWebpackPlugin config, as passed to `withSentryConfig`
* @returns The function to set as the nextjs config's `webpack` value
*/
export function constructWebpackConfigFunction(
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/src/config/webpackPluginOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export function getWebpackPluginOptions(
deploy: sentryBuildOptions.release?.deploy,
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.release,
},
bundleSizeOptimizations: {
...sentryBuildOptions.bundleSizeOptimizations,
},
_metaOptions: {
loggerPrefixOverride: `[@sentry/nextjs - ${prefixInsert}]`,
telemetry: {
Expand Down
17 changes: 17 additions & 0 deletions packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ describe('getWebpackPluginOptions()', () => {
});
});

it('forwards bundleSizeOptimization options', () => {
const buildContext = generateBuildContext({ isServer: false });
const generatedPluginOptions = getWebpackPluginOptions(buildContext, {
bundleSizeOptimizations: {
excludeTracing: true,
excludeReplayShadowDom: false,
},
});

expect(generatedPluginOptions).toMatchObject({
bundleSizeOptimizations: {
excludeTracing: true,
excludeReplayShadowDom: false,
},
});
});

it('returns the right `assets` and `ignore` values during the server build', () => {
const buildContext = generateBuildContext({ isServer: true });
const generatedPluginOptions = getWebpackPluginOptions(buildContext, {});
Expand Down

0 comments on commit 318c163

Please sign in to comment.