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

3 lines
2.3 KiB
JavaScript

import{isFunction as w,isArray as g,removeEndingSlash as x,removeLeadingSlash as j,getBundlerName as P,addViteConfig as b,configWebpack as R}from"@vuepress/helper";import{createVitePlugin as S,createWebpackPlugin as I}from"unplugin";import M from"magic-string";const O="@vuepress/plugin-replace-assets",_=["apng","bmp","png","jpe?g","jfif","pjpeg","pjp","gif","svg","ico","webp","avif","cur","jxl"],k=["mp4","webm","ogg","mp3","wav","flac","aac","opus","mov","m4a","vtt","pdf"],o=(t,e)=>new RegExp(`^/${t}/.*\\.(?:${e.join("|")})(\\?.*)?$`),m=t=>{const e=[];return t?((typeof t=="string"||w(t))&&(t={all:t}),g(t)?(e.push(...t),e):"find"in t?(t.find&&t.replacement&&e.push(t),e):(t.rules&&e.push(...g(t.rules)?t.rules:[t.rules]),(t.image||t.all)&&e.push({find:o("images",_),replacement:t.image||t.all}),(t.media||t.all)&&e.push({find:o("medias",k),replacement:t.media||t.all}),e)):[]},y=t=>{const e=`(${t}.*?)`;return new RegExp([`(?:"${e}")`,`(?:'${e}')`,`(?:\\(${e}\\))`,`(?:\\('${e}'\\))`,`(?:\\("${e}"\\))`,`(?:\\\\"${e}\\\\")`].join("|"),"gu")},f=(t,e)=>t?e?`${x(e)}/${j(t)}`:t:"",d=(t,e)=>typeof t=="string"?t.startsWith("^")||t.endsWith("$")?new RegExp(t).test(e):e.endsWith(t)||e.startsWith(t):t.test(e),p=new Map,h=(t,e)=>{if(p.has(e))return p.get(e);for(const{find:s,replacement:r}of t)if(s&&d(s,e)){const n=typeof r=="function"?f(r(e)):f(e,r);/* istanbul ignore if -- @preserve */if(n)return p.set(e,n),n}},$=(t,e,s)=>{const r=new M(t);let n,a=!1;for(;n=e.exec(t);){const i=n[6]||n[5]||n[4]||n[3]||n[2]||n[1],[l,v]=n[0].startsWith("(")?['("','")']:n[0].startsWith('\\"')?['\\"','\\"']:['"','"'],c=n.index,A=c+n[0].length,u=h(s,i);u&&(a=!0,r.update(c,A,`${l}${u}${v}`))}return a?r.toString():t},W=t=>{const e=y("/[^/]");return{name:"vuepress:replace-assets",enforce:"pre",transform:{filter:{id:{exclude:[/\.json(?:$|\?)/,/\.html?$/]}},handler(s){return $(s,e,t)}}}},E=()=>S(W),N=()=>I(W),U=t=>{const e={name:O},s=m(t);return s.length===0?e:{...e,extendsBundlerOptions(r,n){const a=P(n);if(a==="vite"){const i=E();b(r,n,{plugins:[i(s)]})}a==="webpack"&&R(r,n,i=>{i.plugins??=[];const l=N();i.plugins.push(l(s))})}}};export{o as createFindPattern,E as createVitePluginReplaceAssets,N as createWebpackPluginReplaceAssets,d as isMatchUrl,m as normalizeRules,U as replaceAssetsPlugin,h as replacementAssetWithRules,$ as transformAssets};
//# sourceMappingURL=index.js.map