1
+ import { dropUndefinedKeys } from '@sentry/utils' ;
1
2
import type { Plugin } from 'vite' ;
2
-
3
- import type { SentryVitePluginOptions } from '@sentry/vite-plugin' ;
4
3
import type { AutoInstrumentSelection } from './autoInstrument' ;
5
4
import { makeAutoInstrumentationPlugin } from './autoInstrument' ;
6
- import type { SupportedSvelteKitAdapters } from './detectAdapter' ;
7
5
import { detectAdapter } from './detectAdapter' ;
8
6
import { makeCustomSentryVitePlugins } from './sourceMaps' ;
9
-
10
- /**
11
- * Options related to source maps upload to Sentry
12
- */
13
- type SourceMapsUploadOptions = {
14
- /**
15
- * If this flag is `true`, the Sentry plugins will automatically upload source maps to Sentry.
16
- * @default true`.
17
- */
18
- autoUploadSourceMaps ?: boolean ;
19
-
20
- /**
21
- * Options for the Sentry Vite plugin to customize and override the release creation and source maps upload process.
22
- * See [Sentry Vite Plugin Options](https://github.com/getsentry/sentry-javascript-bundler-plugins/tree/main/packages/vite-plugin#configuration) for a detailed description.
23
- */
24
- sourceMapsUploadOptions ?: {
25
- /**
26
- * The auth token to use when uploading source maps to Sentry.
27
- *
28
- * Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
29
- *
30
- * To create an auth token, follow this guide:
31
- * @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
32
- */
33
- authToken ?: string ;
34
-
35
- /**
36
- * The organization slug of your Sentry organization.
37
- * Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
38
- */
39
- org ?: string ;
40
-
41
- /**
42
- * The project slug of your Sentry project.
43
- * Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
44
- */
45
- project ?: string ;
46
-
47
- /**
48
- * If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
49
- * It will not collect any sensitive or user-specific data.
50
- *
51
- * @default true
52
- */
53
- telemetry ?: boolean ;
54
-
55
- /**
56
- * Options related to sourcemaps
57
- */
58
- sourcemaps ?: {
59
- /**
60
- * A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
61
- *
62
- * If this option is not specified, sensible defaults based on your adapter and svelte.config.js
63
- * setup will be used. Use this option to override these defaults, for instance if you have a
64
- * customized build setup that diverges from SvelteKit's defaults.
65
- *
66
- * The globbing patterns must follow the implementation of the `glob` package.
67
- * @see https://www.npmjs.com/package/glob#glob-primer
68
- */
69
- assets ?: string | Array < string > ;
70
-
71
- /**
72
- * A glob or an array of globs that specifies which build artifacts should not be uploaded to Sentry.
73
- *
74
- * @default [] - By default no files are ignored. Thus, all files matching the `assets` glob
75
- * or the default value for `assets` are uploaded.
76
- *
77
- * The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
78
- */
79
- ignore ?: string | Array < string > ;
80
-
81
- /**
82
- * A glob or an array of globs that specifies the build artifacts that should be deleted after the artifact
83
- * upload to Sentry has been completed.
84
- *
85
- * @default [] - By default no files are deleted.
86
- *
87
- * The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
88
- */
89
- filesToDeleteAfterUpload ?: string | Array < string > ;
90
- } ;
91
-
92
- /**
93
- * Options related to managing the Sentry releases for a build.
94
- *
95
- * Note: Managing releases is optional and not required for uploading source maps.
96
- */
97
- release ?: {
98
- /**
99
- * Unique identifier for the release you want to create.
100
- * This value can also be specified via the SENTRY_RELEASE environment variable.
101
- *
102
- * Defaults to automatically detecting a value for your environment. This includes values for Cordova, Heroku,
103
- * AWS CodeBuild, CircleCI, Xcode, and Gradle, and otherwise uses the git HEAD's commit SHA (the latter requires
104
- * access to git CLI and for the root directory to be a valid repository).
105
- *
106
- * If you didn't provide a value and the plugin can't automatically detect one, no release will be created.
107
- */
108
- name ?: string ;
109
-
110
- /**
111
- * Whether the plugin should inject release information into the build for the SDK to pick it up when
112
- * sending events.
113
- *
114
- * Defaults to `true`.
115
- */
116
- inject ?: boolean ;
117
- } ;
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 > ;
133
- } ;
134
- } ;
135
-
136
- type AutoInstrumentOptions = {
137
- /**
138
- * The Sentry plugin will automatically instrument certain parts of your SvelteKit application at build time.
139
- * Set this option to `false` to disable this behavior or what is instrumentated by passing an object.
140
- *
141
- * Auto instrumentation includes:
142
- * - Universal `load` functions in `+page.(js|ts)` files
143
- * - Server-only `load` functions in `+page.server.(js|ts)` files
144
- *
145
- * @default true (meaning, the plugin will instrument all of the above)
146
- */
147
- autoInstrument ?: boolean | AutoInstrumentSelection ;
148
- } ;
149
-
150
- export type SentrySvelteKitPluginOptions = {
151
- /**
152
- * If this flag is `true`, the Sentry plugins will log some useful debug information.
153
- * @default false.
154
- */
155
- debug ?: boolean ;
156
-
157
- /**
158
- * Specify which SvelteKit adapter you're using.
159
- * By default, the SDK will attempt auto-detect the used adapter at build time and apply the
160
- * correct config for source maps upload or auto-instrumentation.
161
- *
162
- * Currently, the SDK supports the following adapters:
163
- * - node (@sveltejs/adapter-node)
164
- * - auto (@sveltejs/adapter-auto) only Vercel
165
- * - vercel (@sveltejs/adapter-auto) only Serverless functions, no edge runtime
166
- *
167
- * Set this option, if the SDK detects the wrong adapter or you want to use an adapter
168
- * that is not in this list. If you specify 'other', you'll most likely need to configure
169
- * source maps upload yourself.
170
- *
171
- * @default {} the SDK attempts to auto-detect the used adapter at build time
172
- */
173
- adapter ?: SupportedSvelteKitAdapters ;
174
- } & SourceMapsUploadOptions &
175
- AutoInstrumentOptions ;
7
+ import type { CustomSentryVitePluginOptions , SentrySvelteKitPluginOptions } from './types' ;
176
8
177
9
const DEFAULT_PLUGIN_OPTIONS : SentrySvelteKitPluginOptions = {
178
10
autoUploadSourceMaps : true ,
@@ -211,18 +43,50 @@ export async function sentrySvelteKit(options: SentrySvelteKitPluginOptions = {}
211
43
) ;
212
44
}
213
45
214
- if ( mergedOptions . autoUploadSourceMaps && process . env . NODE_ENV !== 'development' ) {
46
+ const sentryVitePluginsOptions = generateVitePluginOptions ( mergedOptions ) ;
47
+
48
+ if ( sentryVitePluginsOptions ) {
49
+ const sentryVitePlugins = await makeCustomSentryVitePlugins ( sentryVitePluginsOptions ) ;
50
+
51
+ sentryPlugins . push ( ...sentryVitePlugins ) ;
52
+ }
53
+
54
+ return sentryPlugins ;
55
+ }
56
+
57
+ /**
58
+ * This function creates the options for the custom Sentry Vite plugin.
59
+ * The options are derived from the Sentry SvelteKit plugin options, where the `_unstable` options take precedence.
60
+ *
61
+ * only exported for testing
62
+ */
63
+ export function generateVitePluginOptions (
64
+ svelteKitPluginOptions : SentrySvelteKitPluginOptions ,
65
+ ) : CustomSentryVitePluginOptions | null {
66
+ let sentryVitePluginsOptions : CustomSentryVitePluginOptions | null = null ;
67
+
68
+ // Bundle Size Optimizations
69
+ if ( svelteKitPluginOptions . bundleSizeOptimizations ) {
70
+ sentryVitePluginsOptions = {
71
+ bundleSizeOptimizations : {
72
+ ...svelteKitPluginOptions . bundleSizeOptimizations ,
73
+ } ,
74
+ } ;
75
+ }
76
+
77
+ // Source Maps
78
+ if ( svelteKitPluginOptions . autoUploadSourceMaps && process . env . NODE_ENV !== 'development' ) {
215
79
const { unstable_sentryVitePluginOptions, ...sourceMapsUploadOptions } =
216
- mergedOptions . sourceMapsUploadOptions || { } ;
80
+ svelteKitPluginOptions . sourceMapsUploadOptions || { } ;
217
81
218
- const sentryVitePluginsOptions = {
219
- ...sourceMapsUploadOptions ,
82
+ sentryVitePluginsOptions = {
83
+ ...( sentryVitePluginsOptions ? sentryVitePluginsOptions : { } ) ,
220
84
85
+ ...sourceMapsUploadOptions ,
221
86
...unstable_sentryVitePluginOptions ,
222
-
223
- adapter : mergedOptions . adapter ,
87
+ adapter : svelteKitPluginOptions . adapter ,
224
88
// override the plugin's debug flag with the one from the top-level options
225
- debug : mergedOptions . debug ,
89
+ debug : svelteKitPluginOptions . debug ,
226
90
} ;
227
91
228
92
if ( sentryVitePluginsOptions . sourcemaps ) {
@@ -238,11 +102,7 @@ export async function sentrySvelteKit(options: SentrySvelteKitPluginOptions = {}
238
102
...unstable_sentryVitePluginOptions ?. release ,
239
103
} ;
240
104
}
241
-
242
- const sentryVitePlugins = await makeCustomSentryVitePlugins ( sentryVitePluginsOptions ) ;
243
-
244
- sentryPlugins . push ( ...sentryVitePlugins ) ;
245
105
}
246
106
247
- return sentryPlugins ;
107
+ return dropUndefinedKeys ( sentryVitePluginsOptions ) ;
248
108
}
0 commit comments