@@ -60,9 +60,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
60
60
} ,
61
61
} ;
62
62
63
- const { promise : filesToDeleteAfterUpload , resolve : resolveFilesToDeleteAfterUpload } =
64
- createFilesToDeleteAfterUploadPromise ( ) ;
65
-
66
63
const mergedOptions = {
67
64
...defaultPluginOptions ,
68
65
...options ,
@@ -72,7 +69,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
72
69
} ,
73
70
sourcemaps : {
74
71
...options ?. sourcemaps ,
75
- filesToDeleteAfterUpload,
76
72
} ,
77
73
} ;
78
74
@@ -99,9 +95,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
99
95
'sentry-vite-debug-id-upload-plugin not found in sentryPlugins! Cannot modify plugin - returning default Sentry Vite plugins' ,
100
96
) ;
101
97
102
- // resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
103
- resolveFilesToDeleteAfterUpload ( undefined ) ;
104
-
105
98
return sentryPlugins ;
106
99
}
107
100
@@ -112,9 +105,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
112
105
'sentry-file-deletion-plugin not found in sentryPlugins! Cannot modify plugin - returning default Sentry Vite plugins' ,
113
106
) ;
114
107
115
- // resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
116
- resolveFilesToDeleteAfterUpload ( undefined ) ;
117
-
118
108
return sentryPlugins ;
119
109
}
120
110
@@ -125,9 +115,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
125
115
'sentry-release-management-plugin not found in sentryPlugins! Cannot modify plugin - returning default Sentry Vite plugins' ,
126
116
) ;
127
117
128
- // resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
129
- resolveFilesToDeleteAfterUpload ( undefined ) ;
130
-
131
118
return sentryPlugins ;
132
119
}
133
120
@@ -153,65 +140,51 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
153
140
name : 'sentry-sveltekit-update-source-map-setting-plugin' ,
154
141
apply : 'build' , // only apply this plugin at build time
155
142
config : async ( config : UserConfig ) => {
156
- const settingKey = 'build.sourcemap' ;
157
-
158
- const { updatedSourceMapSetting, previousSourceMapSetting } = getUpdatedSourceMapSetting ( config ) ;
143
+ return {
144
+ ...config ,
145
+ build : {
146
+ ...config . build ,
147
+ sourcemap : _getUpdatedSourceMapSettings ( config , options ) ,
148
+ } ,
149
+ } ;
150
+ } ,
151
+ } ;
159
152
160
- const userProvidedFilesToDeleteAfterUpload = await options ?. sourcemaps ?. filesToDeleteAfterUpload ;
153
+ const filesToDeleteAfterUploadConfigPlugin : Plugin = {
154
+ name : 'sentry-sveltekit-files-to-delete-after-upload-setting-plugin' ,
155
+ apply : 'build' , // only apply this plugin at build time
156
+ config : async ( config : UserConfig ) => {
157
+ const filesToDeleteAfterUpload = mergedOptions . sourcemaps ?. filesToDeleteAfterUpload ;
158
+
159
+ if (
160
+ typeof filesToDeleteAfterUpload === 'undefined' &&
161
+ // Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden")
162
+ typeof config . build ?. sourcemap === 'undefined'
163
+ ) {
164
+ // Including all hidden (`.*`) directories by default so that folders like .vercel,
165
+ // .netlify, etc are also cleaned up. Additionally, we include the adapter output
166
+ // dir which could be a non-hidden directory, like `build` for the Node adapter.
167
+ const defaultFileDeletionGlob = [ './.*/**/*.map' , `./${ adapterOutputDir } /**/*.map` ] ;
161
168
162
- if ( previousSourceMapSetting === 'unset' ) {
163
169
consoleSandbox ( ( ) => {
164
- // eslint-disable-next-line no-console
165
- console . log ( `[Sentry] Enabled source map generation in the build options with \`${ settingKey } : "hidden"\`.` ) ;
166
- } ) ;
167
-
168
- if ( userProvidedFilesToDeleteAfterUpload ) {
169
- resolveFilesToDeleteAfterUpload ( userProvidedFilesToDeleteAfterUpload ) ;
170
- } else {
171
- // Including all hidden (`.*`) directories by default so that folders like .vercel,
172
- // .netlify, etc are also cleaned up. Additionally, we include the adapter output
173
- // dir which could be a non-hidden directory, like `build` for the Node adapter.
174
- const defaultFileDeletionGlob = [ './.*/**/*.map' , `./${ adapterOutputDir } /**/*.map` ] ;
175
-
176
- consoleSandbox ( ( ) => {
170
+ debug &&
177
171
// eslint-disable-next-line no-console
178
- console . warn (
172
+ console . info (
179
173
`[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${ defaultFileDeletionGlob
180
174
. map ( file => `"${ file } "` )
181
175
. join ( ', ' ) } ]\` to delete generated source maps after they were uploaded to Sentry.`,
182
176
) ;
183
- } ) ;
184
-
185
- // In case we enabled source maps and users didn't specify a glob patter to delete, we set a default pattern:
186
- resolveFilesToDeleteAfterUpload ( defaultFileDeletionGlob ) ;
187
- }
177
+ } ) ;
188
178
189
179
return {
190
180
...config ,
191
- build : { ...config . build , sourcemap : updatedSourceMapSetting } ,
181
+ build : {
182
+ ...config . build ,
183
+ filesToDeleteAfterUpload : defaultFileDeletionGlob ,
184
+ } ,
192
185
} ;
193
186
}
194
187
195
- if ( previousSourceMapSetting === 'disabled' ) {
196
- consoleSandbox ( ( ) => {
197
- // eslint-disable-next-line no-console
198
- console . warn (
199
- `[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${ settingKey } : false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${ settingKey } \` (e.g. by setting them to \`hidden\`).` ,
200
- ) ;
201
- } ) ;
202
- } else if ( previousSourceMapSetting === 'enabled' ) {
203
- if ( mergedOptions ?. debug ) {
204
- consoleSandbox ( ( ) => {
205
- // eslint-disable-next-line no-console
206
- console . log (
207
- `[Sentry] We discovered you enabled source map generation in your Vite configuration (\`${ settingKey } \`). Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.` ,
208
- ) ;
209
- } ) ;
210
- }
211
- }
212
-
213
- resolveFilesToDeleteAfterUpload ( userProvidedFilesToDeleteAfterUpload ) ;
214
-
215
188
return config ;
216
189
} ,
217
190
} ;
@@ -390,18 +363,30 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
390
363
return [
391
364
...unchangedSentryVitePlugins ,
392
365
sourceMapSettingsPlugin ,
366
+ filesToDeleteAfterUploadConfigPlugin ,
393
367
customReleaseManagementPlugin ,
394
368
customDebugIdUploadPlugin ,
395
369
customFileDeletionPlugin ,
396
370
] ;
397
371
}
398
372
399
- /**
400
- * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps
373
+ /** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993)
374
+ *
375
+ * 1. User explicitly disabled source maps
376
+ * - keep this setting (emit a warning that errors won't be unminified in Sentry)
377
+ * - We won't upload anything
378
+ *
379
+ * 2. Users enabled source map generation (true, 'hidden', 'inline').
380
+ * - keep this setting (don't do anything - like deletion - besides uploading)
381
+ *
382
+ * 3. Users didn't set source maps generation
383
+ * - we enable 'hidden' source maps generation
384
+ * - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this)
385
+ *
386
+ * --> only exported for testing
401
387
*/
402
- type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined ;
403
388
404
- /** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript /issues/13993)
389
+ /** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-j avascript /issues/13993)
405
390
*
406
391
* 1. User explicitly disabled source maps
407
392
* - keep this setting (emit a warning that errors won't be unminified in Sentry)
@@ -416,30 +401,50 @@ type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
416
401
*
417
402
* --> only exported for testing
418
403
*/
419
- export function getUpdatedSourceMapSetting ( viteConfig : {
420
- build ?: {
421
- sourcemap ?: boolean | 'inline' | 'hidden' ;
422
- } ;
423
- } ) : { updatedSourceMapSetting : boolean | 'inline' | 'hidden' ; previousSourceMapSetting : UserSourceMapSetting } {
404
+ export function _getUpdatedSourceMapSettings (
405
+ viteConfig : UserConfig ,
406
+ sentryPluginOptions ?: CustomSentryVitePluginOptions ,
407
+ ) : boolean | 'inline' | 'hidden' {
424
408
viteConfig . build = viteConfig . build || { } ;
425
409
426
- const originalSourcemapSetting = viteConfig . build . sourcemap ;
410
+ const viteSourceMap = viteConfig ?. build ?. sourcemap ;
411
+ let updatedSourceMapSetting = viteSourceMap ;
427
412
428
- if ( originalSourcemapSetting === false ) {
429
- return {
430
- previousSourceMapSetting : 'disabled' ,
431
- updatedSourceMapSetting : originalSourcemapSetting ,
432
- } ;
433
- }
413
+ const settingKey = 'build.sourcemap' ;
414
+ const debug = sentryPluginOptions ?. debug ;
415
+
416
+ if ( viteSourceMap === false ) {
417
+ updatedSourceMapSetting = viteSourceMap ;
434
418
435
- if ( originalSourcemapSetting && [ 'hidden' , 'inline' , true ] . includes ( originalSourcemapSetting ) ) {
436
- return { previousSourceMapSetting : 'enabled' , updatedSourceMapSetting : originalSourcemapSetting } ;
419
+ if ( debug ) {
420
+ // Longer debug message with more details
421
+ // eslint-disable-next-line no-console
422
+ console . warn (
423
+ `[Sentry] Source map generation is currently disabled in your Vite configuration (\`${ settingKey } : false \`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${ settingKey } \` (e.g. by setting them to \`hidden\`).` ,
424
+ ) ;
425
+ } else {
426
+ // eslint-disable-next-line no-console
427
+ console . warn ( '[Sentry] Source map generation is disabled in your Vite configuration.' ) ;
428
+ }
429
+ } else if ( viteSourceMap && [ 'hidden' , 'inline' , true ] . includes ( viteSourceMap ) ) {
430
+ updatedSourceMapSetting = viteSourceMap ;
431
+
432
+ debug &&
433
+ // eslint-disable-next-line no-console
434
+ console . log (
435
+ `[Sentry] We discovered \`${ settingKey } \` is set to \`${ viteSourceMap . toString ( ) } \`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.` ,
436
+ ) ;
437
+ } else {
438
+ updatedSourceMapSetting = 'hidden' ;
439
+
440
+ debug &&
441
+ // eslint-disable-next-line no-console
442
+ console . log (
443
+ `[Sentry] Enabled source map generation in the build options with \`${ settingKey } : 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.` ,
444
+ ) ;
437
445
}
438
446
439
- return {
440
- previousSourceMapSetting : 'unset' ,
441
- updatedSourceMapSetting : 'hidden' ,
442
- } ;
447
+ return updatedSourceMapSetting ;
443
448
}
444
449
445
450
function getFiles ( dir : string ) : string [ ] {
@@ -475,22 +480,3 @@ function detectSentryRelease(): string {
475
480
476
481
return release ;
477
482
}
478
-
479
- /**
480
- * Creates a deferred promise that can be resolved/rejected by calling the
481
- * `resolve` or `reject` function.
482
- * Inspired by: https://stackoverflow.com/a/69027809
483
- */
484
- function createFilesToDeleteAfterUploadPromise ( ) : {
485
- promise : Promise < string | string [ ] | undefined > ;
486
- resolve : ( value : string | string [ ] | undefined ) => void ;
487
- reject : ( reason ?: unknown ) => void ;
488
- } {
489
- let resolve ! : ( value : string | string [ ] | undefined ) => void ;
490
- let reject ! : ( reason ?: unknown ) => void ;
491
- const promise = new Promise < string | string [ ] | undefined > ( ( res , rej ) => {
492
- resolve = res ;
493
- reject = rej ;
494
- } ) ;
495
- return { resolve, reject, promise } ;
496
- }
0 commit comments