diff --git a/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js b/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js index ac913e2e..34e03b96 100644 --- a/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js +++ b/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js @@ -1,16 +1,7 @@ try { - var globalObject = - "undefined" != typeof window - ? window - : "undefined" != typeof global - ? global - : "undefined" != typeof globalThis - ? global - : "undefined" != typeof self - ? self - : {}; + let globalObject = globalThis; - var stack = new globalObject.Error().stack; + let stack = new globalObject.Error().stack; if (stack) { globalObject._sentryDebugIds = globalObject._sentryDebugIds || {}; diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index 0ad78019..7be558b4 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -695,7 +695,7 @@ export function createComponentNameAnnotateHooks(ignoredComponents?: string[]) { } export function getDebugIdSnippet(debugId: string): string { - return `;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${debugId}",e._sentryDebugIdIdentifier="sentry-dbid-${debugId}")}catch(e){}}();`; + return `;{try{let e=globalThis,n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${debugId}",e._sentryDebugIdIdentifier="sentry-dbid-${debugId}")}catch(e){}};`; } export { stringToUUID, replaceBooleanFlagsInCode } from "./utils"; diff --git a/packages/bundler-plugin-core/src/utils.ts b/packages/bundler-plugin-core/src/utils.ts index 4ad7e01e..0c3ecd7d 100644 --- a/packages/bundler-plugin-core/src/utils.ts +++ b/packages/bundler-plugin-core/src/utils.ts @@ -312,20 +312,8 @@ export function generateGlobalInjectorCode({ release: string; injectBuildInformation: boolean; }) { - // The code below is mostly ternary operators because it saves bundle size. - // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.) let code = `{ - const _global = - typeof window !== 'undefined' ? - window : - typeof global !== 'undefined' ? - global : - typeof globalThis !== 'undefined' ? - globalThis : - typeof self !== 'undefined' ? - self : - {}; - + let _global = globalThis; _global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`; if (injectBuildInformation) { @@ -342,20 +330,9 @@ export function generateGlobalInjectorCode({ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function generateModuleMetadataInjectorCode(metadata: any) { - // The code below is mostly ternary operators because it saves bundle size. - // The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.) // We are merging the metadata objects in case modules are bundled twice with the plugin return `{ - const _sentryModuleMetadataGlobal = - typeof window !== "undefined" - ? window - : typeof global !== "undefined" - ? global - : typeof globalThis !== "undefined" - ? globalThis - : typeof self !== "undefined" - ? self - : {}; + let _sentryModuleMetadataGlobal = globalThis; _sentryModuleMetadataGlobal._sentryModuleMetadata = _sentryModuleMetadataGlobal._sentryModuleMetadata || {}; diff --git a/packages/bundler-plugin-core/test/index.test.ts b/packages/bundler-plugin-core/test/index.test.ts index 71ad97bb..f5dcb751 100644 --- a/packages/bundler-plugin-core/test/index.test.ts +++ b/packages/bundler-plugin-core/test/index.test.ts @@ -4,7 +4,7 @@ describe("getDebugIdSnippet", () => { it("returns the debugId injection snippet for a passed debugId", () => { const snippet = getDebugIdSnippet("1234"); expect(snippet).toMatchInlineSnapshot( - `";!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]=\\"1234\\",e._sentryDebugIdIdentifier=\\"sentry-dbid-1234\\")}catch(e){}}();"` + `";{try{let e=globalThis,n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]=\\"1234\\",e._sentryDebugIdIdentifier=\\"sentry-dbid-1234\\")}catch(e){}};"` ); }); }); diff --git a/packages/bundler-plugin-core/test/utils.test.ts b/packages/bundler-plugin-core/test/utils.test.ts index 2098ef46..ca70d0c7 100644 --- a/packages/bundler-plugin-core/test/utils.test.ts +++ b/packages/bundler-plugin-core/test/utils.test.ts @@ -221,16 +221,7 @@ describe("generateModuleMetadataInjectorCode", () => { const generatedCode = generateModuleMetadataInjectorCode({}); expect(generatedCode).toMatchInlineSnapshot(` "{ - const _sentryModuleMetadataGlobal = - typeof window !== \\"undefined\\" - ? window - : typeof global !== \\"undefined\\" - ? global - : typeof globalThis !== \\"undefined\\" - ? globalThis - : typeof self !== \\"undefined\\" - ? self - : {}; + let _sentryModuleMetadataGlobal = globalThis; _sentryModuleMetadataGlobal._sentryModuleMetadata = _sentryModuleMetadataGlobal._sentryModuleMetadata || {}; @@ -256,16 +247,7 @@ describe("generateModuleMetadataInjectorCode", () => { }); expect(generatedCode).toMatchInlineSnapshot(` "{ - const _sentryModuleMetadataGlobal = - typeof window !== \\"undefined\\" - ? window - : typeof global !== \\"undefined\\" - ? global - : typeof globalThis !== \\"undefined\\" - ? globalThis - : typeof self !== \\"undefined\\" - ? self - : {}; + let _sentryModuleMetadataGlobal = globalThis; _sentryModuleMetadataGlobal._sentryModuleMetadata = _sentryModuleMetadataGlobal._sentryModuleMetadata || {};