Skip to content

Commit a2f9c1a

Browse files
mydeaclaude
andauthored
test(lighthouse-react): Use static imports gated by MODE for tree-shaking (#21030)
## 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. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 16734d0 commit a2f9c1a

4 files changed

Lines changed: 19 additions & 38 deletions

File tree

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
import * as Sentry from '@sentry/react';
12
import { createRoot } from 'react-dom/client';
23
import App from './App';
34

4-
async function bootstrap() {
5-
const mode = import.meta.env.MODE;
6-
if (mode === 'init-only') {
7-
const { initSentry } = await import('./sentry/init-only');
8-
initSentry();
9-
} else if (mode === 'tracing-replay') {
10-
const { initSentry } = await import('./sentry/tracing-replay');
11-
initSentry();
12-
}
13-
// 'no-sentry' mode: do not import any sentry module — the dynamic-import
14-
// branches above are unreachable and Vite drops them from the bundle.
15-
16-
const root = createRoot(document.getElementById('root')!);
17-
root.render(<App />);
5+
if (import.meta.env.MODE === 'tracing-replay') {
6+
Sentry.init({
7+
dsn: import.meta.env.VITE_E2E_TEST_DSN as string | undefined,
8+
release: 'lighthouse-fixture',
9+
environment: 'qa',
10+
integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
11+
tracesSampleRate: 1.0,
12+
replaysSessionSampleRate: 1.0,
13+
replaysOnErrorSampleRate: 1.0,
14+
});
15+
} else if (import.meta.env.MODE === 'init-only') {
16+
// enabled: false makes the SDK a guaranteed no-op (no transport allocation,
17+
// no DSN warning). We're measuring pure SDK-loading + tree-shaking cost.
18+
Sentry.init({ enabled: false });
1819
}
20+
// 'no-sentry' mode: both branches above are statically dead, so Vite drops
21+
// the @sentry/react import entirely from the bundle.
1922

20-
void bootstrap();
23+
const root = createRoot(document.getElementById('root')!);
24+
root.render(<App />);

dev-packages/e2e-tests/test-applications/lighthouse-react/src/sentry/init-only.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

dev-packages/e2e-tests/test-applications/lighthouse-react/src/sentry/no-sentry.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

dev-packages/e2e-tests/test-applications/lighthouse-react/src/sentry/tracing-replay.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)