Skip to content

Commit b6632c0

Browse files
committed
fix: reuse vite watcher, close #84
1 parent fced066 commit b6632c0

File tree

7 files changed

+38
-31
lines changed

7 files changed

+38
-31
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"vite": "^2.0.0"
3232
},
3333
"dependencies": {
34-
"chokidar": "^3.5.2",
3534
"debug": "^4.3.2",
3635
"fast-glob": "^3.2.6",
3736
"magic-string": "^0.25.7",

pnpm-lock.yaml

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

src/context.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { relative } from 'path'
22
import Debug from 'debug'
3-
import chokidar from 'chokidar'
43
import { ResolvedConfig, UpdatePayload, ViteDevServer } from 'vite'
5-
import { throttle } from '@antfu/utils'
4+
import { throttle, toArray, slash } from '@antfu/utils'
65
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'
87
import { searchComponents } from './fs/glob'
98
import { 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
/**

src/declaration.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { resolve, dirname, relative } from 'path'
22
import { promises as fs, existsSync } from 'fs'
3-
import { notNullish } from '@antfu/utils'
3+
import { notNullish, slash } from '@antfu/utils'
44
import { Context } from './context'
5-
import { slash } from './utils'
65

76
export function parseDeclaration(code: string): Record<string, string> {
87
return Object.fromEntries(Array.from(code.matchAll(/\s+['"]?(.+?)['"]?:\s(.+?)\n/g)).map(i => [i[1], i[2]]))

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ function VitePluginComponents(options: Options = {}): Plugin {
1212
return {
1313
name: 'vite-plugin-components',
1414
enforce: 'post',
15+
config() {
16+
return {
17+
server: {
18+
watch: {
19+
disableGlobbing: false,
20+
},
21+
},
22+
}
23+
},
1524
configResolved(config) {
1625
if (config.plugins.find(i => i.name === 'vite-plugin-vue2'))
1726
options.transformer = options.transformer || 'vue2'

src/resolvers/antdv.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ const matchComponents: IMatcher[] = [
120120
pattern: /^Tab/,
121121
styleDir: 'tabs',
122122
},
123-
124123
{
125124
pattern: /^Mentions/,
126125
styleDir: 'mentions',

src/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { LibraryResolver } from './helpers/libraryResolver'
77
import { defaultOptions } from './constants'
88
import { Context } from './context'
99

10-
export { slash, toArray }
11-
1210
export interface ResolveComponent {
1311
filename: string
1412
namespace?: string
@@ -104,7 +102,7 @@ export function resolveOptions(options: Options, viteConfig: ResolvedConfig): Re
104102
resolved.dirs = toArray(resolved.dirs)
105103
resolved.resolvedDirs = resolved.dirs.map(i => slash(resolve(viteConfig.root, i)))
106104

107-
resolved.globs = resolved.dirs.map(i =>
105+
resolved.globs = resolved.resolvedDirs.map(i =>
108106
resolved.deep
109107
? slash(join(i, `**/*.${extsGlob}`))
110108
: slash(join(i, `*.${extsGlob}`)),

0 commit comments

Comments
 (0)