Skip to content

Commit 8237ac9

Browse files
grushetskyLms24
andauthored
fix(core): Make plugin inject ES5-friendly code (#770)
* fix(webpack): make plugin inject ES5-friendly code Sentry webpack plugin injects code that includes `let` that breaks in ES5-only environments. `let` is substituted for `var` wrapped in an IIFE in order to keep the same scope for the declared variable. * add test for release and build info, minimal snippet format change * changelog credits --------- Co-authored-by: Lukas Stracke <[email protected]>
1 parent 8f059c2 commit 8237ac9

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- "You know what they say ‘Fool me once, strike one, but fool me twice… strike three.’" — Michael Scott
66

7+
- fix(core): Make plugin inject ES5-friendly code ([#770](https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/770))
8+
9+
Work in this release was contributed by @grushetsky. Thank you for your contribution!
10+
711
## 4.0.0
812

913
### Breaking Changes

packages/bundler-plugin-core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ export function createComponentNameAnnotateHooks(ignoredComponents?: string[]):
456456
}
457457

458458
export function getDebugIdSnippet(debugId: string): string {
459-
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){}};`;
459+
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){}};`;
460460
}
461461

462462
export type { Logger } from "./logger";

packages/bundler-plugin-core/src/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ export function generateGlobalInjectorCode({
314314
}): string {
315315
// The code below is mostly ternary operators because it saves bundle size.
316316
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
317-
let code = `{
318-
let _global =
317+
let code = `(function(){
318+
var _global =
319319
typeof window !== 'undefined' ?
320320
window :
321321
typeof global !== 'undefined' ?
@@ -332,10 +332,10 @@ export function generateGlobalInjectorCode({
332332
const buildInfo = getBuildInformation();
333333

334334
code += `
335-
_global.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
335+
_global.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
336336
}
337337

338-
code += "}";
338+
code += "})();";
339339

340340
return code;
341341
}
@@ -345,8 +345,8 @@ export function generateModuleMetadataInjectorCode(metadata: any): string {
345345
// The code below is mostly ternary operators because it saves bundle size.
346346
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
347347
// We are merging the metadata objects in case modules are bundled twice with the plugin
348-
return `{
349-
let _sentryModuleMetadataGlobal =
348+
return `(function(){
349+
var _sentryModuleMetadataGlobal =
350350
typeof window !== "undefined"
351351
? window
352352
: typeof global !== "undefined"
@@ -366,7 +366,7 @@ export function generateModuleMetadataInjectorCode(metadata: any): string {
366366
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
367367
${JSON.stringify(metadata)}
368368
);
369-
}`;
369+
})();`;
370370
}
371371

372372
export function getBuildInformation(): {

packages/bundler-plugin-core/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe("getDebugIdSnippet", () => {
44
it("returns the debugId injection snippet for a passed debugId", () => {
55
const snippet = getDebugIdSnippet("1234");
66
expect(snippet).toMatchInlineSnapshot(
7-
`";{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){}};"`
7+
`";{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){}};"`
88
);
99
});
1010
});

packages/bundler-plugin-core/test/utils.test.ts

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import {
2+
generateGlobalInjectorCode,
23
generateModuleMetadataInjectorCode,
34
getDependencies,
45
getPackageJson,
56
parseMajorVersion,
67
replaceBooleanFlagsInCode,
78
stringToUUID,
89
} from "../src/utils";
10+
11+
import fs from "fs";
12+
913
import path from "node:path";
1014

1115
type PackageJson = Record<string, unknown>;
@@ -216,12 +220,73 @@ if (false && true) {
216220
});
217221
});
218222

223+
describe("generateGlobalInjectorCode", () => {
224+
it("generates code with release", () => {
225+
const generatedCode = generateGlobalInjectorCode({
226+
release: "1.2.3",
227+
injectBuildInformation: false,
228+
});
229+
230+
expect(generatedCode).toMatchInlineSnapshot(`
231+
"(function(){
232+
var _global =
233+
typeof window !== 'undefined' ?
234+
window :
235+
typeof global !== 'undefined' ?
236+
global :
237+
typeof globalThis !== 'undefined' ?
238+
globalThis :
239+
typeof self !== 'undefined' ?
240+
self :
241+
{};
242+
243+
_global.SENTRY_RELEASE={id:\\"1.2.3\\"};})();"
244+
`);
245+
});
246+
247+
it("generates code with release and build information", () => {
248+
jest.spyOn(fs, "readFileSync").mockReturnValueOnce(
249+
JSON.stringify({
250+
name: "test-app",
251+
dependencies: {
252+
myDep: "^2.1.4",
253+
},
254+
devDependencies: {
255+
rollup: "^3.1.4",
256+
},
257+
})
258+
);
259+
260+
const generatedCode = generateGlobalInjectorCode({
261+
release: "1.2.3",
262+
injectBuildInformation: true,
263+
});
264+
265+
expect(generatedCode).toMatchInlineSnapshot(`
266+
"(function(){
267+
var _global =
268+
typeof window !== 'undefined' ?
269+
window :
270+
typeof global !== 'undefined' ?
271+
global :
272+
typeof globalThis !== 'undefined' ?
273+
globalThis :
274+
typeof self !== 'undefined' ?
275+
self :
276+
{};
277+
278+
_global.SENTRY_RELEASE={id:\\"1.2.3\\"};
279+
_global.SENTRY_BUILD_INFO={\\"deps\\":[\\"myDep\\",\\"rollup\\"],\\"depsVersions\\":{\\"rollup\\":3},\\"nodeVersion\\":18};})();"
280+
`);
281+
});
282+
});
283+
219284
describe("generateModuleMetadataInjectorCode", () => {
220285
it("generates code with empty metadata object", () => {
221286
const generatedCode = generateModuleMetadataInjectorCode({});
222287
expect(generatedCode).toMatchInlineSnapshot(`
223-
"{
224-
let _sentryModuleMetadataGlobal =
288+
"(function(){
289+
var _sentryModuleMetadataGlobal =
225290
typeof window !== \\"undefined\\"
226291
? window
227292
: typeof global !== \\"undefined\\"
@@ -241,7 +306,7 @@ describe("generateModuleMetadataInjectorCode", () => {
241306
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
242307
{}
243308
);
244-
}"
309+
})();"
245310
`);
246311
});
247312

@@ -255,8 +320,8 @@ describe("generateModuleMetadataInjectorCode", () => {
255320
},
256321
});
257322
expect(generatedCode).toMatchInlineSnapshot(`
258-
"{
259-
let _sentryModuleMetadataGlobal =
323+
"(function(){
324+
var _sentryModuleMetadataGlobal =
260325
typeof window !== \\"undefined\\"
261326
? window
262327
: typeof global !== \\"undefined\\"
@@ -276,7 +341,7 @@ describe("generateModuleMetadataInjectorCode", () => {
276341
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
277342
{\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}}
278343
);
279-
}"
344+
})();"
280345
`);
281346
});
282347
});

0 commit comments

Comments
 (0)