1
1
import { relative } from 'path'
2
2
import Debug from 'debug'
3
- import chokidar from 'chokidar'
4
3
import { ResolvedConfig , UpdatePayload , ViteDevServer } from 'vite'
5
- import { throttle } from '@antfu/utils'
4
+ import { throttle , toArray , slash } from '@antfu/utils'
6
5
import { Options , ComponentInfo , ResolvedOptions } from './types'
7
- import { pascalCase , toArray , getNameFromFilePath , resolveAlias , resolveOptions , matchGlobs , slash } from './utils'
6
+ import { pascalCase , getNameFromFilePath , resolveAlias , resolveOptions , matchGlobs } from './utils'
8
7
import { searchComponents } from './fs/glob'
9
8
import { generateDeclaration } from './declaration'
10
9
@@ -29,25 +28,6 @@ export class Context {
29
28
public readonly viteConfig : ResolvedConfig ,
30
29
) {
31
30
this . options = resolveOptions ( options , viteConfig )
32
- const { globs, dirs } = this . options
33
-
34
- if ( viteConfig . command === 'serve' ) {
35
- // TODO: use vite's watcher instead
36
- chokidar . watch ( dirs , { ignoreInitial : true , cwd : this . root } )
37
- . on ( 'unlink' , ( path ) => {
38
- if ( matchGlobs ( path , globs ) ) {
39
- this . removeComponents ( path )
40
- this . onUpdate ( path )
41
- }
42
- } )
43
- . on ( 'add' , ( path ) => {
44
- if ( matchGlobs ( path , globs ) ) {
45
- this . addComponents ( path )
46
- this . onUpdate ( path )
47
- }
48
- } )
49
- }
50
-
51
31
this . generateDeclaration = throttle ( 500 , false , this . generateDeclaration . bind ( this ) )
52
32
}
53
33
@@ -57,6 +37,24 @@ export class Context {
57
37
58
38
setServer ( server : ViteDevServer ) {
59
39
this . _server = server
40
+
41
+ const { globs, dirs } = this . options
42
+
43
+ server . watcher . add ( dirs )
44
+ server . watcher
45
+ . on ( 'unlink' , ( path ) => {
46
+ if ( ! matchGlobs ( path , globs ) )
47
+ return
48
+ this . removeComponents ( path )
49
+ this . onUpdate ( path )
50
+ } )
51
+ server . watcher
52
+ . on ( 'add' , ( path ) => {
53
+ if ( ! matchGlobs ( path , globs ) )
54
+ return
55
+ this . addComponents ( path )
56
+ this . onUpdate ( path )
57
+ } )
60
58
}
61
59
62
60
/**
0 commit comments