Skip to content

Commit d0bf722

Browse files
committed
fix(cli): fix file watching in watchPageFiles
1 parent 34638c5 commit d0bf722

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

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

Lines changed: 22 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,29 @@ 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 isPageMatch = picomatch(pagePatterns, {
56+
ignore: ignorePatterns,
57+
cwd: sourceDir,
58+
})
5059
const pagesWatcher = chokidar.watch('.', {
51-
cwd: app.dir.source(),
60+
cwd: sourceDir,
5261
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))
62+
if (filepath.includes('node_modules') || filepath.includes('.vuepress'))
63+
return true
64+
return (
65+
// ignore non-file and non-matched files
66+
!!stats?.isFile() && !isPageMatch(path.relative(sourceDir, filepath))
67+
)
5768
},
5869
ignoreInitial: true,
5970
})

0 commit comments

Comments
 (0)