From e9d28d4bb376a026a60edd4fc20d68b794384019 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 15 Jan 2025 09:25:18 +0100 Subject: [PATCH 1/2] Revert "feat(core)!: Use `globalThis` for code injection (#610)" This reverts commit 8eb6c5fb5d943865e2b0bda857913f1476547750. --- .../sentry-esbuild-debugid-injection-file.js | 13 +++++++-- packages/bundler-plugin-core/src/index.ts | 2 +- packages/bundler-plugin-core/src/utils.ts | 27 +++++++++++++++++-- .../bundler-plugin-core/test/index.test.ts | 2 +- .../bundler-plugin-core/test/utils.test.ts | 22 +++++++++++++-- 5 files changed, 58 insertions(+), 8 deletions(-) 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 34e03b96..ac913e2e 100644 --- a/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js +++ b/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js @@ -1,7 +1,16 @@ try { - let globalObject = globalThis; + var globalObject = + "undefined" != typeof window + ? window + : "undefined" != typeof global + ? global + : "undefined" != typeof globalThis + ? global + : "undefined" != typeof self + ? self + : {}; - let stack = new globalObject.Error().stack; + var 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 c82985c1..ee6fc715 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 `;{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){}};`; + 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){}}();`; } export { stringToUUID, replaceBooleanFlagsInCode } from "./utils"; diff --git a/packages/bundler-plugin-core/src/utils.ts b/packages/bundler-plugin-core/src/utils.ts index 0c3ecd7d..4ad7e01e 100644 --- a/packages/bundler-plugin-core/src/utils.ts +++ b/packages/bundler-plugin-core/src/utils.ts @@ -312,8 +312,20 @@ 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 = `{ - let _global = globalThis; + const _global = + typeof window !== 'undefined' ? + window : + typeof global !== 'undefined' ? + global : + typeof globalThis !== 'undefined' ? + globalThis : + typeof self !== 'undefined' ? + self : + {}; + _global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`; if (injectBuildInformation) { @@ -330,9 +342,20 @@ 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 `{ - let _sentryModuleMetadataGlobal = globalThis; + const _sentryModuleMetadataGlobal = + typeof window !== "undefined" + ? window + : typeof global !== "undefined" + ? global + : typeof globalThis !== "undefined" + ? globalThis + : typeof self !== "undefined" + ? self + : {}; _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 f5dcb751..71ad97bb 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( - `";{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){}};"` + `";!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){}}();"` ); }); }); diff --git a/packages/bundler-plugin-core/test/utils.test.ts b/packages/bundler-plugin-core/test/utils.test.ts index ca70d0c7..2098ef46 100644 --- a/packages/bundler-plugin-core/test/utils.test.ts +++ b/packages/bundler-plugin-core/test/utils.test.ts @@ -221,7 +221,16 @@ describe("generateModuleMetadataInjectorCode", () => { const generatedCode = generateModuleMetadataInjectorCode({}); expect(generatedCode).toMatchInlineSnapshot(` "{ - let _sentryModuleMetadataGlobal = globalThis; + const _sentryModuleMetadataGlobal = + typeof window !== \\"undefined\\" + ? window + : typeof global !== \\"undefined\\" + ? global + : typeof globalThis !== \\"undefined\\" + ? globalThis + : typeof self !== \\"undefined\\" + ? self + : {}; _sentryModuleMetadataGlobal._sentryModuleMetadata = _sentryModuleMetadataGlobal._sentryModuleMetadata || {}; @@ -247,7 +256,16 @@ describe("generateModuleMetadataInjectorCode", () => { }); expect(generatedCode).toMatchInlineSnapshot(` "{ - let _sentryModuleMetadataGlobal = globalThis; + const _sentryModuleMetadataGlobal = + typeof window !== \\"undefined\\" + ? window + : typeof global !== \\"undefined\\" + ? global + : typeof globalThis !== \\"undefined\\" + ? globalThis + : typeof self !== \\"undefined\\" + ? self + : {}; _sentryModuleMetadataGlobal._sentryModuleMetadata = _sentryModuleMetadataGlobal._sentryModuleMetadata || {}; From 5d4645824c098589e93cde38a847bb6318867434 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Wed, 15 Jan 2025 09:32:34 +0100 Subject: [PATCH 2/2] dont revert stuff we dont wanna revert --- .../sentry-esbuild-debugid-injection-file.js | 4 ++-- packages/bundler-plugin-core/src/index.ts | 2 +- packages/bundler-plugin-core/src/utils.ts | 4 ++-- packages/bundler-plugin-core/test/index.test.ts | 2 +- packages/bundler-plugin-core/test/utils.test.ts | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) 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..61196856 100644 --- a/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js +++ b/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js @@ -1,5 +1,5 @@ try { - var globalObject = + let globalObject = "undefined" != typeof window ? window : "undefined" != typeof global @@ -10,7 +10,7 @@ try { ? self : {}; - 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 ee6fc715..b623f53a 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="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){}};`; } 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..24e2049e 100644 --- a/packages/bundler-plugin-core/src/utils.ts +++ b/packages/bundler-plugin-core/src/utils.ts @@ -315,7 +315,7 @@ export function generateGlobalInjectorCode({ // 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 = + let _global = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? @@ -346,7 +346,7 @@ export function generateModuleMetadataInjectorCode(metadata: any) { // 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 = + let _sentryModuleMetadataGlobal = typeof window !== "undefined" ? window : typeof global !== "undefined" diff --git a/packages/bundler-plugin-core/test/index.test.ts b/packages/bundler-plugin-core/test/index.test.ts index 71ad97bb..6e981ced 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=\\"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){}};"` ); }); }); diff --git a/packages/bundler-plugin-core/test/utils.test.ts b/packages/bundler-plugin-core/test/utils.test.ts index 2098ef46..1ace6e68 100644 --- a/packages/bundler-plugin-core/test/utils.test.ts +++ b/packages/bundler-plugin-core/test/utils.test.ts @@ -221,7 +221,7 @@ describe("generateModuleMetadataInjectorCode", () => { const generatedCode = generateModuleMetadataInjectorCode({}); expect(generatedCode).toMatchInlineSnapshot(` "{ - const _sentryModuleMetadataGlobal = + let _sentryModuleMetadataGlobal = typeof window !== \\"undefined\\" ? window : typeof global !== \\"undefined\\" @@ -256,7 +256,7 @@ describe("generateModuleMetadataInjectorCode", () => { }); expect(generatedCode).toMatchInlineSnapshot(` "{ - const _sentryModuleMetadataGlobal = + let _sentryModuleMetadataGlobal = typeof window !== \\"undefined\\" ? window : typeof global !== \\"undefined\\"