fix(build): recreate server on config change (#1132)

pull/1152/head
Divyansh Singh 2 years ago committed by GitHub
parent 70da5f275b
commit 93fe8207e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,7 +21,6 @@ export async function bundle(
serverResult: RollupOutput serverResult: RollupOutput
pageToHashMap: Record<string, string> pageToHashMap: Record<string, string>
}> { }> {
const { root, srcDir } = config
const pageToHashMap = Object.create(null) const pageToHashMap = Object.create(null)
const clientJSMap = Object.create(null) const clientJSMap = Object.create(null)
@ -35,18 +34,17 @@ export async function bundle(
config.pages.forEach((file) => { config.pages.forEach((file) => {
// page filename conversion // page filename conversion
// foo/bar.md -> foo_bar.md // foo/bar.md -> foo_bar.md
input[slash(file).replace(/\//g, '_')] = path.resolve(srcDir, file) input[slash(file).replace(/\//g, '_')] = path.resolve(config.srcDir, file)
}) })
// resolve options to pass to vite // resolve options to pass to vite
const { rollupOptions } = options const { rollupOptions } = options
const resolveViteConfig = async (ssr: boolean): Promise<ViteUserConfig> => ({ const resolveViteConfig = async (ssr: boolean): Promise<ViteUserConfig> => ({
root: srcDir, root: config.srcDir,
base: config.site.base, base: config.site.base,
logLevel: 'warn', logLevel: 'warn',
plugins: await createVitePressPlugin( plugins: await createVitePressPlugin(
root,
config, config,
ssr, ssr,
pageToHashMap, pageToHashMap,

@ -14,16 +14,19 @@ if (root) {
} }
if (!command || command === 'dev') { if (!command || command === 'dev') {
createServer(root, argv) const createDevServer = async () => {
.then((server) => server.listen()) const server = await createServer(root, argv, async () => {
.then((server) => { await server.close()
console.log() await createDevServer()
server.printUrls()
})
.catch((err) => {
console.error(c.red(`failed to start server. error:\n`), err)
process.exit(1)
}) })
await server.listen()
console.log()
server.printUrls()
}
createDevServer().catch((err) => {
console.error(c.red(`failed to start server. error:\n`), err)
process.exit(1)
})
} else if (command === 'build') { } else if (command === 'build') {
build(root, argv).catch((err) => { build(root, argv).catch((err) => {
console.error(c.red(`build error:\n`), err) console.error(c.red(`build error:\n`), err)

@ -21,6 +21,10 @@ export interface MarkdownCompileResult {
includes: string[] includes: string[]
} }
export function clearCache() {
cache.reset()
}
export async function createMarkdownToVueRenderFn( export async function createMarkdownToVueRenderFn(
srcDir: string, srcDir: string,
options: MarkdownOptions = {}, options: MarkdownOptions = {},
@ -31,9 +35,7 @@ export async function createMarkdownToVueRenderFn(
includeLastUpdatedData = false includeLastUpdatedData = false
) { ) {
const md = await createMarkdownRenderer(srcDir, options, base) const md = await createMarkdownRenderer(srcDir, options, base)
pages = pages.map((p) => slash(p.replace(/\.md$/, ''))) pages = pages.map((p) => slash(p.replace(/\.md$/, '')))
const replaceRegex = genReplaceRegexp(userDefines, isBuild) const replaceRegex = genReplaceRegexp(userDefines, isBuild)
return async ( return async (

@ -1,7 +1,8 @@
import path from 'path' import path from 'path'
import c from 'picocolors'
import { defineConfig, mergeConfig, Plugin, ResolvedConfig } from 'vite' import { defineConfig, mergeConfig, Plugin, ResolvedConfig } from 'vite'
import { SiteConfig, resolveSiteData } from './config' import { SiteConfig } from './config'
import { createMarkdownToVueRenderFn } from './markdownToVue' import { createMarkdownToVueRenderFn, clearCache } from './markdownToVue'
import { DIST_CLIENT_PATH, APP_PATH, SITE_DATA_REQUEST_PATH } from './alias' import { DIST_CLIENT_PATH, APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
import { slash } from './utils/slash' import { slash } from './utils/slash'
import { OutputAsset, OutputChunk } from 'rollup' import { OutputAsset, OutputChunk } from 'rollup'
@ -30,11 +31,11 @@ const isPageChunk = (
) )
export async function createVitePressPlugin( export async function createVitePressPlugin(
root: string,
siteConfig: SiteConfig, siteConfig: SiteConfig,
ssr = false, ssr = false,
pageToHashMap?: Record<string, string>, pageToHashMap?: Record<string, string>,
clientJSMap?: Record<string, string> clientJSMap?: Record<string, string>,
recreateServer?: () => Promise<void>
) { ) {
const { const {
srcDir, srcDir,
@ -244,17 +245,23 @@ export async function createVitePressPlugin(
}, },
async handleHotUpdate(ctx) { async handleHotUpdate(ctx) {
// handle config hmr
const { file, read, server } = ctx const { file, read, server } = ctx
if (file === configPath || configDeps.includes(file)) { if (file === configPath || configDeps.includes(file)) {
const newData = await resolveSiteData(root) console.log(
if (newData.base !== siteData.base) { c.green(
console.warn( `\n${path.relative(
`[vitepress]: config.base has changed. Please restart the dev server.` process.cwd(),
file
)} changed, restarting server...`
) )
)
try {
clearCache()
await recreateServer!()
} catch (err) {
console.error(c.red(`failed to restart server. error:\n`), err)
} }
siteData = newData return
return [server.moduleGraph.getModuleById(SITE_DATA_REQUEST_PATH)!]
} }
// hot reload .md files as .vue files // hot reload .md files as .vue files

@ -4,7 +4,8 @@ import { createVitePressPlugin } from './plugin'
export async function createServer( export async function createServer(
root: string = process.cwd(), root: string = process.cwd(),
serverOptions: ServerOptions = {} serverOptions: ServerOptions = {},
recreateServer: () => Promise<void>
) { ) {
const config = await resolveConfig(root) const config = await resolveConfig(root)
@ -17,7 +18,7 @@ export async function createServer(
root: config.srcDir, root: config.srcDir,
base: config.site.base, base: config.site.base,
// logLevel: 'warn', // logLevel: 'warn',
plugins: await createVitePressPlugin(root, config), plugins: await createVitePressPlugin(config, false, {}, {}, recreateServer),
server: serverOptions server: serverOptions
}) })
} }

Loading…
Cancel
Save