fuc-new/doc/node_modules/@vuepress/plugin-cache/lib/node/index.js
hellcat 6adbc04d4b 1
2025-10-03 17:17:08 +08:00

3 lines
2.0 KiB
JavaScript

import k from"ci-info";import{LRUCache as x}from"lru-cache";import{hash as y,path as F,fs as l}from"vuepress/utils";const j=new x({max:64}),P=(e,n)=>{if(!n.env.isDev)return;const{highlight:t}=e.options;e.options.highlight=(...r)=>{const s=y(r.join("")),i=j.get(s);if(i)return i;const a=t?.(...r)??"";return j.set(s,a),a}},S=async e=>{try{return await l.readJSON(e,"utf-8")}catch{return null}},C=e=>{try{return l.readJSONSync(e,"utf-8")}catch{return null}},v=(e,n)=>l.writeJSON(e,n,"utf-8"),R=.15,T=(e=process.cwd())=>{try{const n=F.join(e,"tmp");l.writeFileSync(n,"{}","utf-8");const t=performance.now();C(n);const r=performance.now();return l.unlinkSync(n),r-t}catch{return R}},g="markdown/rendered",O="_metadata.json",b="_cache.json",D=async(e,n)=>{if(n.env.isBuild&&!l.existsSync(n.dir.cache(g)))return;const t=n.dir.cache(g),r=`${t}/${O}`,s=`${t}/${b}`;await l.ensureDir(t);const[i,a]=await Promise.all([S(r),S(s)]).then(([c,o])=>[c??{},o??{}]);let f=null;const p=()=>{f&&clearTimeout(f),f=setTimeout(()=>{Promise.all([v(r,i),v(s,a)])},200)},h=e.render;e.render=(c,o={})=>{const u=o.filePathRelative;if(!u)return h(c,o);const d=y(c);if(i[u]===d&&u in a){const m=a[u];return Object.assign(o,m.env),m.content}const w=h(c,o);return i[u]=d,a[u]={content:w,env:o},p(),w}},I=async(e,n)=>{if(n.env.isBuild&&!l.existsSync(n.dir.cache(g)))return;const t=n.dir.cache(g);await l.ensureDir(t);const r=T(t),s=`${t}/${O}`,i=await S(s)??{};let a=null;const f=(h,c)=>{v(`${t}/${h}`,c),a&&clearTimeout(a),a=setTimeout(()=>{v(s,i)},200)},p=e.render;e.render=(h,c={})=>{const o=c.filePathRelative;if(!o)return p(h,c);const u=y(h),d=y(o);if(i[o]===u){const $=C(`${t}/${d}`);if($)return Object.assign(c,$.env),$.content;i[o]=""}const w=performance.now(),m=p(h,c);return performance.now()-w>r&&(i[o]=u,f(d,{content:m,env:c})),m}},J=({type:e,enableInCi:n=!1}={})=>{const t={name:"@vuepress/plugin-cache"};return k.isCI&&!n?t:{...t,async extendsMarkdown(r,s){P(r,s),e==="filesystem"?await I(r,s):await D(r,s)}}};export{J as cachePlugin};
//# sourceMappingURL=index.js.map