From b55b529f2346f0d6db72ee65220c4fdbecb27db9 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 4 Aug 2023 20:28:13 +0530 Subject: [PATCH] refactor: use stringified `deserializeFunctions` util (#2733) --- src/node/build/build.ts | 2 +- src/node/plugin.ts | 3 +-- src/node/utils/fnSerialize.ts | 5 +++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/node/build/build.ts b/src/node/build/build.ts index fea62860..685a92b6 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -176,7 +176,7 @@ function generateMetadataScript( const metadataContent = `window.__VP_HASH_MAP__=JSON.parse(${hashMapString});${ 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});` }` diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 0aa75334..615ddf12 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -172,8 +172,7 @@ export async function createVitePressPlugin( } } data = serializeFunctions(data) - return `${deserializeFunctions.toString()} - export default deserializeFunctions(JSON.parse(${JSON.stringify( + return `${deserializeFunctions};export default deserializeFunctions(JSON.parse(${JSON.stringify( JSON.stringify(data) )}))` } diff --git a/src/node/utils/fnSerialize.ts b/src/node/utils/fnSerialize.ts index f3022eec..010cb491 100644 --- a/src/node/utils/fnSerialize.ts +++ b/src/node/utils/fnSerialize.ts @@ -20,6 +20,7 @@ export function serializeFunctions(value: any, key?: string): any { } } +/* export function deserializeFunctions(value: any): any { if (Array.isArray(value)) { return value.map(deserializeFunctions) @@ -34,3 +35,7 @@ export function deserializeFunctions(value: any): any { 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}'