fix: compat with latest vite + handle no export default script tags in md

pull/112/head
Evan You 4 years ago
parent a185822953
commit b10da2f47b

@ -6,7 +6,7 @@ import { renderPage } from './render'
import { OutputChunk, OutputAsset } from 'rollup'
export type BuildOptions = Pick<
ViteBuildOptions,
Partial<ViteBuildOptions>,
| 'root'
| 'rollupInputOptions'
| 'rollupOutputOptions'

@ -118,7 +118,7 @@ export async function bundle(
// resolve options to pass to vite
const { rollupInputOptions = {}, rollupOutputOptions = {} } = options
const viteOptions: ViteBuildOptions = {
const viteOptions: Partial<ViteBuildOptions> = {
...options,
base: config.site.base,
resolvers: [resolver],
@ -153,5 +153,5 @@ export async function bundle(
outDir: config.tempDir
})
return [clientResult, serverResult, pageToHashMap]
return [clientResult[0], serverResult[0], pageToHashMap]
}

@ -63,6 +63,8 @@ export function createMarkdownToVueRenderFn(
}
const scriptRE = /<\/script>/
const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/
function injectPageData(tags: string[], data: PageData) {
const code = `\nexport const __pageData = ${JSON.stringify(
@ -70,9 +72,14 @@ function injectPageData(tags: string[], data: PageData) {
)}`
const existingScriptIndex = tags.findIndex((tag) => scriptRE.test(tag))
if (existingScriptIndex > -1) {
tags[existingScriptIndex] = tags[existingScriptIndex].replace(
const tagSrc = tags[existingScriptIndex]
// user has <script> tag inside markdown
// if it doesn't have export default it will error out on build
const hasDefaultExport =
defaultExportRE.test(tagSrc) || namedDefaultExportRE.test(tagSrc)
tags[existingScriptIndex] = tagSrc.replace(
scriptRE,
code + `</script>`
code + (hasDefaultExport ? `` : `\nexport default{}\n`) + `</script>`
)
} else {
tags.push(`<script>${code}\nexport default {}</script>`)

Loading…
Cancel
Save