Skip to content

Commit

Permalink
fix(build): resolve user provided index.html, close #51
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 10, 2021
1 parent 9415fcb commit fa3b12e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions demo/composable-vue/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<link rel="icon" type="image/svg" href="https://antfu.me/favicon.svg" />
</head>
12 changes: 10 additions & 2 deletions packages/slidev/node/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs } from 'fs'
import { resolve, join } from 'path'
import http from 'http'
import fs from 'fs-extra'
import { build as viteBuild, InlineConfig, mergeConfig, ResolvedConfig } from 'vite'
import connect from 'connect'
import sirv from 'sirv'
Expand All @@ -14,6 +14,11 @@ export async function build(
viteConfig: InlineConfig = {},
) {
const indexPath = resolve(options.userRoot, 'index.html')

let originalIndexHTML: string | undefined
if (fs.existsSync(indexPath))
originalIndexHTML = await fs.readFile(indexPath, 'utf-8')

await fs.writeFile(indexPath, await getIndexHtml(options), 'utf-8')
let config: ResolvedConfig = undefined!

Expand All @@ -36,7 +41,10 @@ export async function build(
)
}
finally {
await fs.unlink(indexPath)
if (originalIndexHTML != null)
await fs.writeFile(indexPath, originalIndexHTML, 'utf-8')
else
await fs.unlink(indexPath)
}

if (options.data.config.download === true || options.data.config.download === 'auto') {
Expand Down
4 changes: 2 additions & 2 deletions packages/slidev/node/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { uniq } from '@antfu/utils'
import { ResolvedSlidevOptions } from './options'
import { toAtFS } from './utils'

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

const roots = uniq([
...themeRoots,
// userRoot,
userRoot,
])

for (const root of roots) {
Expand Down

0 comments on commit fa3b12e

Please sign in to comment.