Skip to content

Commit

Permalink
feat(core): Write module injections to globalThis (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Dec 5, 2024
1 parent d811088 commit 10e4689
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ try {
? window
: "undefined" != typeof global
? global
: "undefined" != typeof globalThis
? global
: "undefined" != typeof self
? self
: {};
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 @@ -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";
Expand Down
10 changes: 7 additions & 3 deletions packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}};`;

Expand All @@ -345,6 +347,8 @@ export function generateModuleMetadataInjectorCode(metadata: any) {
? window
: typeof global !== "undefined"
? global
: typeof globalThis !== "undefined"
? globalThis
: typeof self !== "undefined"
? self
: {};
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(
`";!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){}}();"`
);
});
});
4 changes: 4 additions & 0 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ describe("generateModuleMetadataInjectorCode", () => {
? window
: typeof global !== \\"undefined\\"
? global
: typeof globalThis !== \\"undefined\\"
? globalThis
: typeof self !== \\"undefined\\"
? self
: {};
Expand Down Expand Up @@ -259,6 +261,8 @@ describe("generateModuleMetadataInjectorCode", () => {
? window
: typeof global !== \\"undefined\\"
? global
: typeof globalThis !== \\"undefined\\"
? globalThis
: typeof self !== \\"undefined\\"
? self
: {};
Expand Down

0 comments on commit 10e4689

Please sign in to comment.