Skip to content

test(lighthouse-react): Use static imports gated by MODE for tree-shaking#21030

Merged
mydea merged 1 commit into
developfrom
feat/lighthouse-react-static-imports
May 20, 2026
Merged

test(lighthouse-react): Use static imports gated by MODE for tree-shaking#21030
mydea merged 1 commit into
developfrom
feat/lighthouse-react-static-imports

Conversation

@mydea
Copy link
Copy Markdown
Member

@mydea mydea commented May 20, 2026

Summary

Replace the dynamic await import('./sentry/<mode>') branching in main.tsx with a single static import * as Sentry from '@sentry/react' guarded by import.meta.env.MODE. This more closely mirrors what a real consumer app looks like: a static SDK import at the top, with Sentry.init() called conditionally. The build matrix still produces three distinct bundles via vite build --mode <mode>, but the code path is now realistic instead of a code-splitting trick.

How tree-shaking handles each mode

Vite inlines import.meta.env.MODE as a string literal at build time. Rollup then eliminates dead if branches, and @sentry/react's "sideEffects": false lets the namespace import be tree-shaken away entirely when no member is referenced.

  • no-sentry: both branches are statically dead → Sentry.* unreferenced → the entire @sentry/react import is dropped from the bundle.
  • init-only: only Sentry.init({ enabled: false }) survives; browserTracingIntegration / replayIntegration are tree-shaken.
  • tracing-replay: full SDK + tracing + replay.

Verified bundle output

Mode Bundle Sentry SDK symbols¹
no-sentry 144 KB 0
init-only 229 KB (+85 KB) present
tracing-replay 418 KB (+274 KB) 7

¹ Count of captureException / startSpan / getCurrentScope / __SENTRY__ / sentry-trace occurrences in the output JS. The only Sentry literal in the no-sentry bundle is the marketing copy string in App.tsx, not SDK code.

The per-mode helper files in src/sentry/ are no longer needed and have been deleted.

…king

Replace the dynamic `await import('./sentry/<mode>')` branching in
`main.tsx` with a single static `import * as Sentry from '@sentry/react'`
guarded by `import.meta.env.MODE`. Vite inlines `MODE` as a literal at
build time, Rollup eliminates the dead branches, and `@sentry/react`'s
`sideEffects: false` lets the namespace import be tree-shaken away
entirely in `no-sentry` mode. This matches what a real consumer app
looks like (static SDK import, conditional init).

Verified bundle output:
  - no-sentry:      144 KB, 0 Sentry SDK symbols
  - init-only:      229 KB (+85 KB core)
  - tracing-replay: 418 KB (+274 KB core + tracing + replay)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mydea mydea requested a review from HazAT May 20, 2026 07:00
@mydea mydea self-assigned this May 20, 2026
@mydea mydea enabled auto-merge (squash) May 20, 2026 07:01
@mydea mydea merged commit a2f9c1a into develop May 20, 2026
44 checks passed
@mydea mydea deleted the feat/lighthouse-react-static-imports branch May 20, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants