feat: copy public dir

pull/5/head
Evan You 4 years ago
parent ea4b21b1d1
commit ddc9d519c6

@ -1,8 +1,10 @@
import path from 'path'
import { promises as fs } from 'fs'
import { bundle } from './bundle'
import { BuildOptions as ViteBuildOptions } from 'vite'
import { resolveConfig } from '../config'
import { renderPage } from './render'
import { exists, copyDir } from '../utils/fs'
export type BuildOptions = Pick<
ViteBuildOptions,
@ -18,6 +20,14 @@ export async function build(buildOptions: BuildOptions = {}) {
for (const page of siteConfig.pages) {
await renderPage(siteConfig, page, result)
}
if (await exists(siteConfig.publicDir)) {
console.log('copying public dir...')
await copyDir(
siteConfig.publicDir,
path.join(siteConfig.outDir, 'public')
)
}
} finally {
await fs.rmdir(siteConfig.tempDir, { recursive: true })
}

@ -1,10 +1,10 @@
import path from 'path'
import chalk from 'chalk'
import globby from 'globby'
import { promises as fs } from 'fs'
import { createResolver, APP_PATH } from './utils/pathResolver'
import { Resolver } from 'vite'
import { Header } from './markdown/plugins/header'
import { exists } from './utils/fs'
const debug = require('debug')('vitepress:config')
@ -26,6 +26,7 @@ export interface SiteConfig<ThemeConfig = any> {
site: SiteData<ThemeConfig>
configPath: string
themeDir: string
publicDir: string
outDir: string
tempDir: string
resolver: Resolver
@ -57,13 +58,9 @@ export async function resolveConfig(
// resolve theme path
const userThemeDir = resolve(root, 'theme')
let themeDir: string
try {
await fs.stat(userThemeDir)
themeDir = userThemeDir
} catch (e) {
themeDir = path.join(__dirname, '../lib/theme-default')
}
const themeDir = (await exists(userThemeDir))
? userThemeDir
: path.join(__dirname, '../lib/theme-default')
const config: SiteConfig = {
root,
@ -71,6 +68,7 @@ export async function resolveConfig(
themeDir,
pages: await globby(['**.md'], { cwd: root, ignore: ['node_modules'] }),
configPath: resolve(root, 'config.js'),
publicDir: resolve(root, 'public'),
outDir: resolve(root, 'dist'),
tempDir: path.resolve(APP_PATH, 'temp'),
resolver: createResolver(themeDir)
@ -82,12 +80,7 @@ export async function resolveConfig(
export async function resolveSiteData(root: string): Promise<SiteData> {
// load user config
const configPath = resolve(root, 'config.js')
let hasUserConfig = false
try {
await fs.stat(configPath)
hasUserConfig = true
} catch (e) {}
const hasUserConfig = await exists(configPath)
// always delete cache first before loading config
delete require.cache[configPath]
const userConfig: UserConfig = hasUserConfig ? require(configPath) : {}

@ -0,0 +1,29 @@
import path from 'path'
import { promises as fs } from 'fs'
export async function exists(path: string) {
try {
await fs.stat(path)
return true
} catch (e) {
return false
}
}
export async function copyDir(from: string, to: string) {
if (exists(to)) {
await fs.rmdir(to, { recursive: true })
}
await fs.mkdir(to, { recursive: true })
const content = await fs.readdir(from)
for (const entry of content) {
const fromPath = path.join(from, entry)
const toPath = path.join(to, entry)
const stat = await fs.stat(fromPath)
if (stat.isFile()) {
await fs.copyFile(fromPath, toPath)
} else if (stat.isDirectory()) {
await copyDir(fromPath, toPath)
}
}
}
Loading…
Cancel
Save