diff --git a/src/client/app/utils.ts b/src/client/app/utils.ts index 1c3f5ae6..f24f4dbf 100644 --- a/src/client/app/utils.ts +++ b/src/client/app/utils.ts @@ -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)[] = [] /** diff --git a/src/client/index.ts b/src/client/index.ts index d00b9876..f97c8c78 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -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' diff --git a/src/node/plugin.ts b/src/node/plugin.ts index fee87e1e..06954fc6 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -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 + } +}