From e0be677554a517e8b02fcaf930828bb052d1c4a4 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sun, 13 Aug 2023 14:20:50 +0530 Subject: [PATCH] fix: restart server on theme creation/deletion (#2785) --- src/node/plugin.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 29f38513..c5d5f364 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -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 () => {