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
16 changes: 16 additions & 0 deletions examples/vite-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "vitest-test",
"private": true,
"main": "index.js",
"license": "MIT",
"type": "module",
"scripts": {
"start": "vite-node --watch ./src/*"
},
"devDependencies": {
"vite-node": "latest"
},
"stackblitz": {
"startCommand": "npm run start"
}
}
6 changes: 6 additions & 0 deletions examples/vite-node/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable no-console */
import { a } from './testModule'

console.log('[index.js] load!')
console.log('hello world')
console.log(a)
4 changes: 4 additions & 0 deletions examples/vite-node/src/testModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const a = 'hello testModule'

// eslint-disable-next-line no-console
console.log('[testModule.js] load!')
12 changes: 12 additions & 0 deletions examples/vite-node/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="vitest" />

// Configure Vitest (https://vitest.dev/config)

import { defineConfig } from 'vite'

export default defineConfig({
test: {
/* for example, use global to avoid globals imports (describe, test, expect): */
// globals: true,
},
})
15 changes: 13 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,16 @@ 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('all', async(eventName, path) => {
// eslint-disable-next-line no-console
console.log(dim(`[update] ${path}`))
// because module don't had `import.meta.hot.accept`
// only can refresh all module
runner.moduleCache.clear()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at least we should keep the cache of node_modules.

for (const file of files)
await runner.executeFile(file)
})
}