Skip to content

Commit 06b6860

Browse files
committed
feat: support watch mode
1 parent 9ea6313 commit 06b6860

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
},
6262
"dependencies": {
6363
"@rollup/pluginutils": "^4.1.1",
64+
"chokidar": "^3.5.2",
6465
"debug": "^4.3.2",
6566
"fast-glob": "^3.2.7",
6667
"has-pkg": "^0.0.1",

pnpm-lock.yaml

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/context.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { relative } from 'path'
2+
import fs from 'fs'
23
import Debug from 'debug'
34
import { UpdatePayload, ViteDevServer } from 'vite'
45
import { throttle, toArray, slash } from '@antfu/utils'
@@ -60,19 +61,25 @@ export class Context {
6061
return this.transformer(code, id, path, query)
6162
}
6263

63-
setViteServer(server: ViteDevServer) {
64+
setupViteServer(server: ViteDevServer) {
65+
if (this._server === server)
66+
return
67+
6468
this._server = server
69+
this.setupWather(server.watcher)
70+
}
6571

72+
setupWather(watcher: fs.FSWatcher) {
6673
const { globs } = this.options
6774

68-
server.watcher
75+
watcher
6976
.on('unlink', (path) => {
7077
if (!matchGlobs(path, globs))
7178
return
7279
this.removeComponents(path)
7380
this.onUpdate(path)
7481
})
75-
server.watcher
82+
watcher
7683
.on('add', (path) => {
7784
if (!matchGlobs(path, globs))
7885
return
@@ -125,6 +132,8 @@ export class Context {
125132
}
126133

127134
onUpdate(path: string) {
135+
this.generateDeclaration()
136+
128137
if (!this._server)
129138
return
130139

@@ -150,8 +159,6 @@ export class Context {
150159

151160
if (payload.updates.length)
152161
this._server.ws.send(payload)
153-
154-
this.generateDeclaration()
155162
}
156163

157164
private updateComponentNameMap() {

src/core/unplugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createUnplugin } from 'unplugin'
22
import { createFilter } from '@rollup/pluginutils'
3+
import chokidar from 'chokidar'
34
import { Options } from '../types'
45
import { Context } from './context'
56
import { shouldTransform } from './utils'
@@ -44,9 +45,12 @@ export default createUnplugin<Options>((options = {}) => {
4445
ctx.searchGlob()
4546
ctx.generateDeclaration()
4647
}
48+
49+
if (config.build.watch && config.command === 'build')
50+
ctx.setupWather(chokidar.watch(ctx.options.globs))
4751
},
4852
configureServer(server) {
49-
ctx.setViteServer(server)
53+
ctx.setupViteServer(server)
5054
},
5155
},
5256
}

0 commit comments

Comments
 (0)