Skip to content

Commit 02cc268

Browse files
committed
feat($core): extra watching files
1 parent 8a234bb commit 02cc268

File tree

1 file changed

+29
-9
lines changed
  • packages/@vuepress/core/lib

1 file changed

+29
-9
lines changed

packages/@vuepress/core/lib/dev.js

+29-9
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ async function prepareServer (sourceDir, cliOptions = {}, context) {
2929

3030
// setup watchers to update options and dynamically generated files
3131
const update = (reason) => {
32-
logger.debug(`Re-prepare due to ${chalk.cyan(reason)}`)
32+
console.log(`Reload due to ${reason}`)
3333
ctx.pluginAPI.options.updated.syncApply()
3434
prepare(sourceDir, cliOptions, false /* isProd */).catch(err => {
3535
console.error(logger.error(chalk.red(err.stack), false))
3636
})
3737
}
3838

39+
// Curry update handler by update type
40+
const spawnUpdate = (updateType) =>
41+
file => update(`${chalk.red(updateType)} ${chalk.cyan(file)}`)
42+
3943
// watch add/remove of files
4044
const pagesWatcher = chokidar.watch([
4145
'**/*.md',
@@ -45,21 +49,29 @@ async function prepareServer (sourceDir, cliOptions = {}, context) {
4549
ignored: ['.vuepress/**/*.md', 'node_modules'],
4650
ignoreInitial: true
4751
})
48-
pagesWatcher.on('add', () => update('add page'))
49-
pagesWatcher.on('unlink', () => update('unlink page'))
50-
pagesWatcher.on('addDir', () => update('addDir'))
51-
pagesWatcher.on('unlinkDir', () => update('unlinkDir'))
52+
pagesWatcher.on('add', spawnUpdate('add'))
53+
pagesWatcher.on('unlink', spawnUpdate('unlink'))
54+
pagesWatcher.on('addDir', spawnUpdate('addDir'))
55+
pagesWatcher.on('unlinkDir', spawnUpdate('unlinkDir'))
5256

53-
// watch config file
54-
const configWatcher = chokidar.watch([
57+
const watchFiles = [
5558
'.vuepress/config.js',
5659
'.vuepress/config.yml',
5760
'.vuepress/config.toml'
58-
], {
61+
].concat(
62+
(
63+
ctx.siteConfig.extraWatchFiles || []
64+
).map(file => normalizeWatchFilePath(file, ctx.sourceDir))
65+
)
66+
67+
logger.debug('watchFiles', watchFiles)
68+
69+
// watch config file
70+
const configWatcher = chokidar.watch(watchFiles, {
5971
cwd: sourceDir,
6072
ignoreInitial: true
6173
})
62-
configWatcher.on('change', () => update('config change'))
74+
configWatcher.on('change', spawnUpdate('change'))
6375

6476
// also listen for frontmatter changes from markdown files
6577
frontmatterEmitter.on('update', () => update('frontmatter or headers change'))
@@ -177,3 +189,11 @@ async function resolvePort (port) {
177189
port = await portfinder.getPortPromise()
178190
return port
179191
}
192+
193+
function normalizeWatchFilePath (filepath, baseDir) {
194+
const { isAbsolute, relative } = require('path')
195+
if (isAbsolute(filepath)) {
196+
return relative(baseDir, filepath)
197+
}
198+
return filepath
199+
}

0 commit comments

Comments
 (0)