Skip to content

Commit 9725a10

Browse files
committed
feat(core): add onWatched hook
1 parent 34914e4 commit 9725a10

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Temporarily record some breaking changes here.
4747

4848
- `shouldPrefetch` -> the default value is changed to `false`
4949
- `patterns` -> `pagePatterns`
50-
- `extraWatchFiles` -> removed
50+
- `extraWatchFiles` -> removed, watch files manually in `onWatched` hook
5151
- `evergreen` -> the default value is changed to `true`
5252
- `markdown`
5353
- `markdown.lineNumbers` -> `markdown.code.lineNumbers`, and the default value is changed to `true`
@@ -125,7 +125,7 @@ Theme authors can use their own way for users to configure styles (not be limite
125125
#### Plugin API
126126

127127
- `ready` -> `onPrepared`
128-
- `updated` -> removed
128+
- `updated` -> `onWatched`
129129
- `generated` -> `onGenerated`
130130
- `additionalPages` -> removed, use `app.pages.push(createPage())` in `onInitialized` hook
131131
- `clientDynamicModules` -> removed, use `app.writeTemp()` in `onPrepared` hook

packages/@vuepress/cli/src/commands/dev/dev.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as chokidar from 'chokidar'
2+
import type { FSWatcher } from 'chokidar'
23
import { createApp } from '@vuepress/core'
34
import { chalk, debug, fs, logger } from '@vuepress/utils'
45
import {
@@ -70,48 +71,53 @@ export const dev = async (
7071
return
7172
}
7273

74+
// all watchers
75+
const watchers: FSWatcher[] = []
76+
77+
// restart dev command
78+
const restart = async (): Promise<void> => {
79+
await Promise.all([
80+
// close all watchers
81+
...watchers.map((item) => item.close()),
82+
// close current dev server
83+
close(),
84+
])
85+
// restart dev command
86+
await dev(sourceDir, commandOptions)
87+
logger.tip(`dev server has restarted, please refresh your browser`)
88+
}
89+
7390
// watch page files
7491
const pagesWatcher = chokidar.watch(app.options.pagePatterns, {
7592
cwd: app.dir.source(),
7693
ignoreInitial: true,
7794
})
78-
79-
// handle page add event
8095
pagesWatcher.on('add', (filePathRelative) => {
8196
logger.info(`page ${chalk.magenta(filePathRelative)} is created`)
8297
handlePageAdd(app, app.dir.source(filePathRelative))
8398
})
84-
85-
// handle page change event
8699
pagesWatcher.on('change', (filePathRelative) => {
87100
logger.info(`page ${chalk.magenta(filePathRelative)} is modified`)
88101
handlePageChange(app, app.dir.source(filePathRelative))
89102
})
90-
91-
// handle page unlink event
92103
pagesWatcher.on('unlink', (filePathRelative) => {
93104
logger.info(`page ${chalk.magenta(filePathRelative)} is removed`)
94105
handlePageUnlink(app, app.dir.source(filePathRelative))
95106
})
107+
watchers.push(pagesWatcher)
96108

97109
// watch user config file
98110
if (userConfigPath) {
99111
const configWatcher = chokidar.watch(userConfigPath, {
100112
cwd: process.cwd(),
101113
ignoreInitial: true,
102114
})
103-
configWatcher.on('change', async (configFile) => {
115+
configWatcher.on('change', (configFile) => {
104116
logger.info(`config ${chalk.magenta(configFile)} is modified`)
105-
await Promise.all([
106-
// close file watchers
107-
pagesWatcher.close(),
108-
configWatcher.close(),
109-
// close current dev server
110-
close(),
111-
])
112-
// re-run dev command
113-
await dev(sourceDir, commandOptions)
114-
logger.tip(`dev server has restarted, please refresh your browser`)
117+
restart()
115118
})
119+
watchers.push(configWatcher)
116120
}
121+
122+
await app.pluginApi.hooks.onWatched.process(app, restart)
117123
}

packages/@vuepress/core/src/pluginApi/createPluginApiHooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const createPluginApiHooks = (): PluginApi['hooks'] => ({
55
// life cycle hooks
66
onInitialized: createHookQueue('onInitialized'),
77
onPrepared: createHookQueue('onPrepared'),
8+
onWatched: createHookQueue('onWatched'),
89
onGenerated: createHookQueue('onGenerated'),
910

1011
// page hooks

packages/@vuepress/core/src/types/plugin-api/hooks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export type Hook<
2121
}
2222

2323
// life-cycle hook
24-
export type LifeCycleHook = Hook<(app: App) => PromiseOrNot<void>>
24+
export type LifeCycleHook<T extends unknown[] = []> = Hook<
25+
(app: App, ...args: T) => PromiseOrNot<void>
26+
>
2527

2628
// hook that generates client files
2729
export type ClientFilesHook = Hook<
@@ -49,6 +51,7 @@ export type ExtendsPageDataHook = Hook<
4951
export interface Hooks {
5052
onInitialized: LifeCycleHook
5153
onPrepared: LifeCycleHook
54+
onWatched: LifeCycleHook<[restart: () => Promise<void>]>
5255
onGenerated: LifeCycleHook
5356
extendsMarkdown: ExtendsMarkdownHook
5457
extendsPageData: ExtendsPageDataHook

0 commit comments

Comments
 (0)