Skip to content

Commit b0d4926

Browse files
authored
feat(astro): Add bundleSizeOptimizations vite options to integration (#13250)
The bundler-plugins still refer to the option as `excludePerformanceMonitoring` ([here](https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/main/packages/bundler-plugin-core/src/types.ts#L260)), but this is going to be renamed to `excludeTracing`, so I already used the new naming as discussed with @Lms24 and @mydea. part of #13013
1 parent 0666eb5 commit b0d4926

File tree

5 files changed

+147
-69
lines changed

5 files changed

+147
-69
lines changed

packages/astro/src/integration/index.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'path';
33
import { sentryVitePlugin } from '@sentry/vite-plugin';
44
import type { AstroConfig, AstroIntegration } from 'astro';
55

6+
import { dropUndefinedKeys } from '@sentry/utils';
67
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
78
import type { SentryOptions } from './types';
89

@@ -40,21 +41,29 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
4041
sourcemap: true,
4142
},
4243
plugins: [
43-
sentryVitePlugin({
44-
org: uploadOptions.org ?? env.SENTRY_ORG,
45-
project: uploadOptions.project ?? env.SENTRY_PROJECT,
46-
authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN,
47-
telemetry: uploadOptions.telemetry ?? true,
48-
sourcemaps: {
49-
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
50-
},
51-
_metaOptions: {
52-
telemetry: {
53-
metaFramework: 'astro',
44+
sentryVitePlugin(
45+
dropUndefinedKeys({
46+
org: uploadOptions.org ?? env.SENTRY_ORG,
47+
project: uploadOptions.project ?? env.SENTRY_PROJECT,
48+
authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN,
49+
telemetry: uploadOptions.telemetry ?? true,
50+
sourcemaps: {
51+
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
5452
},
55-
},
56-
debug: options.debug ?? false,
57-
}),
53+
bundleSizeOptimizations: {
54+
...options.bundleSizeOptimizations,
55+
// TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore
56+
// ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582
57+
excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing,
58+
},
59+
_metaOptions: {
60+
telemetry: {
61+
metaFramework: 'astro',
62+
},
63+
},
64+
debug: options.debug ?? false,
65+
}),
66+
),
5867
],
5968
},
6069
});

