Skip to content

Commit 10e4689

Browse files
authored
feat(core): Write module injections to globalThis (#636)
1 parent d811088 commit 10e4689

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

packages/bundler-plugin-core/sentry-esbuild-debugid-injection-file.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ try {
44
? window
55
: "undefined" != typeof global
66
? global
7+
: "undefined" != typeof globalThis
8+
? global
79
: "undefined" != typeof self
810
? self
911
: {};

packages/bundler-plugin-core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ export function createComponentNameAnnotateHooks() {
700700
}
701701

702702
export function getDebugIdSnippet(debugId: string): string {
703-
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){}}();`;
703+
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){}}();`;
704704
}
705705

706706
export { stringToUUID, replaceBooleanFlagsInCode } from "./utils";

packages/bundler-plugin-core/src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ export function generateGlobalInjectorCode({
318318
window :
319319
typeof global !== 'undefined' ?
320320
global :
321-
typeof self !== 'undefined' ?
322-
self :
323-
{};
321+
typeof globalThis !== 'undefined' ?
322+
globalThis :
323+
typeof self !== 'undefined' ?
324+
self :
325+
{};
324326
325327
_global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`;
326328

@@ -345,6 +347,8 @@ export function generateModuleMetadataInjectorCode(metadata: any) {
345347
? window
346348
: typeof global !== "undefined"
347349
? global
350+
: typeof globalThis !== "undefined"
351+
? globalThis
348352
: typeof self !== "undefined"
349353
? self
350354
: {};

packages/bundler-plugin-core/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe("getDebugIdSnippet", () => {
44
it("returns the debugId injection snippet for a passed debugId", () => {
55
const snippet = getDebugIdSnippet("1234");
66
expect(snippet).toMatchInlineSnapshot(
7-
`";!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){}}();"`
7+
`";!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){}}();"`
88
);
99
});
1010
});

packages/bundler-plugin-core/test/utils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ describe("generateModuleMetadataInjectorCode", () => {
226226
? window
227227
: typeof global !== \\"undefined\\"
228228
? global
229+
: typeof globalThis !== \\"undefined\\"
230+
? globalThis
229231
: typeof self !== \\"undefined\\"
230232
? self
231233
: {};
@@ -259,6 +261,8 @@ describe("generateModuleMetadataInjectorCode", () => {
259261
? window
260262
: typeof global !== \\"undefined\\"
261263
? global
264+
: typeof globalThis !== \\"undefined\\"
265+
? globalThis
262266
: typeof self !== \\"undefined\\"
263267
? self
264268
: {};

0 commit comments

Comments
 (0)