11import type { Config } from '@react-router/dev/dist/config' ;
22import SentryCli from '@sentry/cli' ;
3- import glob from 'glob' ;
4- import * as fs from 'fs' ;
3+ import { glob } from 'glob' ;
4+ import { rm } from 'node:fs/promises' ;
5+ import type { SentryReactRouterBuildOptions } from '../types' ;
56
6- type ExtendedBuildEndArgs = Parameters < NonNullable < Config [ 'buildEnd' ] > > [ 0 ] & {
7- sentryConfig : {
8- authToken ?: string ;
9- org ?: string ;
10- project ?: string ;
11- sourceMapsUploadOptions ?: {
12- enabled ?: boolean ;
13- filesToDeleteAfterUpload ?: string | string [ ] | false ;
14- } ;
15- release ?: {
16- name ?: string ;
17- } ;
18- debug ?: boolean ;
19- } ;
20- } ;
7+ type BuildEndHook = NonNullable < Config [ 'buildEnd' ] > ;
8+
9+ function getSentryConfig ( viteConfig : unknown ) : SentryReactRouterBuildOptions {
10+ if ( ! viteConfig || typeof viteConfig !== 'object' || ! ( 'sentryConfig' in viteConfig ) ) {
11+ // eslint-disable-next-line no-console
12+ console . error ( '[Sentry] sentryConfig not found - it needs to be passed to vite.config.ts' ) ;
13+ }
2114
22- type ExtendedBuildEndHook = ( args : ExtendedBuildEndArgs ) => void | Promise < void > ;
15+ return ( viteConfig as { sentryConfig : SentryReactRouterBuildOptions } ) . sentryConfig ;
16+ }
2317
2418/**
2519 * A build end hook that handles Sentry release creation and source map uploads.
2620 * It creates a new Sentry release if configured, uploads source maps to Sentry,
2721 * and optionally deletes the source map files after upload.
2822 */
29- export const sentryOnBuildEnd : ExtendedBuildEndHook = async ( { reactRouterConfig, viteConfig, sentryConfig } ) => {
30- const { authToken, org, project, release, sourceMapsUploadOptions, debug } = sentryConfig ;
23+ export const sentryOnBuildEnd : BuildEndHook = async ( { reactRouterConfig, viteConfig } ) => {
24+ const {
25+ authToken = 'test-token' ,
26+ org = 'test-org' ,
27+ project = 'test-project' ,
28+ release = { name : 'test-release' } ,
29+ sourceMapsUploadOptions = { enabled : true } ,
30+ debug = false ,
31+ } = getSentryConfig ( viteConfig ) ;
32+
3133 const cliInstance = new SentryCli ( null , {
3234 authToken,
3335 org,
3436 project,
3537 } ) ;
36-
3738 // check if release should be created
3839 if ( release ?. name ) {
3940 try {
@@ -43,7 +44,6 @@ export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig
4344 console . error ( '[Sentry] Could not create release' , error ) ;
4445 }
4546 }
46-
4747 // upload sourcemaps
4848 if ( sourceMapsUploadOptions ?. enabled ?? ( true && viteConfig . build . sourcemap !== false ) ) {
4949 try {
@@ -59,14 +59,11 @@ export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig
5959 console . error ( '[Sentry] Could not upload sourcemaps' , error ) ;
6060 }
6161 }
62-
6362 // delete sourcemaps after upload
6463 let updatedFilesToDeleteAfterUpload = sourceMapsUploadOptions ?. filesToDeleteAfterUpload ;
65-
6664 // set a default value no option was set
6765 if ( typeof sourceMapsUploadOptions ?. filesToDeleteAfterUpload === 'undefined' ) {
6866 updatedFilesToDeleteAfterUpload = [ `${ reactRouterConfig . buildDirectory } /**/*.map` ] ;
69-
7067 if ( debug ) {
7168 // eslint-disable-next-line no-console
7269 console . info (
@@ -76,24 +73,21 @@ export const sentryOnBuildEnd: ExtendedBuildEndHook = async ({ reactRouterConfig
7673 ) ;
7774 }
7875 }
79-
8076 if ( updatedFilesToDeleteAfterUpload ) {
8177 try {
8278 const filePathsToDelete = await glob ( updatedFilesToDeleteAfterUpload , {
8379 absolute : true ,
8480 nodir : true ,
8581 } ) ;
86-
8782 if ( debug ) {
8883 filePathsToDelete . forEach ( filePathToDelete => {
8984 // eslint-disable-next-line no-console
9085 console . info ( `Deleting asset after upload: ${ filePathToDelete } ` ) ;
9186 } ) ;
9287 }
93-
9488 await Promise . all (
9589 filePathsToDelete . map ( filePathToDelete =>
96- fs . promises . rm ( filePathToDelete , { force : true } ) . catch ( ( e : unknown ) => {
90+ rm ( filePathToDelete , { force : true } ) . catch ( ( e : unknown ) => {
9791 if ( debug ) {
9892 // This is allowed to fail - we just don't do anything
9993 // eslint-disable-next-line no-console
0 commit comments