packages/astro/src/integration/snippets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
4747
}`;
4848

4949
/**
50-
* We don't include the `BrowserTracing` integration if the tracesSampleRate is set to 0.
50+
* We don't include the `BrowserTracing` integration if `bundleSizeOptimizations.excludeTracing` is falsy.
5151
* Likewise, we don't include the `Replay` integration if the replaysSessionSampleRate
5252
* and replaysOnErrorSampleRate are set to 0.
5353
*
@@ -56,7 +56,7 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
5656
const buildClientIntegrations = (options: SentryOptions): string => {
5757
const integrations: string[] = [];
5858

59-
if (options.tracesSampleRate == null || options.tracesSampleRate) {
59+
if (!options.bundleSizeOptimizations?.excludeTracing) {
6060
integrations.push('Sentry.browserTracingIntegration()');
6161
}
6262

packages/astro/src/integration/types.ts

Lines changed: 98 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,62 +25,95 @@ type SdkInitPaths = {
2525

2626
type SourceMapsOptions = {
2727
/**
28-
* Options for the Sentry Vite plugin to customize the source maps upload process.
28+
* If this flag is `true`, and an auth token is detected, the Sentry integration will
29+
* automatically generate and upload source maps to Sentry during a production build.
2930
*
30-
* These options are always read from the `sentryAstro` integration.
31-
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
31+
* @default true
3232
*/
33-
sourceMapsUploadOptions?: {
34-
/**
35-
* If this flag is `true`, and an auth token is detected, the Sentry integration will
36-
* automatically generate and upload source maps to Sentry during a production build.
37-
*
38-
* @default true
39-
*/
40-
enabled?: boolean;
33+
enabled?: boolean;
4134

42-
/**
43-
* The auth token to use when uploading source maps to Sentry.
44-
*
45-
* Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
46-
*
47-
* To create an auth token, follow this guide:
48-
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
49-
*/
50-
authToken?: string;
35+
/**
36+
* The auth token to use when uploading source maps to Sentry.
37+
*
38+
* Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
39+
*
40+
* To create an auth token, follow this guide:
41+
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
42+
*/
43+
authToken?: string;
5144

52-
/**
53-
* The organization slug of your Sentry organization.
54-
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
55-
*/
56-
org?: string;
45+
/**
46+
* The organization slug of your Sentry organization.
47+
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
48+
*/
49+
org?: string;
5750

58-
/**
59-
* The project slug of your Sentry project.
60-
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
61-
*/
62-
project?: string;
51+
/**
52+
* The project slug of your Sentry project.
53+
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
54+
*/
55+
project?: string;
6356

64-
/**
65-
* If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
66-
* It will not collect any sensitive or user-specific data.
67-
*
68-
* @default true
69-
*/
70-
telemetry?: boolean;
57+
/**
58+
* If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
59+
* It will not collect any sensitive or user-specific data.
60+
*
61+
* @default true
62+
*/
63+
telemetry?: boolean;
7164

72-
/**
73-
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
74-
*
75-
* If this option is not specified, sensible defaults based on your `outDir`, `rootDir` and `adapter`
76-
* config will be used. Use this option to override these defaults, for instance if you have a
77-
* customized build setup that diverges from Astro's defaults.
78-
*
79-
* The globbing patterns must follow the implementation of the `glob` package.
80-
* @see https://www.npmjs.com/package/glob#glob-primer
81-
*/
82-
assets?: string | Array<string>;
83-
};
65+
/**
66+
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
67+
*
68+
* If this option is not specified, sensible defaults based on your `outDir`, `rootDir` and `adapter`
69+
* config will be used. Use this option to override these defaults, for instance if you have a
70+
* customized build setup that diverges from Astro's defaults.
71+
*
72+
* The globbing patterns must follow the implementation of the `glob` package.
73+
* @see https://www.npmjs.com/package/glob#glob-primer
74+
*/
75+
assets?: string | Array<string>;
76+
};
77+
78+
type BundleSizeOptimizationOptions = {
79+
/**
80+
* If set to `true`, the plugin will attempt to tree-shake (remove) any debugging code within the Sentry SDK.
81+
* Note that the success of this depends on tree shaking being enabled in your build tooling.
82+
*
83+
* Setting this option to `true` will disable features like the SDK's `debug` option.
84+
*/
85+
excludeDebugStatements?: boolean;
86+
87+
/**
88+
* If set to true, the plugin will try to tree-shake performance monitoring statements out.
89+
* Note that the success of this depends on tree shaking generally being enabled in your build.
90+
* Attention: DO NOT enable this when you're using any performance monitoring-related SDK features (e.g. Sentry.startTransaction()).
91+
*/
92+
excludeTracing?: boolean;
93+
94+
/**
95+
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay Shadow DOM recording functionality.
96+
* Note that the success of this depends on tree shaking being enabled in your build tooling.
97+
*
98+
* This option is safe to be used when you do not want to capture any Shadow DOM activity via Sentry Session Replay.
99+
*/
100+
excludeReplayShadowDom?: boolean;
101+
102+
/**
103+
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay `iframe` recording functionality.
104+
* Note that the success of this depends on tree shaking being enabled in your build tooling.
105+
*
106+
* You can safely do this when you do not want to capture any `iframe` activity via Sentry Session Replay.
107+
*/
108+
excludeReplayIframe?: boolean;
109+
110+
/**
111+
* If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay's Compression Web Worker.
112+
* Note that the success of this depends on tree shaking being enabled in your build tooling.
113+
*
114+
* **Notice:** You should only do use this option if you manually host a compression worker and configure it in your Sentry Session Replay integration config via the `workerUrl` option.
115+
*/
116+
excludeReplayWorker?: boolean;
84117
};
85118

86119
type InstrumentationOptions = {
@@ -138,6 +171,20 @@ type SdkEnabledOptions = {
138171
export type SentryOptions = SdkInitPaths &
139172
Pick<Options, 'dsn' | 'release' | 'environment' | 'sampleRate' | 'tracesSampleRate' | 'debug'> &
140173
Pick<BrowserOptions, 'replaysSessionSampleRate' | 'replaysOnErrorSampleRate'> &
141-
SourceMapsOptions &
142174
InstrumentationOptions &
143-
SdkEnabledOptions;
175+
SdkEnabledOptions & {
176+
/**
177+
* Options for the Sentry Vite plugin to customize the source maps upload process.
178+
*
179+
* These options are always read from the `sentryAstro` integration.
180+
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
181+
*/
182+
sourceMapsUploadOptions?: SourceMapsOptions;
183+
/**
184+
* Options for the Sentry Vite plugin to customize bundle size optimizations.
185+
*
186+
* These options are always read from the `sentryAstro` integration.
187+
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
188+
*/
189+
bundleSizeOptimizations?: BundleSizeOptimizationOptions;
190+
};

packages/astro/test/integration/index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('sentryAstro integration', () => {
5757
project: 'my-project',
5858
telemetry: false,
5959
debug: false,
60+
bundleSizeOptimizations: {},
6061
sourcemaps: {
6162
assets: ['out/**/*'],
6263
},
@@ -82,6 +83,7 @@ describe('sentryAstro integration', () => {
8283
project: 'my-project',
8384
telemetry: false,
8485
debug: false,
86+
bundleSizeOptimizations: {},
8587
sourcemaps: {
8688
assets: ['dist/**/*'],
8789
},
@@ -114,6 +116,7 @@ describe('sentryAstro integration', () => {
114116
project: 'my-project',
115117
telemetry: false,
116118
debug: false,
119+
bundleSizeOptimizations: {},
117120
sourcemaps: {
118121
assets: ['{.vercel,dist}/**/*'],
119122
},
@@ -151,6 +154,7 @@ describe('sentryAstro integration', () => {
151154
project: 'my-project',
152155
telemetry: true,
153156
debug: false,
157+
bundleSizeOptimizations: {},
154158
sourcemaps: {
155159
assets: ['dist/server/**/*, dist/client/**/*'],
156160
},

packages/astro/test/integration/snippets.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,25 @@ describe('buildClientSnippet', () => {
5252
`);
5353
});
5454

