1
+ import { getSentryRelease } from '@sentry/node' ;
2
+ import { uuid4 } from '@sentry/utils' ;
1
3
import type { SentryVitePluginOptions } from '@sentry/vite-plugin' ;
2
4
import { sentryVitePlugin } from '@sentry/vite-plugin' ;
5
+ import * as child_process from 'child_process' ;
3
6
import * as fs from 'fs' ;
4
7
import * as path from 'path' ;
5
8
// @ts -ignore -sorcery has no types :(
@@ -22,6 +25,10 @@ type SentryVitePluginOptionsOptionalInclude = Omit<SentryVitePluginOptions, 'inc
22
25
include ?: SentryVitePluginOptions [ 'include' ] ;
23
26
} ;
24
27
28
+ // storing this in the module scope because `makeCustomSentryVitePlugin` is called multiple times
29
+ // and we only want to generate a uuid once in case we have to fall back to it.
30
+ const release = detectSentryRelease ( ) ;
31
+
25
32<
8000
/code>
/**
26
33
* Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create
27
34
* releases and upload source maps to Sentry.
@@ -51,6 +58,7 @@ export async function makeCustomSentryVitePlugin(options?: SentryVitePluginOptio
51
58
{ paths : [ `${ outputDir } /server` ] , ignore : [ 'chunks/**' ] } ,
52
59
] ,
53
60
configFile : hasSentryProperties ? 'sentry.properties' : undefined ,
61
+ release,
54
62
} ;
55
63
56
64
const mergedOptions = {
@@ -174,3 +182,19 @@ function getFiles(dir: string): string[] {
174
182
175
183
return Array . prototype . concat ( ...files ) ;
176
184
}
185
+
186
+ function detectSentryRelease ( ) : string {
187
+ let releaseFallback : string ;
188
+ try {
189
+ releaseFallback = child_process . execSync ( 'git rev-parse HEAD' , { stdio : 'ignore' } ) . toString ( ) . trim ( ) ;
190
+ } catch ( _ ) {
191
+ // the command can throw for various reasons. Most importantly:
192
+ // - git is not installed
193
+ // - there is no git repo or no commit yet
194
+ // regardless of the case we just fall back to assigning a random uuid.
195
+ releaseFallback = uuid4 ( ) ;
196
+ }
197
+ const release = getSentryRelease ( ) || releaseFallback ;
198
+
199
+ return release ;
200
+ }
0 commit comments