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

pull/2787/head
Divyansh Singh 1 year 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 hashRE = /\.(\w+)\.js$/
const staticInjectMarkerRE = const staticInjectMarkerRE =
/\b(const _hoisted_\d+ = \/\*(?:#|@)__PURE__\*\/\s*createStaticVNode)\("(.*)", (\d+)\)/g /\b(const _hoisted_\d+ = \/\*(?:#|@)__PURE__\*\/\s*createStaticVNode)\("(.*)", (\d+)\)/g
@ -225,8 +226,22 @@ export async function createVitePressPlugin(
configDeps.forEach((file) => server.watcher.add(file)) configDeps.forEach((file) => server.watcher.add(file))
} }
// update pages, dynamicRoutes and rewrites on md file add / deletion const onFileAddDelete = async (added: boolean, file: string) => {
const onFileAddDelete = async (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')) { if (file.endsWith('.md')) {
Object.assign( Object.assign(
siteConfig, 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 // serve our index.html after vite history fallback
return () => { return () => {

Loading…
Cancel
Save