Skip to content

Commit e97cb55

Browse files
committed
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.
1 parent 8f059c2 commit e97cb55

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

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: 6 additions & 6 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' ?
@@ -335,7 +335,7 @@ export function generateGlobalInjectorCode({
335335
_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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ describe("generateModuleMetadataInjectorCode", () => {
220220
it("generates code with empty metadata object", () => {
221221
const generatedCode = generateModuleMetadataInjectorCode({});
222222
expect(generatedCode).toMatchInlineSnapshot(`
223-
"{
224-
let _sentryModuleMetadataGlobal =
223+
"(function(){
224+
var _sentryModuleMetadataGlobal =
225225
typeof window !== \\"undefined\\"
226226
? window
227227
: typeof global !== \\"undefined\\"
@@ -241,7 +241,7 @@ describe("generateModuleMetadataInjectorCode", () => {
241241
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
242242
{}
243243
);
244-
}"
244+
})();"
245245
`);
246246
});
247247

@@ -255,8 +255,8 @@ describe("generateModuleMetadataInjectorCode", () => {
255255
},
256256
});
257257
expect(generatedCode).toMatchInlineSnapshot(`
258-
"{
259-
let _sentryModuleMetadataGlobal =
258+
"(function(){
259+
var _sentryModuleMetadataGlobal =
260260
typeof window !== \\"undefined\\"
261261
? window
262262
: typeof global !== \\"undefined\\"
@@ -276,7 +276,7 @@ describe("generateModuleMetadataInjectorCode", () => {
276276
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
277277
{\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}}
278278
);
279-
}"
279+
})();"
280280
`);
281281
});
282282
});

0 commit comments

Comments
 (0)