Skip to content

feat(astro): Accept all vite-plugin options #15638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,23 +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,
}),
),
],
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/src/integration/types.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand Down Expand Up @@ -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<string>;

/**
* 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<SentryVitePluginOptions>;
};

type BundleSizeOptimizationOptions = {
Expand Down
53 changes: 53 additions & 0 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,59 @@ describe('sentryAstro integration', () => {
);
});

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/**/*'],
unstable_sentryVitePluginOptions: {
org: 'my-other-org',
project: 'my-other-project',
applicationKey: 'my-application-key',
sourcemaps: {
assets: ['foo/*.js'],
ignore: ['bar/*.js'],
},
bundleSizeOptimizations: {
excludeReplayIframe: true,
},
},
},
});
// @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',
sourcemaps: {
assets: ['foo/*.js'],
ignore: ['bar/*.js'],
filesToDeleteAfterUpload: ['./dist/**/client/**/*.map', './dist/**/server/**/*.map'],
},
bundleSizeOptimizations: {
excludeReplayShadowDom: true,
excludeReplayIframe: true,
},
}),
);
});

it("doesn't enable source maps if `sourceMapsUploadOptions.enabled` is `false`", async () => {
const integration = sentryAstro({
sourceMapsUploadOptions: { enabled: false },
Expand Down
Loading