Skip to content

Commit

Permalink
fix: compat with latest vite + handle no export default script tags i…
Browse files Browse the repository at this point in the history
…n md
  • Loading branch information
yyx990803 committed Oct 30, 2020
1 parent a185822 commit b10da2f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/node/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { renderPage } from './render'
import { OutputChunk, OutputAsset } from 'rollup'

export type BuildOptions = Pick<
ViteBuildOptions,
Partial<ViteBuildOptions>,
| 'root'
| 'rollupInputOptions'
| 'rollupOutputOptions'
Expand Down
4 changes: 2 additions & 2 deletions src/node/build/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -153,5 +153,5 @@ export async function bundle(
outDir: config.tempDir
})

return [clientResult, serverResult, pageToHashMap]
return [clientResult[0], serverResult[0], pageToHashMap]
}
11 changes: 9 additions & 2 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,23 @@ 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(
JSON.stringify(data)
)}`
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>`)
Expand Down

0 comments on commit b10da2f

Please sign in to comment.