Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -456,7 +456,7 @@ export function createComponentNameAnnotateHooks(ignoredComponents?: string[]):
}

export function getDebugIdSnippet(debugId: string): string {
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){}};`;
return `;{try{(function(){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 type { Logger } from "./logger";
Expand Down
14 changes: 7 additions & 7 deletions packages/bundler-plugin-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ export function generateGlobalInjectorCode({
}): string {
// 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 =
let code = `(function(){
var _global =
typeof window !== 'undefined' ?
window :
typeof global !== 'undefined' ?
Expand All @@ -332,10 +332,10 @@ export function generateGlobalInjectorCode({
const buildInfo = getBuildInformation();

code += `
_global.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
_global.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
}

code += "}";
code += "})();";

return code;
}
Expand All @@ -345,8 +345,8 @@ export function generateModuleMetadataInjectorCode(metadata: any): string {
// 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 =
return `(function(){
var _sentryModuleMetadataGlobal =
typeof window !== "undefined"
? window
: typeof global !== "undefined"
Expand All @@ -366,7 +366,7 @@ export function generateModuleMetadataInjectorCode(metadata: any): string {
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
${JSON.stringify(metadata)}
);
}`;
})();`;
}

export function getBuildInformation(): {
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=\\"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){}};"`
`";{try{(function(){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){}};"`
);
});
});
77 changes: 71 additions & 6 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {
generateGlobalInjectorCode,
generateModuleMetadataInjectorCode,
getDependencies,
getPackageJson,
parseMajorVersion,
replaceBooleanFlagsInCode,
stringToUUID,
} from "../src/utils";

import fs from "fs";

import path from "node:path";

type PackageJson = Record<string, unknown>;
Expand Down Expand Up @@ -216,12 +220,73 @@ if (false && true) {
});
});

describe("generateGlobalInjectorCode", () => {
it("generates code with release", () => {
const generatedCode = generateGlobalInjectorCode({
release: "1.2.3",
injectBuildInformation: false,
});

expect(generatedCode).toMatchInlineSnapshot(`
"(function(){
var _global =
typeof window !== 'undefined' ?
window :
typeof global !== 'undefined' ?
global :
typeof globalThis !== 'undefined' ?
globalThis :
typeof self !== 'undefined' ?
self :
{};

_global.SENTRY_RELEASE={id:\\"1.2.3\\"};})();"
`);
});

it("generates code with release and build information", () => {
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(
JSON.stringify({
name: "test-app",
dependencies: {
myDep: "^2.1.4",
},
devDependencies: {
rollup: "^3.1.4",
},
})
);

const generatedCode = generateGlobalInjectorCode({
release: "1.2.3",
injectBuildInformation: true,
});

expect(generatedCode).toMatchInlineSnapshot(`
"(function(){
var _global =
typeof window !== 'undefined' ?
window :
typeof global !== 'undefined' ?
global :
typeof globalThis !== 'undefined' ?
globalThis :
typeof self !== 'undefined' ?
self :
{};

_global.SENTRY_RELEASE={id:\\"1.2.3\\"};
_global.SENTRY_BUILD_INFO={\\"deps\\":[\\"myDep\\",\\"rollup\\"],\\"depsVersions\\":{\\"rollup\\":3},\\"nodeVersion\\":18};})();"
`);
});
});

describe("generateModuleMetadataInjectorCode", () => {
it("generates code with empty metadata object", () => {
const generatedCode = generateModuleMetadataInjectorCode({});
expect(generatedCode).toMatchInlineSnapshot(`
"{
let _sentryModuleMetadataGlobal =
"(function(){
var _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
Expand All @@ -241,7 +306,7 @@ describe("generateModuleMetadataInjectorCode", () => {
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
{}
);
}"
})();"
`);
});

Expand All @@ -255,8 +320,8 @@ describe("generateModuleMetadataInjectorCode", () => {
},
});
expect(generatedCode).toMatchInlineSnapshot(`
"{
let _sentryModuleMetadataGlobal =
"(function(){
var _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
Expand All @@ -276,7 +341,7 @@ describe("generateModuleMetadataInjectorCode", () => {
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
{\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}}
);
}"
})();"
`);
});
});
Loading