55-
it('does not include browserTracingIntegration if tracesSampleRate is 0', () => {
55+
it('does not include browserTracingIntegration if bundleSizeOptimizations.excludeTracing is true', () => {
56+
const snippet = buildClientSnippet({ bundleSizeOptimizations: { excludeTracing: true } });
57+
expect(snippet).toMatchInlineSnapshot(`
58+
"import * as Sentry from "@sentry/astro";
59+
60+
Sentry.init({
61+
dsn: import.meta.env.PUBLIC_SENTRY_DSN,
62+
debug: false,
63+
environment: import.meta.env.PUBLIC_VERCEL_ENV,
64+
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
65+
tracesSampleRate: 1,
66+
integrations: [Sentry.replayIntegration()],
67+
replaysSessionSampleRate: 0.1,
68+
replaysOnErrorSampleRate: 1,
69+
});"
70+
`);
71+
});
72+
73+
it('still include browserTracingIntegration if tracesSampleRate is 0', () => {
5674
const snippet = buildClientSnippet({ tracesSampleRate: 0 });
5775
expect(snippet).toMatchInlineSnapshot(`
5876
"import * as Sentry from "@sentry/astro";
@@ -63,7 +81,7 @@ describe('buildClientSnippet', () => {
6381
environment: import.meta.env.PUBLIC_VERCEL_ENV,
6482
release: import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA,
6583
tracesSampleRate: 0,
66-
integrations: [Sentry.replayIntegration()],
84+
integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
6785
replaysSessionSampleRate: 0.1,
6886
replaysOnErrorSampleRate: 1,
6987
});"

0 commit comments

Comments
 (0)