@@ -312,8 +312,20 @@ export function generateGlobalInjectorCode({
312312 release : string ;
313313 injectBuildInformation : boolean ;
314314} ) {
315+ // The code below is mostly ternary operators because it saves bundle size.
316+ // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
315317 let code = `{
316- let _global = globalThis;
318+ const _global =
319+ typeof window !== 'undefined' ?
320+ window :
321+ typeof global !== 'undefined' ?
322+ global :
323+ typeof globalThis !== 'undefined' ?
324+ globalThis :
325+ typeof self !== 'undefined' ?
326+ self :
327+ {};
328+
317329 _global.SENTRY_RELEASE={id:${ JSON . stringify ( release ) } };` ;
318330
319331 if ( injectBuildInformation ) {
@@ -330,9 +342,20 @@ export function generateGlobalInjectorCode({
330342
331343// eslint-disable-next-line @typescript-eslint/no-explicit-any
332344export function generateModuleMetadataInjectorCode ( metadata : any ) {
345+ // The code below is mostly ternary operators because it saves bundle size.
346+ // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
333347 // We are merging the metadata objects in case modules are bundled twice with the plugin
334348 return `{
335- let _sentryModuleMetadataGlobal = globalThis;
349+ const _sentryModuleMetadataGlobal =
350+ typeof window !== "undefined"
351+ ? window
352+ : typeof global !== "undefined"
353+ ? global
354+ : typeof globalThis !== "undefined"
355+ ? globalThis
356+ : typeof self !== "undefined"
357+ ? self
358+ : {};
336359
337360 _sentryModuleMetadataGlobal._sentryModuleMetadata =
338361 _sentryModuleMetadataGlobal._sentryModuleMetadata || {};
0 commit comments