Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions packages/cli/src/commands/dev/watchPageFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import type { App, Page } from '@vuepress/core'
import { colors, logger, picomatch } from '@vuepress/utils'
import { colors, logger, path, picomatch } from '@vuepress/utils'
import type { FSWatcher } from 'chokidar'
import chokidar from 'chokidar'
import { handlePageAdd } from './handlePageAdd.js'
Expand Down Expand Up @@ -42,18 +42,37 @@ export const watchPageFiles = (app: App): FSWatcher[] => {
})

// watch page files
const pagePatternsMatchers = app.options.pagePatterns.map((pattern) =>
picomatch(pattern, {
cwd: app.dir.source(),
}),
)
const pagePatterns: string[] = []
const ignorePatterns: string[] = []
for (const pattern of app.options.pagePatterns) {
if (pattern.startsWith('!')) {
ignorePatterns.push(pattern.slice(1))
} else {
pagePatterns.push(pattern)
}
}
const sourceDir = app.dir.source()
const tempDir = app.dir.temp()
const cacheDir = app.dir.cache()
const isPageMatch = picomatch(pagePatterns, {
ignore: ignorePatterns,
cwd: sourceDir,
})
const pagesWatcher = chokidar.watch('.', {
cwd: app.dir.source(),
cwd: sourceDir,
ignored: (filepath, stats) => {
// do not ignore non-file paths
if (!stats?.isFile()) return false
// ignore if the file does not match all patterns
return pagePatternsMatchers.some((matcher) => !matcher(filepath))
// ignore node_modules
if (filepath.includes('node_modules')) {
return true
}
// ignore internal temp files
if (filepath.startsWith(tempDir) || filepath.startsWith(cacheDir)) {
return true
}
// ignore non-matched files
return (
!!stats?.isFile() && !isPageMatch(path.relative(sourceDir, filepath))
)
},
ignoreInitial: true,
})
Expand Down
Loading