Skip to content

Commit 8eb6c5f

Browse files
timfishLuca Forstner
andauthored
feat(core)!: Use globalThis for code injection (#610)
Co-authored-by: Luca Forstner <luca.forstner@sentry.io>
1 parent b734d45 commit 8eb6c5f

File tree

5 files changed

+8
-58
lines changed

5 files changed

+8
-58
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
try {
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-
: {};
2+
let globalObject = globalThis;
123

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

156
if (stack) {
167
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 `;!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){}}();`;
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){}};`;
699699
}
700700

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

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,8 @@ 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.)
317315
let code = `{
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-
316+
let _global = globalThis;
329317
_global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`;
330318

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

343331
// eslint-disable-next-line @typescript-eslint/no-explicit-any
344332
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.)
347333
// We are merging the metadata objects in case modules are bundled twice with the plugin
348334
return `{
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-
: {};
335+
let _sentryModuleMetadataGlobal = globalThis;
359336
360337
_sentryModuleMetadataGlobal._sentryModuleMetadata =
361338
_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-
`";!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){}}();"`
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){}};"`
88
);
99
});
1010
});

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,7 @@ describe("generateModuleMetadataInjectorCode", () => {
221221
const generatedCode = generateModuleMetadataInjectorCode({});
222222
expect(generatedCode).toMatchInlineSnapshot(`
223223
"{
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-
: {};
224+
let _sentryModuleMetadataGlobal = globalThis;
234225
235226
_sentryModuleMetadataGlobal._sentryModuleMetadata =
236227
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
@@ -256,16 +247,7 @@ describe("generateModuleMetadataInjectorCode", () => {
256247
});
257248
expect(generatedCode).toMatchInlineSnapshot(`
258249
"{
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-
: {};
250+
let _sentryModuleMetadataGlobal = globalThis;
269251
270252
_sentryModuleMetadataGlobal._sentryModuleMetadata =
271253
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};

0 commit comments

Comments
 (0)