@@ -11,7 +11,8 @@ import * as sorcery from 'sorcery';
1111import type { Plugin } from 'vite' ;
1212
1313import { WRAPPED_MODULE_SUFFIX } from './autoInstrument' ;
14- import { getAdapterOutputDir , loadSvelteConfig } from './svelteConfig' ;
14+ import type { SupportedSvelteKitAdapters } from './detectAdapter' ;
15+ import { getAdapterOutputDir , getHooksFileName , loadSvelteConfig } from './svelteConfig' ;
1516
1617// sorcery has no types, so these are some basic type definitions:
1718type Chain = {
@@ -26,6 +27,10 @@ type SentryVitePluginOptionsOptionalInclude = Omit<SentryVitePluginOptions, 'inc
2627 include ?: SentryVitePluginOptions [ 'include' ] ;
2728} ;
2829
30+ type CustomSentryVitePluginOptions = SentryVitePluginOptionsOptionalInclude & {
31+ adapter : SupportedSvelteKitAdapters ;
32+ } ;
33+
2934// storing this in the module scope because `makeCustomSentryVitePlugin` is called multiple times
3035// and we only want to generate a uuid once in case we have to fall back to it.
3136const release = detectSentryRelease ( ) ;
@@ -46,18 +51,15 @@ const release = detectSentryRelease();
4651 *
4752 * @returns the custom Sentry Vite plugin
4853 */
49- export async function makeCustomSentryVitePlugin ( options ?: SentryVitePluginOptionsOptionalInclude ) : Promise < Plugin > {
54+ export async function makeCustomSentryVitePlugin ( options ?: CustomSentryVitePluginOptions ) : Promise < Plugin > {
5055 const svelteConfig = await loadSvelteConfig ( ) ;
5156
52- const outputDir = await getAdapterOutputDir ( svelteConfig ) ;
57+ const usedAdapter = options ?. adapter || 'other' ;
58+ const outputDir = await getAdapterOutputDir ( svelteConfig , usedAdapter ) ;
5359 const hasSentryProperties = fs . existsSync ( path . resolve ( process . cwd ( ) , 'sentry.properties' ) ) ;
5460
5561 const defaultPluginOptions : SentryVitePluginOptions = {
56- include : [
57- { paths : [ `${ outputDir } /client` ] } ,
58- { paths : [ `${ outputDir } /server/chunks` ] } ,
59- { paths : [ `${ outputDir } /server` ] , ignore : [ 'chunks/**' ] } ,
60- ] ,
62+ include : [ { paths : [ `${ outputDir } /client` ] } , { paths : [ `${ outputDir } /server` ] } ] ,
6163 configFile : hasSentryProperties ? 'sentry.properties' : undefined ,
6264 release,
6365 } ;
@@ -74,6 +76,8 @@ export async function makeCustomSentryVitePlugin(options?: SentryVitePluginOptio
7476
7577 let isSSRBuild = true ;
7678
79+ const serverHooksFile = getHooksFileName ( svelteConfig , 'server' ) ;
80+
7781 const customPlugin : Plugin = {
7882 name : 'sentry-upload-source-maps' ,
7983 apply : 'build' , // only apply this plugin at build time
@@ -84,7 +88,6 @@ export async function makeCustomSentryVitePlugin(options?: SentryVitePluginOptio
8488 buildStart,
8589 resolveId,
8690 renderChunk,
87- transform,
8891
8992 // Modify the config to generate source maps
9093 config : config => {
@@ -109,6 +112,18 @@ export async function makeCustomSentryVitePlugin(options?: SentryVitePluginOptio
109112 }
110113 } ,
111114
115+ transform : async ( code , id ) => {
116+ let modifiedCode = code ;
117+ const isServerHooksFile = new RegExp ( `\/${ escapeStringForRegex ( serverHooksFile ) } (\.(js|ts|mjs|mts))?` ) . test ( id ) ;
118+
119+ if ( isServerHooksFile ) {
120+ let injectedCode = `global.__sentry_sveltekit_output_dir = "${ outputDir || 'undefined' } ";\n` ;
121+ modifiedCode = `${ code } \n${ injectedCode } ` ;
122+ }
123+ // @ts -ignore - this hook exists on the plugin!
124+ return sentryPlugin . transform ( modifiedCode , id ) ;
125+ } ,
126+
112127 // We need to start uploading source maps later than in the original plugin
113128 // because SvelteKit is invoking the adapter at closeBundle.
114129 // This means that we need to wait until the adapter is done before we start uploading.
0 commit comments