Skip to content

Commit

Permalink
fix: restart server on theme creation/deletion (#2785)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Aug 13, 2023
1 parent 1bda710 commit e0be677
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/node/plugin.ts
Expand Up @@ -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
Expand Down Expand Up @@ -225,16 +226,32 @@ 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,
await resolvePages(siteConfig.srcDir, siteConfig.userConfig)
)
}
}
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 () => {
Expand Down

0 comments on commit e0be677

Please sign in to comment.