From 21388bd0b1c7893d4a521f298cef9a5fb66dccde Mon Sep 17 00:00:00 2001 From: Angelika Cathor Date: Wed, 12 Mar 2025 14:53:42 +0100 Subject: [PATCH 1/3] feat(astro): Accept all vite-plugin options Adds support for `unstable_sentryVitePluginOptions` which can be used to pass any valid vite-plugin option. Fixes #15601 --- packages/astro/src/integration/index.ts | 3 +- packages/astro/src/integration/types.ts | 15 +++++++ packages/astro/test/integration/index.test.ts | 45 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index 79b74b8804c1..b94def710cfe 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -30,7 +30,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { }; const sourceMapsNeeded = sdkEnabled.client || sdkEnabled.server; - const uploadOptions = options.sourceMapsUploadOptions || {}; + const { unstable_sentryVitePluginOptions, ...uploadOptions } = options.sourceMapsUploadOptions || {}; const shouldUploadSourcemaps = (sourceMapsNeeded && uploadOptions?.enabled) ?? true; // We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env @@ -85,6 +85,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { }, }, debug: options.debug ?? false, + ...unstable_sentryVitePluginOptions }), ), ], diff --git a/packages/astro/src/integration/types.ts b/packages/astro/src/integration/types.ts index 6c2e41808eca..611594573bb8 100644 --- a/packages/astro/src/integration/types.ts +++ b/packages/astro/src/integration/types.ts @@ -1,5 +1,6 @@ import type { BrowserOptions } from '@sentry/browser'; import type { Options } from '@sentry/core'; +import type { SentryVitePluginOptions } from '@sentry/vite-plugin'; type SdkInitPaths = { /** @@ -83,6 +84,20 @@ type SourceMapsOptions = { * The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob) */ filesToDeleteAfterUpload?: string | Array; + + /** + * Options to further customize the Sentry Vite Plugin (@sentry/vite-plugin) behavior directly. + * Options specified in this object take precedence over all other options. + * + * @see https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#options which lists all available options. + * + * Warning: Options within this object are subject to change at any time. + * We DO NOT guarantee semantic versioning for these options, meaning breaking + * changes can occur at any time within a major SDK version. + * + * Furthermore, some options are untested with Astro specifically. Use with caution. + */ + unstable_sentryVitePluginOptions?: Partial }; type BundleSizeOptimizationOptions = { diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index 6684a841ba4e..b7a775b1be1d 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -202,6 +202,51 @@ describe('sentryAstro integration', () => { ); }); + it('prefers user-specified unstable vite plugin options over everything else', async () => { + const integration = sentryAstro({ + sourceMapsUploadOptions: { + enabled: true, + org: 'my-org', + project: 'my-project', + assets: ['dist/server/**/*, dist/client/**/*'], + debug: true, + unstable_sentryVitePluginOptions: { + org: 'my-other-org', + project: 'my-other-project', + applicationKey: 'my-application-key', + debug: false, + sourcemaps: { + assets: ['foo/*.js'], + ignore: ['bar/*.js'], + }, + } + }, + }); + // @ts-expect-error - the hook exists, and we only need to pass what we actually use + await integration.hooks['astro:config:setup']({ + updateConfig, + injectScript, + // @ts-expect-error - only passing in partial config + config: { + outDir: new URL('file://path/to/project/build'), + }, + }); + + expect(sentryVitePluginSpy).toHaveBeenCalledTimes(1); + expect(sentryVitePluginSpy).toHaveBeenCalledWith( + expect.objectContaining({ + org: 'my-other-org', + project: 'my-other-project', + applicationKey: 'my-application-key', + debug: false, + sourcemaps: { + assets: ['foo/*.js'], + ignore: ['bar/*.js'], + }, + }), + ); + }); + it("doesn't enable source maps if `sourceMapsUploadOptions.enabled` is `false`", async () => { const integration = sentryAstro({ sourceMapsUploadOptions: { enabled: false }, From 15198e30bd04de19245793d2aac688b5a03b044c Mon Sep 17 00:00:00 2001 From: Angelika Cathor Date: Wed, 12 Mar 2025 16:05:32 +0100 Subject: [PATCH 2/3] Don't overwrite nested default options --- packages/astro/src/integration/index.ts | 16 +++++++++------- packages/astro/test/integration/index.test.ts | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index b94def710cfe..14947362a2cc 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -68,24 +68,26 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { project: uploadOptions.project ?? env.SENTRY_PROJECT, authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN, telemetry: uploadOptions.telemetry ?? true, + _metaOptions: { + telemetry: { + metaFramework: 'astro', + }, + }, + ...unstable_sentryVitePluginOptions, + debug: options.debug ?? false, sourcemaps: { assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)], filesToDeleteAfterUpload: uploadOptions?.filesToDeleteAfterUpload ?? updatedFilesToDeleteAfterUpload, + ...unstable_sentryVitePluginOptions?.sourcemaps }, bundleSizeOptimizations: { ...options.bundleSizeOptimizations, // TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore // ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582 excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing, + ...unstable_sentryVitePluginOptions?.bundleSizeOptimizations }, - _metaOptions: { - telemetry: { - metaFramework: 'astro', - }, - }, - debug: options.debug ?? false, - ...unstable_sentryVitePluginOptions }), ), ], diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index b7a775b1be1d..fd2ffc919246 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -202,23 +202,27 @@ describe('sentryAstro integration', () => { ); }); - it('prefers user-specified unstable vite plugin options over everything else', async () => { + it('prefers user-specified unstable vite plugin options and merges them with default values', async () => { const integration = sentryAstro({ + bundleSizeOptimizations: { + excludeReplayShadowDom: true + }, sourceMapsUploadOptions: { enabled: true, org: 'my-org', project: 'my-project', assets: ['dist/server/**/*, dist/client/**/*'], - debug: true, unstable_sentryVitePluginOptions: { org: 'my-other-org', project: 'my-other-project', applicationKey: 'my-application-key', - debug: false, sourcemaps: { assets: ['foo/*.js'], ignore: ['bar/*.js'], }, + bundleSizeOptimizations: { + excludeReplayIframe: true + } } }, }); @@ -238,11 +242,15 @@ describe('sentryAstro integration', () => { org: 'my-other-org', project: 'my-other-project', applicationKey: 'my-application-key', - debug: false, sourcemaps: { assets: ['foo/*.js'], ignore: ['bar/*.js'], + filesToDeleteAfterUpload: ['./dist/**/client/**/*.map', './dist/**/server/**/*.map'], }, + bundleSizeOptimizations: { + excludeReplayShadowDom: true, + excludeReplayIframe: true + } }), ); }); From 09edcfa7f01bed7bb8cdcd24549c163924754302 Mon Sep 17 00:00:00 2001 From: Angelika Cathor Date: Wed, 12 Mar 2025 16:57:00 +0100 Subject: [PATCH 3/3] Fix formatting --- packages/astro/src/integration/index.ts | 4 ++-- packages/astro/src/integration/types.ts | 2 +- packages/astro/test/integration/index.test.ts | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index 14947362a2cc..fb26e956daeb 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -79,14 +79,14 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)], filesToDeleteAfterUpload: uploadOptions?.filesToDeleteAfterUpload ?? updatedFilesToDeleteAfterUpload, - ...unstable_sentryVitePluginOptions?.sourcemaps + ...unstable_sentryVitePluginOptions?.sourcemaps, }, bundleSizeOptimizations: { ...options.bundleSizeOptimizations, // TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore // ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582 excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing, - ...unstable_sentryVitePluginOptions?.bundleSizeOptimizations + ...unstable_sentryVitePluginOptions?.bundleSizeOptimizations, }, }), ), diff --git a/packages/astro/src/integration/types.ts b/packages/astro/src/integration/types.ts index 611594573bb8..5b5308e3a474 100644 --- a/packages/astro/src/integration/types.ts +++ b/packages/astro/src/integration/types.ts @@ -97,7 +97,7 @@ type SourceMapsOptions = { * * Furthermore, some options are untested with Astro specifically. Use with caution. */ - unstable_sentryVitePluginOptions?: Partial + unstable_sentryVitePluginOptions?: Partial; }; type BundleSizeOptimizationOptions = { diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index fd2ffc919246..01d86e85baee 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -205,7 +205,7 @@ describe('sentryAstro integration', () => { it('prefers user-specified unstable vite plugin options and merges them with default values', async () => { const integration = sentryAstro({ bundleSizeOptimizations: { - excludeReplayShadowDom: true + excludeReplayShadowDom: true, }, sourceMapsUploadOptions: { enabled: true, @@ -221,9 +221,9 @@ describe('sentryAstro integration', () => { ignore: ['bar/*.js'], }, bundleSizeOptimizations: { - excludeReplayIframe: true - } - } + excludeReplayIframe: true, + }, + }, }, }); // @ts-expect-error - the hook exists, and we only need to pass what we actually use @@ -249,8 +249,8 @@ describe('sentryAstro integration', () => { }, bundleSizeOptimizations: { excludeReplayShadowDom: true, - excludeReplayIframe: true - } + excludeReplayIframe: true, + }, }), ); });