Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
try {
let globalObject = globalThis;
let globalObject =
"undefined" != typeof window
? window
: "undefined" != typeof global
? global
: "undefined" != typeof globalThis
? global
: "undefined" != typeof self
? self
: {};

let stack = new globalObject.Error().stack;

Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `;{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";
Expand Down
27 changes: 25 additions & 2 deletions packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
let _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) {
Expand All @@ -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;
let _sentryModuleMetadataGlobal =
typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: typeof globalThis !== "undefined"
? globalThis
: typeof self !== "undefined"
? self
: {};

_sentryModuleMetadataGlobal._sentryModuleMetadata =
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-plugin-core/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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){}};"`
`";{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){}};"`
);
});
});
22 changes: 20 additions & 2 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,16 @@ describe("generateModuleMetadataInjectorCode", () => {
const generatedCode = generateModuleMetadataInjectorCode({});
expect(generatedCode).toMatchInlineSnapshot(`
"{
let _sentryModuleMetadataGlobal = globalThis;
let _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
? global
: typeof globalThis !== \\"undefined\\"
? globalThis
: typeof self !== \\"undefined\\"
? self
: {};

_sentryModuleMetadataGlobal._sentryModuleMetadata =
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
Expand All @@ -247,7 +256,16 @@ describe("generateModuleMetadataInjectorCode", () => {
});
expect(generatedCode).toMatchInlineSnapshot(`
"{
let _sentryModuleMetadataGlobal = globalThis;
let _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
? global
: typeof globalThis !== \\"undefined\\"
? globalThis
: typeof self !== \\"undefined\\"
? self
: {};

_sentryModuleMetadataGlobal._sentryModuleMetadata =
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
Expand Down
Loading