diff --git a/packages/vite-node/src/cli.ts b/packages/vite-node/src/cli.ts index 88668100baf3..0fcb87cce1da 100644 --- a/packages/vite-node/src/cli.ts +++ b/packages/vite-node/src/cli.ts @@ -61,11 +61,11 @@ export interface CliOptions { _?: string[] root?: string config?: string + watch?: boolean } async function run(options: CliOptions = {}) { const files = options.files || options._ || [] - const server = await createServer({ logLevel: 'error', clearScreen: false, @@ -93,5 +93,21 @@ async function run(options: CliOptions = {}) { for (const file of files) await runner.executeFile(file) - await server.close() + if (!options.watch) + await server.close() + + server.watcher.on('change', async(eventName, path) => { + // eslint-disable-next-line no-console + console.log(dim(`[${eventName}] ${path}`)) + + // invalidate module cache but not node_modules + Array.from(runner.moduleCache.keys()) + .forEach((i) => { + if (!i.includes('node_modules')) + runner.moduleCache.delete(i) + }) + + for (const file of files) + await runner.executeFile(file) + }) }