|
1 | 1 | /* eslint-disable @typescript-eslint/no-misused-promises */ |
2 | 2 | import type { App, Page } from '@vuepress/core' |
3 | | -import { colors, logger, picomatch } from '@vuepress/utils' |
| 3 | +import { colors, logger, path, picomatch } from '@vuepress/utils' |
4 | 4 | import type { FSWatcher } from 'chokidar' |
5 | 5 | import chokidar from 'chokidar' |
6 | 6 | import { handlePageAdd } from './handlePageAdd.js' |
@@ -42,18 +42,29 @@ export const watchPageFiles = (app: App): FSWatcher[] => { |
42 | 42 | }) |
43 | 43 |
|
44 | 44 | // 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 | + }) |
50 | 59 | const pagesWatcher = chokidar.watch('.', { |
51 | | - cwd: app.dir.source(), |
| 60 | + cwd: sourceDir, |
52 | 61 | 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 | + ) |
57 | 68 | }, |
58 | 69 | ignoreInitial: true, |
59 | 70 | }) |
|
0 commit comments