Skip to content

Commit b734d45

Browse files
authored
fix!: Wrap injected code in block-statement to contain scope (#646)
1 parent 0074fb8 commit b734d45

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ export function generateGlobalInjectorCode({
314314
}) {
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-
var _global =
317+
let code = `{
318+
const _global =
319319
typeof window !== 'undefined' ?
320320
window :
321321
typeof global !== 'undefined' ?
@@ -335,6 +335,8 @@ export function generateGlobalInjectorCode({
335335
_global.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
336336
}
337337

338+
code += "}";
339+
338340
return code;
339341
}
340342

@@ -344,7 +346,7 @@ export function generateModuleMetadataInjectorCode(metadata: any) {
344346
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
345347
// We are merging the metadata objects in case modules are bundled twice with the plugin
346348
return `{
347-
var _sentryModuleMetadataGlobal =
349+
const _sentryModuleMetadataGlobal =
348350
typeof window !== "undefined"
349351
? window
350352
: typeof global !== "undefined"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe("generateModuleMetadataInjectorCode", () => {
221221
const generatedCode = generateModuleMetadataInjectorCode({});
222222
expect(generatedCode).toMatchInlineSnapshot(`
223223
"{
224-
var _sentryModuleMetadataGlobal =
224+
const _sentryModuleMetadataGlobal =
225225
typeof window !== \\"undefined\\"
226226
? window
227227
: typeof global !== \\"undefined\\"
@@ -256,7 +256,7 @@ describe("generateModuleMetadataInjectorCode", () => {
256256
});
257257
expect(generatedCode).toMatchInlineSnapshot(`
258258
"{
259-
var _sentryModuleMetadataGlobal =
259+
const _sentryModuleMetadataGlobal =
260260
typeof window !== \\"undefined\\"
261261
? window
262262
: typeof global !== \\"undefined\\"

0 commit comments

Comments
 (0)