perf(build): Optimize terser minifier config for CDN bundles#19852
perf(build): Optimize terser minifier config for CDN bundles#19852HazAT wants to merge 1 commit intoautoresearch/browser-bundle-size-2026-03-17from
Conversation
Enable additional terser compress and mangle options that safely reduce
the size of CDN bundle .min.js files:
- compress.passes: 5 (multi-pass finds more dead code)
- compress.ecma: 2020 (allows modern syntax: nullish coalescing, optional chaining)
- compress.toplevel: true (better variable inlining within the IIFE)
- compress.unsafe_arrows: true (function → arrow where this is unused, ~1.3KB raw)
- compress.unsafe_methods: true ({ m: function(){} } → { m(){} })
- compress.unsafe_comps/unsafe_math/pure_getters: safe algebraic opts
- mangle.toplevel: true (mangle top-level names inside IIFE scope)
Also adds 'sW' to the mangle reserved list (used by a follow-up change that
shortens the sentryWrapped function name for frame stripping).
These options only affect CDN .min.js bundles, not npm ESM/CJS output.
Saves ~300 bytes gzipped on the base browser bundle.
Co-Authored-By: Claude claude@anthropic.com
| reserved: ['captureException', 'captureMessage', 'sentryWrapped', 'sW'], | ||
| toplevel: true, |
There was a problem hiding this comment.
Bug: Enabling mangle.toplevel: true may cause Terser to mangle the sentryWrapped function name due to a known bug, which will break internal stack trace filtering.
Severity: MEDIUM
Suggested Fix
To mitigate the risk from the Terser bug, either disable the mangle.toplevel: true option or find an alternative way to identify the sentryWrapped frame that does not rely on its function name remaining unmangled in the minified bundle.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: dev-packages/rollup-utils/plugins/bundlePlugins.mjs#L104-L105
Potential issue: Enabling the Terser option `mangle.toplevel: true` introduces a risk of
breaking stack trace filtering. A known bug in Terser can cause it to mangle function
names like `sentryWrapped` even when they are explicitly added to the `reserved` list.
This function is defined at the top level of an IIFE, making it susceptible to this bug.
If `sentryWrapped` is renamed, a regex check (`/sentryWrapped/`) used to filter internal
Sentry frames from stack traces will fail. This results in internal SDK frames appearing
in user-facing stack traces, which can degrade data quality and affect error grouping.
Did we get this right? 👍 / 👎 to inform future reviews.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| // are all listed here just for the clarity's sake, as they are all used in the frames manipulation process. | ||
| reserved: ['captureException', 'captureMessage', 'sentryWrapped'], | ||
| reserved: ['captureException', 'captureMessage', 'sentryWrapped', 'sW'], | ||
| toplevel: true, |
There was a problem hiding this comment.
Toplevel mangling renames global Sentry IIFE variable
High Severity
The mangle.toplevel: true and compress.toplevel: true options will mangle or eliminate the top-level var Sentry declaration produced by rollup's IIFE format (name: 'Sentry' in bundleHelpers.mjs). Since Sentry is not in the reserved list, terser sees it as an unreferenced top-level variable (the window.Sentry inside the IIFE is a property access, not a variable reference) and will rename it to a shorter name. This breaks window.Sentry for all CDN bundle consumers.
Additional Locations (1)
There was a problem hiding this comment.
Confirmed that this is accurate. Strips out the var Sentry= prefix at the start of the rollup. I'll poke around to see if there's a workaround.
size-limit report 📦
|
node-overhead report 🧳Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.
|


Summary
Enable additional terser compress and mangle options that safely reduce CDN
.min.jsbundle sizes. Saves ~300 bytes gzipped on the base browser bundle.Changes
compress.passes: 5compress.ecma: 2020compress.toplevel: truecompress.unsafe_arrows: truefunctionto=>wherethisis unused (~1.3KB raw)compress.unsafe_methods: true{ m(){} }compress.unsafe_comps / unsafe_math / pure_gettersmangle.toplevel: trueThese options only affect CDN
.min.jsbundles, not npm ESM/CJS output. Theunsafe_*options are safe for our codebase because the CDN bundles run in browser contexts where the assumptions hold.Also pre-reserves
sWin the mangle list for a follow-up change.Part of #19833.
Co-Authored-By: Claude claude@anthropic.com