š vite-plugin-watch-and-run
Adding watch mode to any command / Tool!
Installation
Section titled āInstallationānpm i -D vite-plugin-watch-and-runConfiguration
Section titled āConfigurationāAdd watchAndRun plugin with the following configuration:
watch: a glob pattern to watch for changes. This will be matched against the absolute path for altered files.run: a command to trigger when a file change is detected (You can be very creative š„³!)
import path from 'path'import { watchAndRun } from 'vite-plugin-watch-and-run'import { defineConfig } from 'vite'
export default defineConfig({ plugins: [ watchAndRun([ { name: 'gen', watch: path.resolve('src/**/*.(gql|svelte)'), run: 'npm run gen' // watchKind: ['add', 'change', 'unlink'], // (default) // delay: 300 // (default) } ]) ]}Side Notes
Section titled āSide Notesā-
Full list of
watchKindcan be found here: https://github.com/paulmillr/chokidar#api -
delayis good in case you have 200 files added realy fast! Like this the cmd is executed only once. -
For the
runcommand we recommend to usenpm run xxxas it will work fornpm,yarnandpnpmš -
watchinfos- You can use glob patterns to watch for changes under the root directory.
{ watch: path.resolve('**/*.ts')}- You can use absolute path to watch for changes on a specific file on your machine. Thatās useful if you want to watch for changes on a file that is in your monorepo for example!
{ watch: path.resolve('../../README.md')}- You can also go with
watchFilethat is a function that will be called with the filepath. Inside, you can decide what to do.