Skip to content

Commit

Permalink
feat: vite-node watch mode (#890)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
poyoho and antfu committed Mar 16, 2022
1 parent 980e00b commit 1722f98
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/vite-node/src/cli.ts
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)
})
}

0 comments on commit 1722f98

Please sign in to comment.