Skip to content

Commit e9d28d4

Browse files
authored
Revert "feat(core)!: Use globalThis for code injection (#610)"
This reverts commit 8eb6c5f.
1 parent 57ad14f commit e9d28d4

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
try {
2-
let globalObject = globalThis;
2+
var globalObject =
3+
"undefined" != typeof window
4+
? window
5+
: "undefined" != typeof global
6+
? global
7+
: "undefined" != typeof globalThis
8+
? global
9+
: "undefined" != typeof self
10+
? self
11+
: {};
312

4-
let stack = new globalObject.Error().stack;
13+
var stack = new globalObject.Error().stack;
514

615
if (stack) {
716
globalObject._sentryDebugIds = globalObject._sentryDebugIds || {};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ export function createComponentNameAnnotateHooks(ignoredComponents?: string[]) {
695695
}
696696

697697
export function getDebugIdSnippet(debugId: string): string {
698-
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){}};`;
698+
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){}}();`;
699699
}
700700

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

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,20 @@ export function generateGlobalInjectorCode({
312312
release: string;
313313
injectBuildInformation: boolean;
314314
}) {
315+
// The code below is mostly ternary operators because it saves bundle size.
316+
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
315317
let code = `{
316-
let _global = globalThis;
318+
const _global =
319+
typeof window !== 'undefined' ?
320+
window :
321+
typeof global !== 'undefined' ?
322+
global :
323+
typeof globalThis !== 'undefined' ?
324+
globalThis :
325+
typeof self !== 'undefined' ?
326+
self :
327+
{};
328+
317329
_global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`;
318330

319331
if (injectBuildInformation) {
@@ -330,9 +342,20 @@ export function generateGlobalInjectorCode({
330342

331343
// eslint-disable-next-line @typescript-eslint/no-explicit-any
332344
export function generateModuleMetadataInjectorCode(metadata: any) {
345+
// The code below is mostly ternary operators because it saves bundle size.
346+
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
333347
// We are merging the metadata objects in case modules are bundled twice with the plugin
334348
return `{
335-
let _sentryModuleMetadataGlobal = globalThis;
349+
const _sentryModuleMetadataGlobal =
350+
typeof window !== "undefined"
351+
? window
352+
: typeof global !== "undefined"
353+
? global
354+
: typeof globalThis !== "undefined"
355+
? globalThis
356+
: typeof self !== "undefined"
357+
? self
358+
: {};
336359
337360
_sentryModuleMetadataGlobal._sentryModuleMetadata =
338361
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};

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-
`";{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){}};"`
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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,16 @@ describe("generateModuleMetadataInjectorCode", () => {
221221
const generatedCode = generateModuleMetadataInjectorCode({});
222222
expect(generatedCode).toMatchInlineSnapshot(`
223223
"{
224-
let _sentryModuleMetadataGlobal = globalThis;
224+
const _sentryModuleMetadataGlobal =
225+
typeof window !== \\"undefined\\"
226+
? window
227+
: typeof global !== \\"undefined\\"
228+
? global
229+
: typeof globalThis !== \\"undefined\\"
230+
? globalThis
231+
: typeof self !== \\"undefined\\"
232+
? self
233+
: {};
225234
226235
_sentryModuleMetadataGlobal._sentryModuleMetadata =
227236
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
@@ -247,7 +256,16 @@ describe("generateModuleMetadataInjectorCode", () => {
247256
});
248257
expect(generatedCode).toMatchInlineSnapshot(`
249258
"{
250-
let _sentryModuleMetadataGlobal = globalThis;
259+
const _sentryModuleMetadataGlobal =
260+
typeof window !== \\"undefined\\"
261+
? window
262+
: typeof global !== \\"undefined\\"
263+
? global
264+
: typeof globalThis !== \\"undefined\\"
265+
? globalThis
266+
: typeof self !== \\"undefined\\"
267+
? self
268+
: {};
251269
252270
_sentryModuleMetadataGlobal._sentryModuleMetadata =
253271
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};

0 commit comments

Comments
 (0)