From 10e46898786ad9d558429cc2a357306a33a1c501 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Thu, 5 Dec 2024 12:55:21 +0100 Subject: [PATCH] feat(core): Write module injections to `globalThis` (#636) --- .../sentry-esbuild-debugid-injection-file.js | 2 ++ packages/bundler-plugin-core/src/index.ts | 2 +- packages/bundler-plugin-core/src/utils.ts | 10 +++++++--- packages/bundler-plugin-core/test/index.test.ts | 2 +- packages/bundler-plugin-core/test/utils.test.ts | 4 ++++ 5 files changed, 15 insertions(+), 5 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 6135fac3..ac913e2e 100644 --- a/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js +++ b/packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js @@ -4,6 +4,8 @@ try { ? window : "undefined" != typeof global ? global + : "undefined" != typeof globalThis + ? global : "undefined" != typeof self ? self : {}; diff --git a/packages/bundler-plugin-core/src/index.ts b/packages/bundler-plugin-core/src/index.ts index e03e7bee..1a0dd7c1 100644 --- a/packages/bundler-plugin-core/src/index.ts +++ b/packages/bundler-plugin-core/src/index.ts @@ -700,7 +700,7 @@ export function createComponentNameAnnotateHooks() { } export function getDebugIdSnippet(debugId: string): string { - return `;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"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 `;!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 3c8a92fa..55a747fe 100644 --- a/packages/bundler-plugin-core/src/utils.ts +++ b/packages/bundler-plugin-core/src/utils.ts @@ -318,9 +318,11 @@ export function generateGlobalInjectorCode({ window : typeof global !== 'undefined' ? global : - typeof self !== 'undefined' ? - self : - {}; + typeof globalThis !== 'undefined' ? + globalThis : + typeof self !== 'undefined' ? + self : + {}; _global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`; @@ -345,6 +347,8 @@ export function generateModuleMetadataInjectorCode(metadata: any) { ? window : typeof global !== "undefined" ? global + : typeof globalThis !== "undefined" + ? globalThis : typeof self !== "undefined" ? self : {}; diff --git a/packages/bundler-plugin-core/test/index.test.ts b/packages/bundler-plugin-core/test/index.test.ts index 51d0fd57..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( - `";!function(){try{var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"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){}}();"` + `";!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 3819e39c..b996a47c 100644 --- a/packages/bundler-plugin-core/test/utils.test.ts +++ b/packages/bundler-plugin-core/test/utils.test.ts @@ -226,6 +226,8 @@ describe("generateModuleMetadataInjectorCode", () => { ? window : typeof global !== \\"undefined\\" ? global + : typeof globalThis !== \\"undefined\\" + ? globalThis : typeof self !== \\"undefined\\" ? self : {}; @@ -259,6 +261,8 @@ describe("generateModuleMetadataInjectorCode", () => { ? window : typeof global !== \\"undefined\\" ? global + : typeof globalThis !== \\"undefined\\" + ? globalThis : typeof self !== \\"undefined\\" ? self : {};