@@ -314,7 +314,7 @@ export function generateGlobalInjectorCode({
314314} ) : string {
315315 // The code below is mostly ternary operators because it saves bundle size.
316316 // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
317- let code = `!function(){var e="undefined"!=typeof window? window: "undefined"!=typeof global? global: "undefined"!=typeof globalThis? globalThis: "undefined"!=typeof self? self: {};` ;
317+ let code = `!function(){try{ var e=( "undefined"!=typeof window&& window)||( "undefined"!=typeof global&& global)||( "undefined"!=typeof globalThis&& globalThis)||( "undefined"!=typeof self&& self)|| {};` ;
318318
319319 code += `e.SENTRY_RELEASE={id:${ JSON . stringify ( release ) } };` ;
320320
@@ -324,7 +324,7 @@ export function generateGlobalInjectorCode({
324324 code += `e.SENTRY_BUILD_INFO=${ JSON . stringify ( buildInfo ) } ;` ;
325325 }
326326
327- code += "}();" ;
327+ code += "}catch(e){}} ();" ;
328328
329329 return code ;
330330}
@@ -334,9 +334,10 @@ export function generateModuleMetadataInjectorCode(metadata: any): string {
334334 // The code below is mostly ternary operators because it saves bundle size.
335335 // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
336336 // We are merging the metadata objects in case modules are bundled twice with the plugin
337- return `!function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},e._sentryModuleMetadata[(new e.Error).stack]=function(e){for(var n=1;n<arguments.length;n++){var a=arguments[n];if(null!=a)for(var t in a)a.hasOwnProperty(t)&&(e[t]=a[t])}return e}({},e._sentryModuleMetadata[(new e.Error).stack],${ JSON . stringify (
337+ // Use try-catch to avoid issues when bundlers rename global variables like 'window' to 'k'
338+ return `!function(){try{var e=("undefined"!=typeof window&&window)||("undefined"!=typeof global&&global)||("undefined"!=typeof globalThis&&globalThis)||("undefined"!=typeof self&&self)||{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},e._sentryModuleMetadata[(new e.Error).stack]=function(e){for(var n=1;n<arguments.length;n++){var a=arguments[n];if(null!=a)for(var t in a)a.hasOwnProperty(t)&&(e[t]=a[t])}return e}({},e._sentryModuleMetadata[(new e.Error).stack],${ JSON . stringify (
338339 metadata
339- ) } )}();`;
340+ ) } )}catch(e){}} ();`;
340341}
341342
342343export function getBuildInformation ( ) : {
0 commit comments