Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve site data parsing #780

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/client/app/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InjectionKey, Ref, shallowRef, readonly, computed, inject } from 'vue'
import { Route } from './router'
import serializedSiteData from '@siteData'
import siteData from '@siteData'
import {
PageData,
SiteData,
Expand All @@ -25,17 +25,14 @@ export interface VitePressData<T = any> {
// site data is a singleton
export type SiteDataRef<T = any> = Ref<SiteData<T>>

export const siteDataRef: Ref<SiteData> = shallowRef(parse(serializedSiteData))

function parse(data: string): SiteData {
const parsed = JSON.parse(data)
return (import.meta.env.DEV ? readonly(parsed) : parsed) as SiteData
}
export const siteDataRef: Ref<SiteData> = shallowRef(
import.meta.env.PROD ? siteData : readonly(siteData)
)

// hmr
if (import.meta.hot) {
import.meta.hot!.accept('/@siteData', (m) => {
siteDataRef.value = parse(m.default)
siteDataRef.value = m.default
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/client/shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ declare module '*.vue' {
}

declare module '@siteData' {
const data: string
import type { SiteData } from './shared'
const data: SiteData
export default data
}

Expand Down
4 changes: 3 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export async function createVitePressPlugin(
if (config.command === 'build') {
data = { ...siteData, head: [] }
}
return `export default ${JSON.stringify(JSON.stringify(data))}`
return `export default JSON.parse(${JSON.stringify(
JSON.stringify(data)
)})`
}
},

Expand Down