Skip to content

Commit fa3b12e

Browse files
committed
fix(build): resolve user provided index.html, close #51
1 parent 9415fcb commit fa3b12e

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

demo/composable-vue/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<head>
2+
<link rel="icon" type="image/svg" href="https://antfu.me/favicon.svg" />
3+
</head>

packages/slidev/node/build.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { promises as fs } from 'fs'
21
import { resolve, join } from 'path'
32
import http from 'http'
3+
import fs from 'fs-extra'
44
import { build as viteBuild, InlineConfig, mergeConfig, ResolvedConfig } from 'vite'
55
import connect from 'connect'
66
import sirv from 'sirv'
@@ -14,6 +14,11 @@ export async function build(
1414
viteConfig: InlineConfig = {},
1515
) {
1616
const indexPath = resolve(options.userRoot, 'index.html')
17+
18+
let originalIndexHTML: string | undefined
19+
if (fs.existsSync(indexPath))
20+
originalIndexHTML = await fs.readFile(indexPath, 'utf-8')
21+
1722
await fs.writeFile(indexPath, await getIndexHtml(options), 'utf-8')
1823
let config: ResolvedConfig = undefined!
1924

@@ -36,7 +41,10 @@ export async function build(
3641
)
3742
}
3843
finally {
39-
await fs.unlink(indexPath)
44+
if (originalIndexHTML != null)
45+
await fs.writeFile(indexPath, originalIndexHTML, 'utf-8')
46+
else
47+
await fs.unlink(indexPath)
4048
}
4149

4250
if (options.data.config.download === true || options.data.config.download === 'auto') {

packages/slidev/node/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { uniq } from '@antfu/utils'
44
import { ResolvedSlidevOptions } from './options'
55
import { toAtFS } from './utils'
66

7-
export async function getIndexHtml({ clientRoot, themeRoots, data }: ResolvedSlidevOptions): Promise<string> {
7+
export async function getIndexHtml({ clientRoot, themeRoots, data, userRoot }: ResolvedSlidevOptions): Promise<string> {
88
let main = await fs.readFile(join(clientRoot, 'index.html'), 'utf-8')
99
let head = ''
1010
let body = ''
1111

1212
const roots = uniq([
1313
...themeRoots,
14-
// userRoot,
14+
userRoot,
1515
])
1616

1717
for (const root of roots) {

0 commit comments

Comments
 (0)