Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Use globalThis for code injection #610

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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,12 +1,5 @@
try {
var globalObject =
"undefined" != typeof window
? window
: "undefined" != typeof global
? global
: "undefined" != typeof self
? self
: {};
var globalObject = globalThis;

var 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 @@ -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=globalThis,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
22 changes: 2 additions & 20 deletions packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,8 @@ 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 = `
var _global =
typeof window !== 'undefined' ?
window :
typeof global !== 'undefined' ?
global :
typeof self !== 'undefined' ?
self :
{};
var _global = globalThis;

_global.SENTRY_RELEASE={id:${JSON.stringify(release)}};`;

Expand All @@ -335,18 +326,9 @@ 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 `{
var _sentryModuleMetadataGlobal =
typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: typeof self !== "undefined"
? self
: {};
var _sentryModuleMetadataGlobal = globalThis;

_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(
`";!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=globalThis,n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]=\\"1234\\",e._sentryDebugIdIdentifier=\\"sentry-dbid-1234\\")}catch(e){}}();"`
);
});
});
18 changes: 2 additions & 16 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,7 @@ describe("generateModuleMetadataInjectorCode", () => {
const generatedCode = generateModuleMetadataInjectorCode({});
expect(generatedCode).toMatchInlineSnapshot(`
"{
var _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
? global
: typeof self !== \\"undefined\\"
? self
: {};
var _sentryModuleMetadataGlobal = globalThis;

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

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