Add section on watching a monorepo
Documentation is
- Missing
- Outdated
- Confusing
- Not sure?
Explain in Detail
If you have a monorepo like this:
package.json
vite.config.common.ts
workspace-a/
packages/
package-a-1/
package.json
vite.config.ts
src
…
workspace-b/
packages/
package-b-2/
package.json
vite.config.ts
src
…and run vite serve in one of the packages (e.g. package-a-1), only files in that package will be watched for HMR. Changes in other packages (e.g. package-b-2) require a manual server restart.
This is because the chokidar.watch options are hardcoded to {root, the config files, and envDir}:
envDir is meant to be a folder for saving environment variables. Conveniently, the monorepo root is a good choice for this value, and if you specify it, the other packages in the monorepo will be watched for changes.
envDir may also have a bunch of junk that can be ignored. In my case, yarn and wireit both have caches that don't need to be watched for changes.
Thus, this snippet in vite.config.common.ts ensures that the other src files in my monorepo are being watched for changes:
// Vite only watches the package root, config files, and envDir. By
// setting envDir to the monorepo root (and then exempting cached files),
// we ensure that all our packages are watched.
envDir: __dirname,
server: {
watch: {
ignored: [
'**/.wireit/**',
'**/.yarn/**',
],
}
},Your Suggestion for Changes
The only current mention of using Vite in a monorepo is about linkedDependencies. Perhaps there should be a section/guide/etc dedicated to configuring Vite for a monorepo.
Additionally, maybe Vite should:
- parse
.gitignore, so it doesn't need to be manually duplicated inignored - provide a
projectRootoption that is automatically watched (and clarify that the currentrootis a package root). This would remove the need to overloadenvDir, such that they could go in their own/envfolder.
Reproduction
No response
Steps to reproduce
No response
Source: vitejs/vite