Skip to content

Commit

Permalink
fix: close watcher on bundle closed
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Aug 7, 2024
1 parent a2a2949 commit 2209690
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type Watcher from 'watcher'
import createDebug from 'debug'
import fs from 'fs-extra'
import path from 'node:path'
import { type PluginOption, type ResolvedConfig, type ViteDevServer } from 'vite'
import { type ConfigEnv, type PluginOption, type ResolvedConfig, type ViteDevServer } from 'vite'
import { buildAllOnce } from './build'
import { globalConfig } from './global-config'
import { resolveOptions } from './helper/default-options'
Expand Down Expand Up @@ -33,6 +33,8 @@ export default function publicTypescript(options: VitePublicTypescriptOptions =
debug('user options:', options)

let viteConfig: ResolvedConfig
let viteConfigEnv: ConfigEnv

let server: ViteDevServer

let opts = {
Expand All @@ -43,8 +45,11 @@ export default function publicTypescript(options: VitePublicTypescriptOptions =
{
name: 'vite:public-typescript',
enforce: 'post',
async configResolved(c) {
viteConfig = c
config(_, _viteConfigEnv) {
viteConfigEnv = _viteConfigEnv
},
async configResolved(_viteConfig) {
viteConfig = _viteConfig

opts = resolveOptions(viteConfig, opts)

Expand All @@ -54,15 +59,13 @@ export default function publicTypescript(options: VitePublicTypescriptOptions =
await setupManifestCache(viteConfig, opts)
},
async configureServer(_server) {
if (viteConfigEnv.command === 'build') return
server = _server
const { ws } = server

globalConfig.set('viteDevServer', server)

if (wathcer) {
wathcer.close()
}

wathcer = await initWatcher((file) => reloadPage(ws, file))
server.httpServer?.addListener('close', () => {
wathcer?.close()
Expand Down Expand Up @@ -145,10 +148,12 @@ export default function publicTypescript(options: VitePublicTypescriptOptions =
})
}
},
closeBundle() {
server?.httpServer?.close()
},
async handleHotUpdate(ctx) {
const { file } = ctx
const { moduleGraph } = server
moduleGraph.onFileChange(resolvedVirtualModuleId)
const module = moduleGraph.getModuleById(resolvedVirtualModuleId)
// virtual module hmr
if (module) {
Expand Down

0 comments on commit 2209690

Please sign in to comment.