Skip to content

Commit a4b4cb4

Browse files
committed
fix: auto restart when certain files have changed
1 parent 90a0dc4 commit a4b4cb4

File tree

3 files changed

+120
-48
lines changed

3 files changed

+120
-48
lines changed

packages/slidev/node/cli.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ const CONFIG_RESTART_FIELDS: (keyof SlidevConfig)[] = [
3838
'theme',
3939
]
4040

41+
/**
42+
* Files that triggers a restart when added or removed
43+
*/
44+
const FILES_CREATE_RESTART_GLOBS = [
45+
'global-bottom.vue',
46+
'global-top.vue',
47+
'uno.config.js',
48+
'uno.config.ts',
49+
'unocss.config.js',
50+
'unocss.config.ts',
51+
]
52+
53+
const FILES_CHANGE_RESTART_GLOBS = [
54+
'vite.config.*',
55+
'setup/shiki.ts',
56+
'setup/katex.ts',
57+
'setup/preparser.ts',
58+
]
59+
4160
injectPreparserExtensionLoader(async (headmatter?: Record<string, unknown>, filepath?: string, mode?: string) => {
4261
const addons = headmatter?.addons as string[] ?? []
4362
const { clientRoot, userRoot } = await getRoots()
@@ -113,6 +132,15 @@ cli.command(
113132

114133
let lastRemoteUrl: string | undefined
115134

135+
let restartTimer: ReturnType<typeof setTimeout> | undefined
136+
function restartServer() {
137+
clearTimeout(restartTimer!)
138+
restartTimer = setTimeout(() => {
139+
console.log(yellow('\n restarting...\n'))
140+
initServer()
141+
}, 500)
142+
}
143+
116144
async function initServer() {
117145
if (server)
118146
await server.close()
@@ -149,7 +177,7 @@ cli.command(
149177
const themeRaw = theme || loaded.headmatter.theme as string || 'default'
150178
if (options.themeRaw !== themeRaw) {
151179
console.log(yellow('\n restarting on theme change\n'))
152-
initServer()
180+
restartServer()
153181
return false
154182
}
155183
// Because themeRaw is not changed, we don't resolve it again
@@ -162,7 +190,7 @@ cli.command(
162190

163191
if (CONFIG_RESTART_FIELDS.some(i => !equal(newData.config[i], oldData.config[i]))) {
164192
console.log(yellow('\n restarting on config change\n'))
165-
initServer()
193+
restartServer()
166194
return false
167195
}
168196
return newData
@@ -276,6 +304,30 @@ cli.command(
276304

277305
initServer()
278306
bindShortcut()
307+
308+
// Start watcher to restart server on file changes
309+
const { watch } = await import('chokidar')
310+
const watcher = watch([
311+
...FILES_CREATE_RESTART_GLOBS,
312+
...FILES_CHANGE_RESTART_GLOBS,
313+
], {
314+
ignored: ['node_modules', '.git'],
315+
ignoreInitial: true,
316+
})
317+
watcher.on('unlink', (file) => {
318+
console.log(yellow(`\n file ${file} removed, restarting...\n`))
319+
restartServer()
320+
})
321+
watcher.on('add', (file) => {
322+
console.log(yellow(`\n file ${file} added, restarting...\n`))
323+
restartServer()
324+
})
325+
watcher.on('change', (file) => {
326+
if (FILES_CREATE_RESTART_GLOBS.includes(file))
327+
return
328+
console.log(yellow(`\n file ${file} changed, restarting...\n`))
329+
restartServer()
330+
})
279331
},
280332
)
281333

packages/slidev/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@unocss/reset": "^0.58.5",
6565
"@vitejs/plugin-vue": "^5.0.4",
6666
"@vitejs/plugin-vue-jsx": "^3.1.0",
67+
"chokidar": "^3.6.0",
6768
"cli-progress": "^3.12.0",
6869
"codemirror": "^5.65.16",
6970
"connect": "^3.7.0",

0 commit comments

Comments
 (0)