refactor(build): extract render step from build

Move the rendering pages body into a standalone render() invoked
via bind, taking the bundle output whole. The SSR entry import now
fails under the rendering spinner, and the hashmap.json write runs
inside the task alongside the other rendering outputs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5342/head
Divyansh Singh 4 weeks ago
parent 9e79742861
commit 9071b81608

@ -56,20 +56,59 @@ export async function build(
const pageMetaMap = Object.create(null) as Record<string, PageMeta>
try {
const { clientResult, serverResult, pageToHashMap } = await bundle(
siteConfig,
buildOptions,
pageMetaMap
)
const out = await bundle(siteConfig, buildOptions, pageMetaMap)
if (process.env.BUNDLE_ONLY) {
return
}
await task('rendering pages', render.bind(null, siteConfig, out))
} finally {
await unlinkVue()
if (!process.env.DEBUG) {
await rm(siteConfig.tempDir, {
recursive: true,
force: true,
maxRetries: 10
})
}
}
await generateSitemap(siteConfig, pageMetaMap)
await siteConfig.buildEnd?.(siteConfig)
clearCache()
siteConfig.logger.info(
`build complete in ${((performance.now() - start) / 1000).toFixed(2)}s.`
)
}
async function linkVue() {
const root = await packageDirectory()
if (root) {
const dest = path.resolve(root, 'node_modules/vue')
// if user did not install vue by themselves, link VitePress' version
if (!fs.existsSync(dest)) {
const src = path.dirname(createRequire(import.meta.url).resolve('vue'))
await mkdir(path.dirname(dest), { recursive: true })
await symlink(src, dest, 'junction')
return () => unlink(dest)
}
}
return async () => {}
}
async function render(
siteConfig: SiteConfig,
{
clientResult,
serverResult,
pageToHashMap
}: Awaited<ReturnType<typeof bundle>>
): Promise<void> {
const entryPath = path.join(siteConfig.tempDir, 'app.js')
const { render } = await nativeImport(entryPath)
await task('rendering pages', async () => {
const clientOutput: (Rolldown.OutputChunk | Rolldown.OutputAsset)[] =
clientResult?.output || []
@ -106,10 +145,7 @@ export async function build(
// ----
const additionalHeadTags: HeadConfig[] = []
const metadataScript = await generateMetadataScript(
pageToHashMap,
siteConfig
)
const metadataScript = await generateMetadataScript(pageToHashMap, siteConfig)
if (isDefaultTheme) {
const fontURL = assets.find((file) =>
@ -161,7 +197,6 @@ export async function build(
}).replace(/[^]*?}\n*/, '')
await writeFile(path.join(siteConfig.outDir, 'vp-icons.css'), iconsCss)
})
// emit page hash map for the case where a user session is open
// when the site got redeployed (which invalidates current hash map)
@ -169,39 +204,6 @@ export async function build(
path.join(siteConfig.outDir, 'hashmap.json'),
JSON.stringify(pageToHashMap)
)
} finally {
await unlinkVue()
if (!process.env.DEBUG) {
await rm(siteConfig.tempDir, {
recursive: true,
force: true,
maxRetries: 10
})
}
}
await generateSitemap(siteConfig, pageMetaMap)
await siteConfig.buildEnd?.(siteConfig)
clearCache()
siteConfig.logger.info(
`build complete in ${((performance.now() - start) / 1000).toFixed(2)}s.`
)
}
async function linkVue() {
const root = await packageDirectory()
if (root) {
const dest = path.resolve(root, 'node_modules/vue')
// if user did not install vue by themselves, link VitePress' version
if (!fs.existsSync(dest)) {
const src = path.dirname(createRequire(import.meta.url).resolve('vue'))
await mkdir(path.dirname(dest), { recursive: true })
await symlink(src, dest, 'junction')
return () => unlink(dest)
}
}
return async () => {}
}
async function generateMetadataScript(

Loading…
Cancel
Save