Skip to content

Commit d3aa0af

Browse files
authored
feat(sveltekit): Export unstable_sentryVitePluginOptions for full Vite plugin customization (getsentry#10930)
We decided to provide an escape hatch to the reduced (but therefore version-agnostic) Vite plugin option API in the SvelteKit version of the plugin (`sentrySvelteKit()`). By specifying options in `unstable_vitePluginOptions`, users can still set and override all options the Vite plugin can take. We explicitly warn that keys in `unstable_vitePluginOptions` do not follow semver and might be removed or changed at any time.
1 parent 3f373f2 commit d3aa0af

File tree

3 files changed

+169
-3
lines changed

3 files changed

+169
-3
lines changed

Diff for: MIGRATION.md

+73
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ We now support the following integrations out of the box:
254254
- [Browser SDK](./MIGRATION.md#browser-sdk-browser-react-vue-angular-ember-etc)
255255
- [Server-side SDKs (Node, Deno, Bun)](./MIGRATION.md#server-side-sdks-node-deno-bun-etc)
256256
- [Next.js SDK](./MIGRATION.md#nextjs-sdk)
257+
- [SvelteKit SDK](./MIGRATION.md#sveltekit-sdk)
257258
- [Astro SDK](./MIGRATION.md#astro-sdk)
258259

259260
### General
@@ -581,6 +582,78 @@ Sentry.init({
581582
});
582583
```
583584

585+
### SvelteKit SDK
586+
587+
#### Breaking `sentrySvelteKit()` changes
588+
589+
We upgraded the `@sentry/vite-plugin` which is a dependency of the SvelteKit SDK from version 0.x to 2.x. With this
590+
change, resolving uploaded source maps should work out of the box much more often than before
591+
([more information](https://docs.sentry.io/platforms/javascript/sourcemaps/troubleshooting_js/artifact-bundles/)).
592+
593+
To allow future upgrades of the Vite plugin without breaking stable and public APIs in `sentrySvelteKit`, we modified
594+
the `sourceMapsUploadOptions` to remove the hard dependency on the API of the plugin. While you previously could specify
595+
all [version 0.x Vite plugin options](https://www.npmjs.com/package/@sentry/vite-plugin/v/0.6.1), we now reduced them to
596+
a subset of [2.x options](https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#options). All of these options are
597+
optional just like before but here's an example of using the new options.
598+
599+
```js
600+
// Before (v7):
601+
sentrySvelteKit({
602+
sourceMapsUploadOptions: {
603+
org: process.env.SENTRY_ORG,
604+
project: process.env.SENTRY_PROJECT,
605+
authToken: process.env.SENTRY_AUTH_TOKEN,
606+
release: '1.0.1',
607+
injectRelease: true,
608+
include: ['./build/*/**/*'],
609+
ignore: ['**/build/client/**/*']
610+
},
611+
}),
612+
613+
// After (v8):
614+
sentrySvelteKit({
615+
sourceMapsUploadOptions: {
616+
org: process.env.SENTRY_ORG,
617+
project: process.env.SENTRY_PROJECT,
618+
authToken: process.env.SENTRY_AUTH_TOKEN,
619+
release: {
620+
name: '1.0.1',
621+
inject: true
622+
},
623+
sourcemaps: {
624+
assets: ['./build/*/**/*'],
625+
ignore: ['**/build/client/**/*'],
626+
filesToDeleteAfterUpload: ['./build/**/*.map']
627+
},
628+
},
629+
}),
630+
```
631+
632+
In the future, we might add additional [options](https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#options)
633+
from the Vite plugin but if you would like to specify some of them directly, you can do this by passing in an
634+
`unstable_sentryVitePluginOptions` object:
635+
636+
```js
637+
sentrySvelteKit({
638+
sourceMapsUploadOptions: {
639+
// ...
640+
release: {
641+
name: '1.0.1',
642+
},
643+
unstable_sentryVitePluginOptions: {
644+
release: {
645+
setCommits: {
646+
auto: true
647+
}
648+
}
649+
}
650+
},
651+
}),
652+
```
653+
654+
Important: we DO NOT guarantee stability of `unstable_sentryVitePluginOptions`. They can be removed or updated at any
655+
time, including breaking changes within the same major version of the SDK.
656+
584657
## 5. Behaviour Changes
585658

586659
- [Updated behaviour of `tracePropagationTargets` in the browser](./MIGRATION.md#updated-behaviour-of-tracepropagationtargets-in-the-browser-http-tracing-headers--cors)

Diff for: packages/sveltekit/src/vite/sentryVitePlugins.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Plugin } from 'vite';
22

3+
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
34
import type { AutoInstrumentSelection } from './autoInstrument';
45
import { makeAutoInstrumentationPlugin } from './autoInstrument';
56
import type { SupportedSvelteKitAdapters } from './detectAdapter';
@@ -114,6 +115,21 @@ type SourceMapsUploadOptions = {
114115
*/
115116
inject?: boolean;
116117
};
118+
119+
/**
120+
* Options to further customize the Sentry Vite Plugin (@sentry/vite-plugin) behavior directly.
121+
* Options specified in this object take precedence over the options specified in
122+
* the `sourcemaps` and `release` objects.
123+
*
124+
* @see https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#options which lists all available options.
125+
*
126+
* Warning: Options within this object are subject to change at any time.
127+
* We DO NOT guarantee semantic versioning for these options, meaning breaking
128+
* changes can occur at any time within a major SDK version.
129+
*
130+
* Furthermore, some options are untested with SvelteKit specifically. Use with caution.
131+
*/
132+
unstable_sentryVitePluginOptions?: Partial<SentryVitePluginOptions>;
117133
};
118134
};
119135

@@ -196,15 +212,35 @@ export async function sentrySvelteKit(options: SentrySvelteKitPluginOptions = {}
196212
}
197213

198214
if (mergedOptions.autoUploadSourceMaps && process.env.NODE_ENV !== 'development') {
199-
const sourceMapsUploadOptions = mergedOptions.sourceMapsUploadOptions;
215+
const { unstable_sentryVitePluginOptions, ...sourceMapsUploadOptions } =
216+
mergedOptions.sourceMapsUploadOptions || {};
200217

201-
const sentryVitePlugins = await makeCustomSentryVitePlugins({
218+
const sentryVitePluginsOptions = {
202219
...sourceMapsUploadOptions,
203220

221+
...unstable_sentryVitePluginOptions,
222+
204223
adapter: mergedOptions.adapter,
205224
// override the plugin's debug flag with the one from the top-level options
206225
debug: mergedOptions.debug,
207-
});
226+
};
227+
228+
if (sentryVitePluginsOptions.sourcemaps) {
229+
sentryVitePluginsOptions.sourcemaps = {
230+
...sourceMapsUploadOptions?.sourcemaps,
231+
...unstable_sentryVitePluginOptions?.sourcemaps,
232+
};
233+
}
234+
235+
if (sentryVitePluginsOptions.release) {
236+
sentryVitePluginsOptions.release = {
237+
...sourceMapsUploadOptions?.release,
238+
...unstable_sentryVitePluginOptions?.release,
239+
};
240+
}
241+
242+
const sentryVitePlugins = await makeCustomSentryVitePlugins(sentryVitePluginsOptions);
243+
208244
sentryPlugins.push(...sentryVitePlugins);
209245
}
210246

Diff for: packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts

+57
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,63 @@ describe('sentrySvelteKit()', () => {
113113
});
114114
});
115115

116+
it('passes user-specified vite plugin options to the custom sentry source maps plugin', async () => {
117+
const makePluginSpy = vi.spyOn(sourceMaps, 'makeCustomSentryVitePlugins');
118+
await getSentrySvelteKitPlugins({
119+
debug: true,
120+
sourceMapsUploadOptions: {
121+
org: 'my-org',
122+
sourcemaps: {
123+
assets: ['nope/*.js'],
124+
filesToDeleteAfterUpload: ['baz/*.js'],
125+
},
126+
release: {
127+
inject: false,
128+
name: '2.0.0',
129+
},
130+
unstable_sentryVitePluginOptions: {
131+
org: 'other-org',
132+
sourcemaps: {
133+
assets: ['foo/*.js'],
134+
ignore: ['bar/*.js'],
135+
},
136+
release: {
137+
name: '3.0.0',
138+
setCommits: {
139+
auto: true,
140+
},
141+
},
142+
headers: {
143+
'X-My-Header': 'foo',
144+
},
145+
},
146+
},
147+
autoInstrument: false,
148+
adapter: 'vercel',
149+
});
150+
151+
expect(makePluginSpy).toHaveBeenCalledWith({
152+
debug: true,
153+
org: 'other-org',
154+
sourcemaps: {
155+
assets: ['foo/*.js'],
156+
ignore: ['bar/*.js'],
157+
filesToDeleteAfterUpload: ['baz/*.js'],
158+
},
159+
release: {
160+
inject: false,
161+
name: '3.0.0',
162+
setCommits: {
163+
auto: true,
164+
},
165+
},
166+
headers: {
167+
'X-My-Header': 'foo',
168+
},
169+
adapter: 'vercel',
170+
});
171+
});
172+
116173
it('passes user-specified options to the auto instrument plugin', async () => {
117174
const makePluginSpy = vi.spyOn(autoInstrument, 'makeAutoInstrumentationPlugin');
118175
const plugins = await getSentrySvelteKitPlugins({

0 commit comments

Comments
 (0)