Skip to content

perf(browser): pre-bundle the vitest runtime in optimizeDeps - #10713

Merged
sheremet-va merged 5 commits into
mainfrom
perf/browser-mode
Jul 23, 2026
Merged

sheremet-va merged 5 commits into
mainfrom
perf/browser-mode

Conversation

@sheremet-va

@sheremet-va sheremet-va commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

Pre-bundles the vitest runtime for browser mode so the browser fetches a few optimized chunks instead of ~20 separately-served dist chunks (faster startup, fewer module requests).

vitest, vitest/internal/browser and @vitest/browser/client are moved from optimizeDeps.exclude to include. They are optimized together in a single pass, so esbuild dedupes their shared stateful chunks (the test collector, the runner, the RPC client) to a single instance — preserving module identity between the test files' import 'vitest' and the tester. Their transitive deps (@vitest/utils, @vitest/spy, pathe, tinyrainbow, …) are inlined into these bundles. The virtual per-server modules (@vitest/browser/context, …), vite/module-runner and msw must stay excluded (see the comment in index.ts).

Shared-server resolver fix. The vitest:resolve-core plugin (vitestResolver.ts) short-circuits bare vitest imports straight to the running copy's dist file. It now lives on the single Vite server shared by every environment, so it also governs the browser client environment where the runtime is pre-bundled — without care the tester would use the optimized bundle while test files receive the raw dist graph, producing two @vitest/runner instances and failing every test() with "Vitest failed to find the current suite". When the environment includes vitest in optimizeDeps, the plugin now routes the import through the optimizer (this.resolve) so both share one instance; every other environment keeps the pinned dist resolution.

Stack frames pointing into the optimizer cache (.vite/vitest) are ignored in stack traces like the un-bundled /vitest/dist/ ones. The console-logging spec now runs with an explicit default reporter and asserts on Vitest's own console forwarding: once the runtime is pre-bundled the tester loads a stubbed @vite/client, so Vite's client console relay no longer runs.

@hi-ogawa hi-ogawa left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't look through yet, but it looks like it involves many independent changes. Is there a reasonable way to split PRs to ease reviews? (perhaps sourcemap related stuff?)

@sheremet-va

sheremet-va commented Jul 7, 2026 •

Copy link
Copy Markdown
Member Author

There are already 4 PRs stacked on top of each other 😩 It’s gonna be a merge headache. I’ll ask claude to split it 😔

@sheremet-va
sheremet-va changed the base branch from perf/improve-loading-perf to main July 7, 2026 10:22
@netlify

netlify Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 6baf7dd
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a60d1457d2d310008fa4826
😎 Deploy Preview https://deploy-preview-10713--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

The browser now fetches a few optimized chunks instead of ~20
separately-served dist chunks. vitest, vitest/internal/browser and
@vitest/browser/client are optimized in a single pass so esbuild dedupes
their shared stateful chunks, preserving module identity between the test
files' import of 'vitest' and the tester. Frames from the optimizer cache
(.vite/vitest) are ignored in stack traces like the un-bundled dist ones.
@sheremet-va sheremet-va changed the title perf(browser): reduce headless run overhead perf(browser): pre-bundle the vitest runtime in optimizeDeps Jul 7, 2026
@sheremet-va
sheremet-va requested a review from hi-ogawa July 14, 2026 09:39

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread packages/utils/src/source-map.ts Resolved
Comment thread packages/browser/src/node/index.ts Resolved
@sheremet-va
sheremet-va requested a review from hi-ogawa July 22, 2026 14:26
@sheremet-va
sheremet-va requested a review from hi-ogawa July 23, 2026 08:42
@sheremet-va

Copy link
Copy Markdown
Member Author
Screenshot 2026-07-23 at 10 45 00

First time seeing this 🤷

@hi-ogawa

Copy link
Copy Markdown
Collaborator

It's spinning for me too.

@sheremet-va

sheremet-va commented Jul 23, 2026 •

Copy link
Copy Markdown
Member Author

Should be done spinning @hi-ogawa

Removing the line is enough, I also added deps/vitest.js just in case, although most of the code will probably end up in chunks 👀 (But CI doesn't confirm this, everything passes 🤷 )

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I see the commit!

@sheremet-va
sheremet-va merged commit de63174 into main Jul 23, 2026
27 checks passed
@sheremet-va
sheremet-va deleted the perf/browser-mode branch July 23, 2026 09:30
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants