@@ -29,13 +29,17 @@ async function prepareServer (sourceDir, cliOptions = {}, context) {
29
29
30
30
// setup watchers to update options and dynamically generated files
31
31
const update = ( reason ) => {
32
- logger . debug ( `Re-prepare due to ${ chalk . cyan ( reason ) } `)
32
+ console . log ( `Reload due to ${ reason } `)
33
33
ctx . pluginAPI . options . updated . syncApply ( )
34
34
prepare ( sourceDir , cliOptions , false /* isProd */ ) . catch ( err => {
35
35
console . error ( logger . error ( chalk . red ( err . stack ) , false ) )
36
36
} )
37
37
}
38
38
39
+ // Curry update handler by update type
40
+ const spawnUpdate = ( updateType ) =>
41
+ file => update ( `${ chalk . red ( updateType ) } ${ chalk . cyan ( file ) } ` )
42
+
39
43
// watch add/remove of files
40
44
const pagesWatcher = chokidar . watch ( [
41
45
'**/*.md' ,
@@ -45,21 +49,29 @@ async function prepareServer (sourceDir, cliOptions = {}, context) {
45
49
ignored : [ '.vuepress/**/*.md' , 'node_modules' ] ,
46
50
ignoreInitial : true
47
51
} )
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' ) )
52
56
53
- // watch config file
54
- const configWatcher = chokidar . watch ( [
57
+ const watchFiles = [
55
58
'.vuepress/config.js' ,
56
59
'.vuepress/config.yml' ,
57
60
'.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 , {
59
71
cwd : sourceDir ,
60
72
ignoreInitial : true
61
73
} )
62
- configWatcher . on ( 'change' , ( ) => update ( 'config change') )
74
+ configWatcher . on ( 'change' , spawnUpdate ( ' change') )
63
75
64
76
// also listen for frontmatter changes from markdown files
65
77
frontmatterEmitter . on ( 'update' , ( ) => update ( 'frontmatter or headers change' ) )
@@ -177,3 +189,11 @@ async function resolvePort (port) {
177
189
port = await portfinder . getPortPromise ( )
178
190
return port
179
191
}
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