fix: restart server on theme creation/deletion (#2785)

pull/2787/head
Divyansh Singh 11 months ago committed by GitHub
parent 1bda710702
commit e0be677554
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,6 +35,7 @@ declare module 'vite' {
}
}
const themeRE = /\/\.vitepress\/theme\/index\.(m|c)?(j|t)s$/
const hashRE = /\.(\w+)\.js$/
const staticInjectMarkerRE =
/\b(const _hoisted_\d+ = \/\*(?:#|@)__PURE__\*\/\s*createStaticVNode)\("(.*)", (\d+)\)/g
@ -225,8 +226,22 @@ export async function createVitePressPlugin(
configDeps.forEach((file) => server.watcher.add(file))
}
// update pages, dynamicRoutes and rewrites on md file add / deletion
const onFileAddDelete = async (file: string) => {
const onFileAddDelete = async (added: boolean, file: string) => {
// restart server on theme file creation / deletion
if (themeRE.test(slash(file))) {
siteConfig.logger.info(
c.green(
`${path.relative(process.cwd(), file)} ${
added ? 'created' : 'deleted'
}, restarting server...\n`
),
{ clear: true, timestamp: true }
)
await recreateServer?.()
}
// update pages, dynamicRoutes and rewrites on md file creation / deletion
if (file.endsWith('.md')) {
Object.assign(
siteConfig,
@ -234,7 +249,9 @@ export async function createVitePressPlugin(
)
}
}
server.watcher.on('add', onFileAddDelete).on('unlink', onFileAddDelete)
server.watcher
.on('add', onFileAddDelete.bind(null, true))
.on('unlink', onFileAddDelete.bind(null, false))
// serve our index.html after vite history fallback
return () => {

Loading…
Cancel
Save