test(lighthouse-react): Use static imports gated by MODE for tree-shaking#21030
Merged
Conversation
…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>
HazAT
approved these changes
May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace the dynamic
await import('./sentry/<mode>')branching inmain.tsxwith a single staticimport * as Sentry from '@sentry/react'guarded byimport.meta.env.MODE. This more closely mirrors what a real consumer app looks like: a static SDK import at the top, withSentry.init()called conditionally. The build matrix still produces three distinct bundles viavite 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.MODEas a string literal at build time. Rollup then eliminates deadifbranches, and@sentry/react's"sideEffects": falselets 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/reactimport is dropped from the bundle.init-only: onlySentry.init({ enabled: false })survives;browserTracingIntegration/replayIntegrationare tree-shaken.tracing-replay: full SDK + tracing + replay.Verified bundle output
no-sentryinit-onlytracing-replay¹ Count of
captureException/startSpan/getCurrentScope/__SENTRY__/sentry-traceoccurrences in the output JS. The onlySentryliteral in theno-sentrybundle is the marketing copy string inApp.tsx, not SDK code.The per-mode helper files in
src/sentry/are no longer needed and have been deleted.