Skip to content
Merged
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
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

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
Loading