refactor: use stringified `deserializeFunctions` util (#2733)

pull/2734/head
Divyansh Singh 1 year ago committed by GitHub
parent e3f8fc7972
commit b55b529f23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -176,7 +176,7 @@ function generateMetadataScript(
const metadataContent = `window.__VP_HASH_MAP__=JSON.parse(${hashMapString});${ const metadataContent = `window.__VP_HASH_MAP__=JSON.parse(${hashMapString});${
siteDataString.includes('_vp-fn_') siteDataString.includes('_vp-fn_')
? `${deserializeFunctions.toString()};window.__VP_SITE_DATA__=deserializeFunctions(JSON.parse(${siteDataString}));` ? `${deserializeFunctions};window.__VP_SITE_DATA__=deserializeFunctions(JSON.parse(${siteDataString}));`
: `window.__VP_SITE_DATA__=JSON.parse(${siteDataString});` : `window.__VP_SITE_DATA__=JSON.parse(${siteDataString});`
}` }`

@ -172,8 +172,7 @@ export async function createVitePressPlugin(
} }
} }
data = serializeFunctions(data) data = serializeFunctions(data)
return `${deserializeFunctions.toString()} return `${deserializeFunctions};export default deserializeFunctions(JSON.parse(${JSON.stringify(
export default deserializeFunctions(JSON.parse(${JSON.stringify(
JSON.stringify(data) JSON.stringify(data)
)}))` )}))`
} }

@ -20,6 +20,7 @@ export function serializeFunctions(value: any, key?: string): any {
} }
} }
/*
export function deserializeFunctions(value: any): any { export function deserializeFunctions(value: any): any {
if (Array.isArray(value)) { if (Array.isArray(value)) {
return value.map(deserializeFunctions) return value.map(deserializeFunctions)
@ -34,3 +35,7 @@ export function deserializeFunctions(value: any): any {
return value return value
} }
} }
*/
export const deserializeFunctions =
'function deserializeFunctions(r){return Array.isArray(r)?r.map(deserializeFunctions):typeof r=="object"&&r!==null?Object.keys(r).reduce((t,n)=>(t[n]=deserializeFunctions(r[n]),t),{}):typeof r=="string"&&r.startsWith("_vp-fn_")?new Function(`return ${r.slice(7)}`)():r}'

Loading…
Cancel
Save