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 worlds')
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,
},
})
34 changes: 32 additions & 2 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import EventEmitter from 'events'
import minimist from 'minimist'
import { dim, red } from 'kolorist'
import type { ModuleNode } from 'vite'
import { createServer } from 'vite'
import { ViteNodeServer } from './server'
import { ViteNodeRunner } from './client'
import { viteNodeHmrPlugin } from './hmr'
import { slash } from './utils'

const argv = minimist(process.argv.slice(2), {
'alias': {
Expand Down Expand Up @@ -61,16 +65,20 @@ export interface CliOptions {
_?: string[]
root?: string
config?: string
watch?: boolean
}

async function run(options: CliOptions = {}) {
const files = options.files || options._ || []

const event = new EventEmitter()
const server = await createServer({
logLevel: 'error',
clearScreen: false,
configFile: options.config,
root: options.root,
plugins: [
options.watch && viteNodeHmrPlugin(event),
],
})
await server.pluginContainer.buildStart({})

Expand All @@ -93,5 +101,27 @@ async function run(options: CliOptions = {}) {
for (const file of files)
await runner.executeFile(file)

await server.close()
if (!options.watch)
await server.close()

event.on('updateModules', (modules: ModuleNode[]) => {
walkModules(modules, (module) => {
const { file, url } = module
const id = `/@fs/${slash(file!)}`
runner.moduleCache.delete(url)
runner.moduleCache.delete(id)
runner.executeId(id)
poyoho marked this conversation as resolved.
Show resolved Hide resolved
})
})
}

function walkModules(modules: ModuleNode[], cb: (module: ModuleNode) => void, seen?: Set<string>) {
seen ||= new Set()
modules.forEach((module) => {
if (!seen!.has(module.file!)) {
seen?.add(module.file!)
cb(module)
}
walkModules([...module.importers], cb, seen)
})
}
20 changes: 20 additions & 0 deletions packages/vite-node/src/hmr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { EventEmitter } from 'events'
import type { Plugin, ViteDevServer } from 'vite'

export function viteNodeHmrPlugin(event: EventEmitter): Plugin {
let server: ViteDevServer
return {
name: 'vite-node-hmr',

configureServer(_server) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
server = _server
},

handleHotUpdate(ctx) {
const { modules } = ctx
event.emit('updateModules', modules)
return modules
},
}
}
61 changes: 51 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.