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
Expand Up @@ -8,7 +8,7 @@ try {
? self
: {};

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

if (stack) {
globalObject._sentryDebugIds = globalObject._sentryDebugIds || {};
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 @@ -666,7 +666,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 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 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
4 changes: 2 additions & 2 deletions packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ export function generateModuleMetadataInjectorCode(metadata: any) {
_sentryModuleMetadataGlobal._sentryModuleMetadata =
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};

_sentryModuleMetadataGlobal._sentryModuleMetadata[new Error().stack] =
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack] =
Object.assign(
{},
_sentryModuleMetadataGlobal._sentryModuleMetadata[new Error().stack],
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
${JSON.stringify(metadata)}
);
}`;
Expand Down
10 changes: 10 additions & 0 deletions packages/bundler-plugin-core/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getDebugIdSnippet } from "../src";

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){}}();"`
);
});
});
62 changes: 62 additions & 0 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
generateModuleMetadataInjectorCode,
getDependencies,
getPackageJson,
parseMajorVersion,
Expand Down Expand Up @@ -214,3 +215,64 @@ if (false && true) {
});
});
});

describe("generateModuleMetadataInjectorCode", () => {
it("generates code with empty metadata object", () => {
const generatedCode = generateModuleMetadataInjectorCode({});
expect(generatedCode).toMatchInlineSnapshot(`
"{
var _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
? global
: typeof self !== \\"undefined\\"
? self
: {};

_sentryModuleMetadataGlobal._sentryModuleMetadata =
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};

_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack] =
Object.assign(
{},
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
{}
);
}"
`);
});

it("generates code with metadata object", () => {
const generatedCode = generateModuleMetadataInjectorCode({
"file1.js": {
foo: "bar",
},
"file2.js": {
bar: "baz",
},
});
expect(generatedCode).toMatchInlineSnapshot(`
"{
var _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
? global
: typeof self !== \\"undefined\\"
? self
: {};

_sentryModuleMetadataGlobal._sentryModuleMetadata =
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};

_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack] =
Object.assign(
{},
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
{\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}}
);
}"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function checkBundle(bundlePath1: string, bundlePath2: string): string[] {
expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)
);

expect(Object.keys(debugIdMap1)[0]).toContain("Error");

const process2Output = childProcess.execSync(`node ${bundlePath2}`, { encoding: "utf-8" });
const debugIdMap2 = JSON.parse(process2Output) as Record<string, string>;
const debugIds2 = Object.values(debugIdMap2);
Expand All @@ -21,6 +23,8 @@ function checkBundle(bundlePath1: string, bundlePath2: string): string[] {
expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)
);

expect(Object.keys(debugIdMap2)[0]).toContain("Error");

expect(debugIds1).not.toEqual(debugIds2);

return [...debugIds1, ...debugIds2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function checkBundle(bundlePath: string): void {
expect(Object.values(map)).toHaveLength(1);
// The value should be the expected metadata
expect(Object.values(map)).toEqual([{ team: "frontend" }]);

// The key is the stack trace of the error thrown in the file
expect(Object.keys(map)[0]).toContain("Error");
expect(Object.keys(map)[0]).toContain("bundle.js");
}

describe("metadata injection", () => {
Expand Down
Loading