fix: avoid circular dependency between siteData virtual module and useData()

close #2072
close #2073
close #2074
pull/2040/head
Evan You 1 year ago
parent aa5b0b4cd7
commit 905f58b2a8

@ -58,21 +58,6 @@ export function pathToFile(path: string): string {
return pagePath
}
export function deserializeFunctions(value: any): any {
if (Array.isArray(value)) {
return value.map(deserializeFunctions)
} else if (typeof value === 'object' && value !== null) {
return Object.keys(value).reduce((acc, key) => {
acc[key] = deserializeFunctions(value[key])
return acc
}, {} as any)
} else if (typeof value === 'string' && value.startsWith('_vp-fn_')) {
return new Function(`return ${value.slice(7)}`)()
} else {
return value
}
}
export let contentUpdatedCallbacks: (() => any)[] = []
/**

@ -21,12 +21,7 @@ export { useData } from './app/data.js'
export { useRoute, useRouter } from './app/router.js'
// utilities
export {
deserializeFunctions,
inBrowser,
onContentUpdated,
withBase
} from './app/utils.js'
export { inBrowser, onContentUpdated, withBase } from './app/utils.js'
// components
export { Content } from './app/components/Content.js'

@ -160,7 +160,7 @@ export async function createVitePressPlugin(
data = { ...siteData, head: [] }
}
data = serializeFunctions(data)
return `import { deserializeFunctions } from 'vitepress/client'
return `${deserializeFunctions.toString()}
export default deserializeFunctions(JSON.parse(${JSON.stringify(
JSON.stringify(data)
)}))`
@ -359,3 +359,18 @@ export async function createVitePressPlugin(
await dynamicRoutesPlugin(siteConfig)
]
}
function deserializeFunctions(value: any): any {
if (Array.isArray(value)) {
return value.map(deserializeFunctions)
} else if (typeof value === 'object' && value !== null) {
return Object.keys(value).reduce((acc, key) => {
acc[key] = deserializeFunctions(value[key])
return acc
}, {} as any)
} else if (typeof value === 'string' && value.startsWith('_vp-fn_')) {
return new Function(`return ${value.slice(7)}`)()
} else {
return value
}
}

Loading…
Cancel
Save