feat: copy public dir

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

@ -1,8 +1,10 @@
import path from 'path'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import { bundle } from './bundle' import { bundle } from './bundle'
import { BuildOptions as ViteBuildOptions } from 'vite' import { BuildOptions as ViteBuildOptions } from 'vite'
import { resolveConfig } from '../config' import { resolveConfig } from '../config'
import { renderPage } from './render' import { renderPage } from './render'
import { exists, copyDir } from '../utils/fs'
export type BuildOptions = Pick< export type BuildOptions = Pick<
ViteBuildOptions, ViteBuildOptions,
@ -18,6 +20,14 @@ export async function build(buildOptions: BuildOptions = {}) {
for (const page of siteConfig.pages) { for (const page of siteConfig.pages) {
await renderPage(siteConfig, page, result) 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 { } finally {
await fs.rmdir(siteConfig.tempDir, { recursive: true }) await fs.rmdir(siteConfig.tempDir, { recursive: true })
} }

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