Skip to content

Commit 93fe820

Browse files
authoredAug 7, 2022
fix(build): recreate server on config change (#1132)
1 parent 70da5f2 commit 93fe820

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed
 

‎src/node/build/bundle.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function bundle(
2121
serverResult: RollupOutput
2222
pageToHashMap: Record<string, string>
2323
}> {
24-
const { root, srcDir } = config
2524
const pageToHashMap = Object.create(null)
2625
const clientJSMap = Object.create(null)
2726

@@ -35,18 +34,17 @@ export async function bundle(
3534
config.pages.forEach((file) => {
3635
// page filename conversion
3736
// foo/bar.md -> foo_bar.md
38-
input[slash(file).replace(/\//g, '_')] = path.resolve(srcDir, file)
37+
input[slash(file).replace(/\//g, '_')] = path.resolve(config.srcDir, file)
3938
})
4039

4140
// resolve options to pass to vite
4241
const { rollupOptions } = options
4342

4443
const resolveViteConfig = async (ssr: boolean): Promise<ViteUserConfig> => ({
45-
root: srcDir,
44+
root: config.srcDir,
4645
base: config.site.base,
4746
logLevel: 'warn',
4847
plugins: await createVitePressPlugin(
49-
root,
5048
config,
5149
ssr,
5250
pageToHashMap,

‎src/node/cli.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ if (root) {
1414
}
1515

1616
if (!command || command === 'dev') {
17-
createServer(root, argv)
18-
.then((server) => server.listen())
19-
.then((server) => {
20-
console.log()
21-
server.printUrls()
22-
})
23-
.catch((err) => {
24-
console.error(c.red(`failed to start server. error:\n`), err)
25-
process.exit(1)
17+
const createDevServer = async () => {
18+
const server = await createServer(root, argv, async () => {
19+
await server.close()
20+
await createDevServer()
2621
})
22+
await server.listen()
23+
console.log()
24+
server.printUrls()
25+
}
26+
createDevServer().catch((err) => {
27+
console.error(c.red(`failed to start server. error:\n`), err)
28+
process.exit(1)
29+
})
2730
} else if (command === 'build') {
2831
build(root, argv).catch((err) => {
2932
console.error(c.red(`build error:\n`), err)

‎src/node/markdownToVue.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export interface MarkdownCompileResult {
2121
includes: string[]
2222
}
2323

24+
export function clearCache() {
25+
cache.reset()
26+
}
27+
2428
export async function createMarkdownToVueRenderFn(
2529
srcDir: string,
2630
options: MarkdownOptions = {},
@@ -31,9 +35,7 @@ export async function createMarkdownToVueRenderFn(
3135
includeLastUpdatedData = false
3236
) {
3337
const md = await createMarkdownRenderer(srcDir, options, base)
34-
3538
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))
36-
3739
const replaceRegex = genReplaceRegexp(userDefines, isBuild)
3840

3941
return async (

‎src/node/plugin.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path'
2+
import c from 'picocolors'
23
import { defineConfig, mergeConfig, Plugin, ResolvedConfig } from 'vite'
3-
import { SiteConfig, resolveSiteData } from './config'
4-
import { createMarkdownToVueRenderFn } from './markdownToVue'
4+
import { SiteConfig } from './config'
5+
import { createMarkdownToVueRenderFn, clearCache } from './markdownToVue'
56
import { DIST_CLIENT_PATH, APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
67
import { slash } from './utils/slash'
78
import { OutputAsset, OutputChunk } from 'rollup'
@@ -30,11 +31,11 @@ const isPageChunk = (
3031
)
3132

3233
export async function createVitePressPlugin(
33-
root: string,
3434
siteConfig: SiteConfig,
3535
ssr = false,
3636
pageToHashMap?: Record<string, string>,
37-
clientJSMap?: Record<string, string>
37+
clientJSMap?: Record<string, string>,
38+
recreateServer?: () => Promise<void>
3839
) {
3940
const {
4041
srcDir,
@@ -244,17 +245,23 @@ export async function createVitePressPlugin(
244245
},
245246

246247
async handleHotUpdate(ctx) {
247-
// handle config hmr
248248
const { file, read, server } = ctx
249249
if (file === configPath || configDeps.includes(file)) {
250-
const newData = await resolveSiteData(root)
251-
if (newData.base !== siteData.base) {
252-
console.warn(
253-
`[vitepress]: config.base has changed. Please restart the dev server.`
250+
console.log(
251+
c.green(
252+
`\n${path.relative(
253+
process.cwd(),
254+
file
255+
)} changed, restarting server...`
254256
)
257+
)
258+
try {
259+
clearCache()
260+
await recreateServer!()
261+
} catch (err) {
262+
console.error(c.red(`failed to restart server. error:\n`), err)
255263
}
256-
siteData = newData
257-
return [server.moduleGraph.getModuleById(SITE_DATA_REQUEST_PATH)!]
264+
return
258265
}
259266

260267
// hot reload .md files as .vue files

‎src/node/server.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { createVitePressPlugin } from './plugin'
44

55
export async function createServer(
66
root: string = process.cwd(),
7-
serverOptions: ServerOptions = {}
7+
serverOptions: ServerOptions = {},
8+
recreateServer: () => Promise<void>
89
) {
910
const config = await resolveConfig(root)
1011

@@ -17,7 +18,7 @@ export async function createServer(
1718
root: config.srcDir,
1819
base: config.site.base,
1920
// logLevel: 'warn',
20-
plugins: await createVitePressPlugin(root, config),
21+
plugins: await createVitePressPlugin(config, false, {}, {}, recreateServer),
2122
server: serverOptions
2223
})
2324
}

0 commit comments

Comments
 (0)
Please sign in to comment.