1
1
/* eslint-disable max-lines */
2
- import { consoleSandbox , escapeStringForRegex , uuid4 } from '@sentry/core' ;
2
+ import { escapeStringForRegex , uuid4 } from '@sentry/core' ;
3
3
import { getSentryRelease } from '@sentry/node' ;
4
4
import type { SentryVitePluginOptions } from '@sentry/vite-plugin' ;
5
5
import { sentryVitePlugin } from '@sentry/vite-plugin' ;
@@ -27,6 +27,8 @@ type Sorcery = {
27
27
// and we only want to generate a uuid once in case we have to fall back to it.
28
28
const releaseName = detectSentryRelease ( ) ;
29
29
30
+ type FilesToDeleteAfterUpload = string | string [ ] | undefined ;
31
+
30
32
/**
31
33
* Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create
32
34
* releases and upload source maps to Sentry.
@@ -60,8 +62,12 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
60
62
} ,
61
63
} ;
62
64
63
- const { promise : filesToDeleteAfterUpload , resolve : resolveFilesToDeleteAfterUpload } =
64
- createFilesToDeleteAfterUploadPromise ( ) ;
65
+ let _resolveFilesToDeleteAfterUpload :
66
+ | undefined
67
+ | ( ( value : FilesToDeleteAfterUpload | Promise < FilesToDeleteAfterUpload > ) => void ) ;
68
+ const filesToDeleteAfterUploadPromise = new Promise < FilesToDeleteAfterUpload > ( resolve => {
69
+ _resolveFilesToDeleteAfterUpload = resolve ;
70
+ } ) ;
65
71
66
72
const mergedOptions = {
67
73
...defaultPluginOptions ,
@@ -72,7 +78,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
72
78
} ,
73
79
sourcemaps : {
74
80
...options ?. sourcemaps ,
75
- filesToDeleteAfterUpload,
81
+ filesToDeleteAfterUpload : filesToDeleteAfterUploadPromise ,
76
82
} ,
77
83
} ;
78
84
@@ -100,7 +106,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
100
106
) ;
101
107
102
108
// resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
103
- resolveFilesToDeleteAfterUpload ( undefined ) ;
109
+ _resolveFilesToDeleteAfterUpload ?. ( undefined ) ;
104
110
105
111
return sentryPlugins ;
106
112
}
@@ -113,7 +119,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
113
119
) ;
114
120
115
121
// resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
116
- resolveFilesToDeleteAfterUpload ( undefined ) ;
122
+ _resolveFilesToDeleteAfterUpload ?. ( undefined ) ;
117
123
118
124
return sentryPlugins ;
119
125
}
@@ -126,7 +132,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
126
132
) ;
127
133
128
134
// resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
129
- resolveFilesToDeleteAfterUpload ( undefined ) ;
135
+ _resolveFilesToDeleteAfterUpload ?. ( undefined ) ;
130
136
131
137
return sentryPlugins ;
132
138
}
@@ -153,64 +159,40 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
153
159
name : 'sentry-sveltekit-update-source-map-setting-plugin' ,
154
160
apply : 'build' , // only apply this plugin at build time
155
161
config : async ( config : UserConfig ) => {
156
- const settingKey = 'build.sourcemap' ;
157
-
158
- const { updatedSourceMapSetting, previousSourceMapSetting } = getUpdatedSourceMapSetting ( config ) ;
159
-
160
- const userProvidedFilesToDeleteAfterUpload = await options ?. sourcemaps ?. filesToDeleteAfterUpload ;
161
-
162
- if ( previousSourceMapSetting === 'unset' ) {
163
- 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 ( ( ) => {
177
- // eslint-disable-next-line no-console
178
- console . warn (
179
- `[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${ defaultFileDeletionGlob
180
- . map ( file => `"${ file } "` )
181
- . join ( ', ' ) } ]\` to delete generated source maps after they were uploaded to Sentry.`,
182
- ) ;
183
- } ) ;
162
+ return {
163
+ ...config ,
164
+ build : {
165
+ ...config . build ,
166
+ sourcemap : _getUpdatedSourceMapSettings ( config , options ) ,
167
+ } ,
168
+ } ;
169
+ } ,
170
+ } ;
184
171
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
- }
172
+ const filesToDeleteAfterUploadConfigPlugin : Plugin = {
173
+ name : 'sentry-sveltekit-files-to-delete-after-upload-setting-plugin' ,
174
+ apply : 'build' , // only apply this plugin at build time
175
+ config : ( config : UserConfig ) => {
176
+ const originalFilesToDeleteAfterUpload = options ?. sourcemaps ?. filesToDeleteAfterUpload ;
188
177
189
- return {
190
- ... config ,
191
- build : { ... config . build , sourcemap : updatedSourceMapSetting } ,
192
- } ;
193
- }
178
+ if ( typeof originalFilesToDeleteAfterUpload === 'undefined' && typeof config . build ?. sourcemap === 'undefined' ) {
179
+ // Including all hidden (`.*`) directories by default so that folders like .vercel ,
180
+ // .netlify, etc are also cleaned up. Additionally, we include the adapter output
181
+ // dir which could be a non-hidden directory, like `build` for the Node adapter.
182
+ const defaultFileDeletionGlob = [ './.*/**/*.map' , `./ ${ adapterOutputDir } /**/*.map` ] ;
194
183
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\`).` ,
184
+ debug &&
185
+ // eslint-disable-next-line no-console
186
+ console . info (
187
+ `[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${ defaultFileDeletionGlob
188
+ . map ( file => `"${ file } "` )
189
+ . join ( ', ' ) } ]\` to delete generated source maps after they were uploaded to Sentry.`,
200
190
) ;
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
191
213
- resolveFilesToDeleteAfterUpload ( userProvidedFilesToDeleteAfterUpload ) ;
192
+ _resolveFilesToDeleteAfterUpload ?.( defaultFileDeletionGlob ) ;
193
+ } else {
194
+ _resolveFilesToDeleteAfterUpload ?.( originalFilesToDeleteAfterUpload ) ;
195
+ }
214
196
215
197
return config ;
216
198
} ,
@@ -390,18 +372,14 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
390
372
return [
391
373
...unchangedSentryVitePlugins ,
392
374
sourceMapSettingsPlugin ,
375
+ filesToDeleteAfterUploadConfigPlugin ,
393
376
customReleaseManagementPlugin ,
394
377
customDebugIdUploadPlugin ,
395
378
customFileDeletionPlugin ,
396
379
] ;
397
380
}
398
381
399
- /**
400
- * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps
401
- */
402
- type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined ;
403
-
404
- /** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993)
382
+ /** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-j avascript/issues/13993)
405
383
*
406
384
* 1. User explicitly disabled source maps
407
385
* - keep this setting (emit a warning that errors won't be unminified in Sentry)
@@ -416,30 +394,50 @@ type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
416
394
*
417
395
* --> only exported for testing
418
396
*/
419
- export function getUpdatedSourceMapSetting ( viteConfig : {
420
- build ?: {
421
- sourcemap ?: boolean | 'inline' | 'hidden' ;
422
- } ;
423
- } ) : { updatedSourceMapSetting : boolean | 'inline' | 'hidden' ; previousSourceMapSetting : UserSourceMapSetting } {
397
+ export function _getUpdatedSourceMapSettings (
398
+ viteConfig : UserConfig ,
399
+ sentryPluginOptions ?: CustomSentryVitePluginOptions ,
400
+ ) : boolean | 'inline' | 'hidden' {
424
401
viteConfig . build = viteConfig . build || { } ;
425
402
426
- const originalSourcemapSetting = viteConfig . build . sourcemap ;
403
+ const viteSourceMap = viteConfig ?. build ?. sourcemap ;
404
+ let updatedSourceMapSetting = viteSourceMap ;
427
405
428
- if ( originalSourcemapSetting === false ) {
429
- return {
430
- previousSourceMapSetting : 'disabled' ,
431
- updatedSourceMapSetting : originalSourcemapSetting ,
432
- } ;
433
- }
406
+ const settingKey = 'build.sourcemap' ;
407
+ const debug = sentryPluginOptions ?. debug ;
408
+
409
+ if ( viteSourceMap === false ) {
410
+ updatedSourceMapSetting = viteSourceMap ;
411
+
412
+ if ( debug ) {
413
+ // Longer debug message with more details
414
+ // eslint-disable-next-line no-console
415
+ console . warn (
416
+ `[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\`).` ,
417
+ ) ;
418
+ } else {
419
+ // eslint-disable-next-line no-console
420
+ console . warn ( '[Sentry] Source map generation is disabled in your Vite configuration.' ) ;
421
+ }
422
+ } else if ( viteSourceMap && [ 'hidden' , 'inline' , true ] . includes ( viteSourceMap ) ) {
423
+ updatedSourceMapSetting = viteSourceMap ;
424
+
425
+ debug &&
426
+ // eslint-disable-next-line no-console
427
+ console . log (
428
+ `[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.` ,
429
+ ) ;
430
+ } else {
431
+ updatedSourceMapSetting = 'hidden' ;
434
432
435
- if ( originalSourcemapSetting && [ 'hidden' , 'inline' , true ] . includes ( originalSourcemapSetting ) ) {
436
- return { previousSourceMapSetting : 'enabled' , updatedSourceMapSetting : originalSourcemapSetting } ;
433
+ debug &&
434
+ // eslint-disable-next-line no-console
435
+ console . log (
436
+ `[Sentry] Enabled source map generation in the build options with \`${ settingKey } : 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.` ,
437
+ ) ;
437
438
}
438
439
439
- return {
440
- previousSourceMapSetting : 'unset' ,
441
- updatedSourceMapSetting : 'hidden' ,
442
- } ;
440
+ return updatedSourceMapSetting ;
443
441
}
444
442
445
443
function getFiles ( dir : string ) : string [ ] {
@@ -475,22 +473,3 @@ function detectSentryRelease(): string {
475
473
476
474
return release ;
477
475
}
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