vite: let a framework build an environment in another process

Open
#4,974 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
node.js, typescript, vite

Research direction

Start by reading src/build/vite/prod.ts, especially buildEnvironments, then inspect src/build/vite/plugin.ts around nitro:prepare. Trace when Nitro reads client output and cleans the output directory; the issue is done when a framework can await an external environment build and avoid cleanup in a client-only process, while the default build remains serial.

Written by the indexing model from the issue text.

Description

discussion performance vite

I tried building the client in a second process while the main process builds SSR, on a Nuxt 5 app. On a big test app (12 layers, about 2,900 components) a clean build went from about 35 s to 23.5 s, with the same output file for file. On a real app it saved about 8 s of 140 s, because Nitro's own build was most of that build there.

Two processes are needed. With Promise.all in one process the build wasn't faster (37.5 s), because each build keeps the JS thread busy about 90% of the time.

Most of this works today. A framework sets builder.environments.client.isBuilt = true in a buildApp hook before Nitro's and builds the client somewhere else. Nitro then skips it. But Nitro assumes isBuilt means the files are already on disk:

  1. Right after the loop in buildEnvironments, Nitro reads the client's index.html for the renderer template, calls writeAssetsManifest, copies public assets and builds the server. Nothing waits for the other process. In my Nuxt test it only works because Nuxt's SSR generateBundle happens to wait for the client.
  2. nitro:prepare cleans the output directory in every process that runs Nitro's plugin. The second process has to avoid it, or it can delete what the main process already wrote.

Some ideas, up to you:

  • A way to register a pending build that buildEnvironments awaits before it goes on. isBuilt could accept a promise, or there could be a hook a framework can await in.
  • A way to skip prepare in a process that only builds a client environment.

The default should stay serial. Frameworks like Nuxt pass state between environments in memory, and in one process it doesn't help anyway.

I haven't proposed the Nuxt side yet, so this is early. If you'd rather wait until a framework actually needs it, that's fine.

Related: the Vite guide shows Promise.all in buildApp, sapphi-red's note that plugin workarounds "won't work when all envs are built concurrently" in vitejs/vite#16358, and hi-ogawa/vite-plugins#350, an open WIP that builds browser and server in parallel.

Versions: Nitro 3.0.260903-beta (same code on main at f794c378), Vite 8.3.5, Rolldown 1.2.15, Node 24.

Dominant language
TypeScript
Stars
11.5k
Forks
968
Avg merge
4d 9h
Merged PRs (30d)
64

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.