Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vite-node watch mode #890

Merged
merged 15 commits into from
Mar 16, 2022
20 changes: 18 additions & 2 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
})
}