Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Wrap injected code in block-statement to contain scope #646

Merged
merged 3 commits into from
Jan 15, 2025
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
8 changes: 5 additions & 3 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({
}) {
// 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 = `
var _global =
let code = `{
const _global =
typeof window !== 'undefined' ?
window :
typeof global !== 'undefined' ?
Expand All @@ -335,6 +335,8 @@ export function generateGlobalInjectorCode({
_global.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
}

code += "}";

return code;
}

Expand All @@ -344,7 +346,7 @@ export function generateModuleMetadataInjectorCode(metadata: any) {
// 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 `{
var _sentryModuleMetadataGlobal =
const _sentryModuleMetadataGlobal =
Copy link
Collaborator Author

@timfish timfish Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this too because block scoping rules for var mean that it wasn't working as expected

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am all for this but wondering whether we should cut a major in case somebody uses the bundler plugins for apps in an env where const isn't a thing yet.

Copy link
Collaborator Author

@timfish timfish Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind which way we go but we should either:

  1. Use var and wrap in self-invoking function
  2. Use var and wrap in try/catch
  3. Use block-statement with const or let

typeof window !== "undefined"
? window
: typeof global !== "undefined"
Expand Down
4 changes: 2 additions & 2 deletions packages/bundler-plugin-core/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe("generateModuleMetadataInjectorCode", () => {
const generatedCode = generateModuleMetadataInjectorCode({});
expect(generatedCode).toMatchInlineSnapshot(`
"{
var _sentryModuleMetadataGlobal =
const _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
Expand Down Expand Up @@ -256,7 +256,7 @@ describe("generateModuleMetadataInjectorCode", () => {
});
expect(generatedCode).toMatchInlineSnapshot(`
"{
var _sentryModuleMetadataGlobal =
const _sentryModuleMetadataGlobal =
typeof window !== \\"undefined\\"
? window
: typeof global !== \\"undefined\\"
Expand Down
Loading