Skip to content

Commit a3ad908

Browse files
fix(cli): fix dev pages watcher (#1662)
Co-authored-by: Xinyu Liu <[email protected]>
1 parent 36a9895 commit a3ad908

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

packages/cli/src/commands/dev/watchPageFiles.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-misused-promises */
22
import type { App, Page } from '@vuepress/core'
3-
import { colors, logger, picomatch } from '@vuepress/utils'
3+
import { colors, logger, path, picomatch } from '@vuepress/utils'
44
import type { FSWatcher } from 'chokidar'
55
import chokidar from 'chokidar'
66
import { handlePageAdd } from './handlePageAdd.js'
@@ -42,18 +42,37 @@ export const watchPageFiles = (app: App): FSWatcher[] => {
4242
})
4343

4444
// watch page files
45-
const pagePatternsMatchers = app.options.pagePatterns.map((pattern) =>
46-
picomatch(pattern, {
47-
cwd: app.dir.source(),
48-
}),
49-
)
45+
const pagePatterns: string[] = []
46+
const ignorePatterns: string[] = []
47+
for (const pattern of app.options.pagePatterns) {
48+
if (pattern.startsWith('!')) {
49+
ignorePatterns.push(pattern.slice(1))
50+
} else {
51+
pagePatterns.push(pattern)
52+
}
53+
}
54+
const sourceDir = app.dir.source()
55+
const tempDir = app.dir.temp()
56+
const cacheDir = app.dir.cache()
57+
const isPageMatch = picomatch(pagePatterns, {
58+
ignore: ignorePatterns,
59+
cwd: sourceDir,
60+
})
5061
const pagesWatcher = chokidar.watch('.', {
51-
cwd: app.dir.source(),
62+
cwd: sourceDir,
5263
ignored: (filepath, stats) => {
53-
// do not ignore non-file paths
54-
if (!stats?.isFile()) return false
55-
// ignore if the file does not match all patterns
56-
return pagePatternsMatchers.some((matcher) => !matcher(filepath))
64+
// ignore node_modules
65+
if (filepath.includes('node_modules')) {
66+
return true
67+
}
68+
// ignore internal temp files
69+
if (filepath.startsWith(tempDir) || filepath.startsWith(cacheDir)) {
70+
return true
71+
}
72+
// ignore non-matched files
73+
return (
74+
!!stats?.isFile() && !isPageMatch(path.relative(sourceDir, filepath))
75+
)
5776
},
5877
ignoreInitial: true,
5978
})

0 commit comments

Comments
 (0)