11import { relative } from 'path'
22import Debug from 'debug'
3- import chokidar from 'chokidar'
43import { ResolvedConfig , UpdatePayload , ViteDevServer } from 'vite'
5- import { throttle } from '@antfu/utils'
4+ import { throttle , toArray , slash } from '@antfu/utils'
65import { 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'
87import { searchComponents } from './fs/glob'
98import { generateDeclaration } from './declaration'
109
@@ -29,25 +28,6 @@ export class Context {
2928 public readonly viteConfig : ResolvedConfig ,
3029 ) {
3130 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-
5131 this . generateDeclaration = throttle ( 500 , false , this . generateDeclaration . bind ( this ) )
5232 }
5333
@@ -57,6 +37,24 @@ export class Context {
5737
5838 setServer ( server : ViteDevServer ) {
5939 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+ } )
6058 }
6159
6260 /**
0 commit comments