1+ import { getSentryRelease } from '@sentry/node' ;
2+ import { uuid4 } from '@sentry/utils' ;
13import type { SentryVitePluginOptions } from '@sentry/vite-plugin' ;
24import { sentryVitePlugin } from '@sentry/vite-plugin' ;
5+ import * as child_process from 'child_process' ;
36import * as fs from 'fs' ;
47import * as path from 'path' ;
58// @ts -ignore -sorcery has no types :(
@@ -22,6 +25,10 @@ type SentryVitePluginOptionsOptionalInclude = Omit<SentryVitePluginOptions, 'inc
2225 include ?: SentryVitePluginOptions [ 'include' ] ;
2326} ;
2427
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+
2532/**
2633 * Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create
2734 * releases and upload source maps to Sentry.
@@ -51,6 +58,7 @@ export async function makeCustomSentryVitePlugin(options?: SentryVitePluginOptio
5158 { paths : [ `${ outputDir } /server` ] , ignore : [ 'chunks/**' ] } ,
5259 ] ,
5360 configFile : hasSentryProperties ? 'sentry.properties' : undefined ,
61+ release,
5462 } ;
5563
5664 const mergedOptions = {
@@ -174,3 +182,19 @@ function getFiles(dir: string): string[] {
174182
175183 return Array . prototype . concat ( ...files ) ;
176184}
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