Skip to content

Commit ddc9d51

Browse files
committed
feat: copy public dir
1 parent ea4b21b commit ddc9d51

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/build/build.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import path from 'path'
12
import { promises as fs } from 'fs'
23
import { bundle } from './bundle'
34
import { BuildOptions as ViteBuildOptions } from 'vite'
45
import { resolveConfig } from '../config'
56
import { renderPage } from './render'
7+
import { exists, copyDir } from '../utils/fs'
68

79
export type BuildOptions = Pick<
810
ViteBuildOptions,
@@ -18,6 +20,14 @@ export async function build(buildOptions: BuildOptions = {}) {
1820
for (const page of siteConfig.pages) {
1921
await renderPage(siteConfig, page, result)
2022
}
23+
24+
if (await exists(siteConfig.publicDir)) {
25+
console.log('copying public dir...')
26+
await copyDir(
27+
siteConfig.publicDir,
28+
path.join(siteConfig.outDir, 'public')
29+
)
30+
}
2131
} finally {
2232
await fs.rmdir(siteConfig.tempDir, { recursive: true })
2333
}

src/config.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'path'
22
import chalk from 'chalk'
33
import globby from 'globby'
4-
import { promises as fs } from 'fs'
54
import { createResolver, APP_PATH } from './utils/pathResolver'
65
import { Resolver } from 'vite'
76
import { Header } from './markdown/plugins/header'
7+
import { exists } from './utils/fs'
88

99
const debug = require('debug')('vitepress:config')
1010

@@ -26,6 +26,7 @@ export interface SiteConfig<ThemeConfig = any> {
2626
site: SiteData<ThemeConfig>
2727
configPath: string
2828
themeDir: string
29+
publicDir: string
2930
outDir: string
3031
tempDir: string
3132
resolver: Resolver
@@ -57,20 +58,17 @@ export async function resolveConfig(
5758

5859
// resolve theme path
5960
const userThemeDir = resolve(root, 'theme')
60-
let themeDir: string
61-
try {
62-
await fs.stat(userThemeDir)
63-
themeDir = userThemeDir
64-
} catch (e) {
65-
themeDir = path.join(__dirname, '../lib/theme-default')
66-
}
61+
const themeDir = (await exists(userThemeDir))
62+
? userThemeDir
63+
: path.join(__dirname, '../lib/theme-default')
6764

6865
const config: SiteConfig = {
6966
root,
7067
site,
7168
themeDir,
7269
pages: await globby(['**.md'], { cwd: root, ignore: ['node_modules'] }),
7370
configPath: resolve(root, 'config.js'),
71+
publicDir: resolve(root, 'public'),
7472
outDir: resolve(root, 'dist'),
7573
tempDir: path.resolve(APP_PATH, 'temp'),
7674
resolver: createResolver(themeDir)
@@ -82,12 +80,7 @@ export async function resolveConfig(
8280
export async function resolveSiteData(root: string): Promise<SiteData> {
8381
// load user config
8482
const configPath = resolve(root, 'config.js')
85-
let hasUserConfig = false
86-
try {
87-
await fs.stat(configPath)
88-
hasUserConfig = true
89-
} catch (e) {}
90-
83+
const hasUserConfig = await exists(configPath)
9184
// always delete cache first before loading config
9285
delete require.cache[configPath]
9386
const userConfig: UserConfig = hasUserConfig ? require(configPath) : {}

src/utils/fs.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'path'
2+
import { promises as fs } from 'fs'
3+
4+
export async function exists(path: string) {
5+
try {
6+
await fs.stat(path)
7+
return true
8+
} catch (e) {
9+
return false
10+
}
11+
}
12+
13+
export async function copyDir(from: string, to: string) {
14+
if (exists(to)) {
15+
await fs.rmdir(to, { recursive: true })
16+
}
17+
await fs.mkdir(to, { recursive: true })
18+
const content = await fs.readdir(from)
19+
for (const entry of content) {
20+
const fromPath = path.join(from, entry)
21+
const toPath = path.join(to, entry)
22+
const stat = await fs.stat(fromPath)
23+
if (stat.isFile()) {
24+
await fs.copyFile(fromPath, toPath)
25+
} else if (stat.isDirectory()) {
26+
await copyDir(fromPath, toPath)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)