Skip to content

Commit e0be677

Browse files
authoredAug 13, 2023
fix: restart server on theme creation/deletion (#2785)
1 parent 1bda710 commit e0be677

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed
 

‎src/node/plugin.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ declare module 'vite' {
3535
}
3636
}
3737

38+
const themeRE = /\/\.vitepress\/theme\/index\.(m|c)?(j|t)s$/
3839
const hashRE = /\.(\w+)\.js$/
3940
const staticInjectMarkerRE =
4041
/\b(const _hoisted_\d+ = \/\*(?:#|@)__PURE__\*\/\s*createStaticVNode)\("(.*)", (\d+)\)/g
@@ -225,16 +226,32 @@ export async function createVitePressPlugin(
225226
configDeps.forEach((file) => server.watcher.add(file))
226227
}
227228

228-
// update pages, dynamicRoutes and rewrites on md file add / deletion
229-
const onFileAddDelete = async (file: string) => {
229+
const onFileAddDelete = async (added: boolean, file: string) => {
230+
// restart server on theme file creation / deletion
231+
if (themeRE.test(slash(file))) {
232+
siteConfig.logger.info(
233+
c.green(
234+
`${path.relative(process.cwd(), file)} ${
235+
added ? 'created' : 'deleted'
236+
}, restarting server...\n`
237+
),
238+
{ clear: true, timestamp: true }
239+
)
240+
241+
await recreateServer?.()
242+
}
243+
244+
// update pages, dynamicRoutes and rewrites on md file creation / deletion
230245
if (file.endsWith('.md')) {
231246
Object.assign(
232247
siteConfig,
233248
await resolvePages(siteConfig.srcDir, siteConfig.userConfig)
234249
)
235250
}
236251
}
237-
server.watcher.on('add', onFileAddDelete).on('unlink', onFileAddDelete)
252+
server.watcher
253+
.on('add', onFileAddDelete.bind(null, true))
254+
.on('unlink', onFileAddDelete.bind(null, false))
238255

239256
// serve our index.html after vite history fallback
240257
return () => {

0 commit comments

Comments
 (0)
Please sign in to comment.