mirror of https://github.com/vuejs/vitepress
parent
b94a0865b0
commit
a512fc7925
@ -1,11 +1,22 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import { bundle } from './bundle'
|
||||
import { BuildOptions as ViteBuildOptions } from 'vite'
|
||||
import { resolveConfig } from '../config'
|
||||
import { renderPage } from './render'
|
||||
|
||||
export type BuildOptions = Pick<
|
||||
ViteBuildOptions,
|
||||
'root' | 'rollupInputOptions' | 'rollupOutputOptions'
|
||||
>
|
||||
|
||||
export async function build(options: BuildOptions = {}) {
|
||||
await bundle(options)
|
||||
export async function build(buildOptions: BuildOptions = {}) {
|
||||
const siteConfig = await resolveConfig(buildOptions.root)
|
||||
try {
|
||||
const result = await bundle(siteConfig, buildOptions)
|
||||
for (const page of siteConfig.pages) {
|
||||
await renderPage(siteConfig, page, result)
|
||||
}
|
||||
} finally {
|
||||
await fs.rmdir(siteConfig.tempDir, { recursive: true })
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
import path from 'path'
|
||||
import { SiteConfig } from '../config'
|
||||
import { BuildResult } from 'vite'
|
||||
import { renderToString } from '@vue/server-renderer'
|
||||
|
||||
export async function renderPage(
|
||||
config: SiteConfig,
|
||||
page: string, // foo.md
|
||||
result: BuildResult[]
|
||||
) {
|
||||
const { createApp } = require(path.join(config.tempDir, 'js/index.js'))
|
||||
const { app, router } = createApp()
|
||||
const routePath = `/${page.replace(/\.md$/, '')}`
|
||||
router.go(routePath)
|
||||
const html = await renderToString(app)
|
||||
console.log(html)
|
||||
}
|
Loading…
Reference in new issue