diff --git a/dist/index.js b/dist/index.js index e3f61b2..17fc52d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,18 +1,18 @@ -var P="astro-svg-sprite";import p from"node:fs";function H(t){let e="./src/assets/images/sprite";if(!t||t==="")return e;if(typeof t=="string")return t;if(Array.isArray(t)){let r=t.filter(s=>s&&s!=="");if(r.length>0)return r}return e}function J(t){return!t||t===""?"assets/images":t}var w=class{colors={reset:"\x1B[0m",fg:{red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",cyanBold:"\x1B[1m\x1B[36m"}};packageName;constructor(e){this.packageName=e}log(e,r=""){let s=e.join(` -`),n=new Date,a=n.getHours().toString().padStart(2,"0"),i=n.getMinutes().toString().padStart(2,"0"),l=n.getSeconds().toString().padStart(2,"0"),c=`\x1B[2m${a}:${i}:${l}\x1B[22m`;console.log(`${c} %s[${this.packageName}]%s ${s}`,r,r?this.colors.reset:"")}info(...e){this.log(e,this.colors.fg.cyanBold)}success(...e){this.log(e,this.colors.fg.cyanBold)}warn(...e){this.log([`${this.colors.fg.yellow}(!)${this.colors.reset} ${e}`],this.colors.fg.cyanBold)}error(...e){this.log([`${this.colors.fg.red}failed!${this.colors.reset}`,...e],this.colors.fg.cyanBold)}};function A(t){let e=/viewBox="([^"]*)"/,r=/fill="([^"]*)"/,s=t.match(e),n=t.match(r),a=s&&s[0]?s[0]:"",i=n&&n[0]?n[0]:"";return`${a} ${i}`}function T(t){let e="",s=t.indexOf(e),n=t.indexOf(r);if(s!==-1&&n!==-1){let a=t.indexOf(">",s);return t.slice(a+1,n).trim()}return""}function $(t){return typeof t=="string"?p.readdirSync(t).some(r=>r.endsWith(".svg")):Array.isArray(t)?t.some(e=>p.readdirSync(e).some(s=>s.endsWith(".svg"))):!1}function y(t){return t.includes("")}function v(t){let e=performance.now();t();let r=performance.now();return Math.floor(r-e)}var F=new w(P);var k=[],b=[],S=[];function K(t){p.readdirSync(t).forEach(e=>{if(!e.toLowerCase().endsWith(".svg"))return;let r=p.readFileSync(`${t}/${e}`,"utf-8");y(r)?S.push(`${t}/${e}`):(b.push(`${t}/${e}`),S.push(`${t}/${e}`)),y(r)&&k.push({name:e.replace(/\.svg$/,""),content:r})})}function U(t){typeof t=="string"?K(t):Array.isArray(t)&&t.forEach(e=>K(e))}function N(t){return $(t)&&(typeof t=="string"?U(t):Array.isArray(t)&&t.forEach(e=>U(e))),k}function E(t){let e='';return t.forEach(r=>{e+=` - - ${T(r.content)} +"use strict";var B="astro-svg-sprite";import $ from"node:fs";function k(e){const t="./src/assets/images/sprite";if(!e||e==="")return t;if(typeof e=="string")return e;if(Array.isArray(e)){const s=e.filter(r=>r&&r!=="");if(s.length>0)return s}return t}function V(e){return!e||e===""?"assets/images":e}var E=class{colors={reset:"\x1B[0m",fg:{red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",cyanBold:"\x1B[1m\x1B[36m"}};packageName;constructor(e){this.packageName=e}log(e,t=""){const s=e.join(` +`),r=new Date,n=r.getHours().toString().padStart(2,"0"),a=r.getMinutes().toString().padStart(2,"0"),i=r.getSeconds().toString().padStart(2,"0"),c=`\x1B[2m${n}:${a}:${i}\x1B[22m`;console.log(`${c} %s[${this.packageName}]%s ${s}`,t,t?this.colors.reset:"")}info(...e){this.log(e,this.colors.fg.cyanBold)}success(...e){this.log(e,this.colors.fg.cyanBold)}warn(...e){this.log([`${this.colors.fg.yellow}(!)${this.colors.reset} ${e}`],this.colors.fg.cyanBold)}error(...e){this.log([`${this.colors.fg.red}failed!${this.colors.reset}`,...e],this.colors.fg.cyanBold)}};function O(e){const t=/viewBox="([^"]*)"/,s=/fill="([^"]*)"/,r=e.match(t),n=e.match(s),a=r&&r[0]?r[0]:"",i=n&&n[0]?n[0]:"";return`${a} ${i}`}function z(e){const t="",r=e.indexOf(t),n=e.indexOf(s);if(r!==-1&&n!==-1){const a=e.indexOf(">",r);return e.slice(a+1,n).trim()}return""}function A(e){return typeof e=="string"?$.readdirSync(e).some(s=>s.endsWith(".svg")):Array.isArray(e)?e.some(t=>$.readdirSync(t).some(r=>r.endsWith(".svg"))):!1}function N(e){return e.includes("")}function T(e){const t=performance.now();e();const s=performance.now();return Math.floor(s-t)}var S=new E(B),w=[],b=[],x=[];function F(e){$.readdirSync(e).forEach(t=>{if(!t.toLowerCase().endsWith(".svg"))return;const s=$.readFileSync(`${e}/${t}`,"utf-8");N(s)?x.push(`${e}/${t}`):(b.push(`${e}/${t}`),x.push(`${e}/${t}`)),N(s)&&w.push({name:t.replace(/\.svg$/,""),content:s})})}function P(e){typeof e=="string"?F(e):Array.isArray(e)&&e.forEach(t=>F(t))}function C(e){return A(e)&&(typeof e=="string"?P(e):Array.isArray(e)&&e.forEach(t=>P(t))),w}function I(e){let t='';return e.forEach(s=>{t+=` + + ${z(s.content)} - `}),e+="",e}function R(t,e){if(!p.existsSync(t)){let r=t.substr(0,t.lastIndexOf("/"));p.existsSync(r)||p.mkdirSync(r,{recursive:!0}),p.writeFileSync(t,"","utf-8")}p.writeFileSync(t,e,"utf-8")}function V(t){let e=/]*)\/?>/g;return t.replace(e,(s,n)=>{let a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={},l;for(;(l=a.exec(n))!==null;){let[,x,d]=l;i[x]=d}let{cx:c,cy:g,r:o,...u}=i,m=`M${Number(c)-Number(o)},${g}A${o},${o} 0 1,0 ${Number(c)+Number(o)},${g}A${o},${o} 0 1,0 ${Number(c)-Number(o)},${g}`,f=Object.entries(u).map(([x,d])=>`${x}="${d}"`).join(" ");return``})}function B(t){let e=/]*)\/?>/g;return t.replace(e,(s,n)=>{let a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={},l;for(;(l=a.exec(n))!==null;){let[,d,h]=l;i[d]=h}let{cx:c,cy:g,rx:o,ry:u,...m}=i,f=`M${Number(c)-Number(o)},${g}A${o},${u} 0 1,0 ${Number(c)+Number(o)},${g}A${o},${u} 0 1,0 ${Number(c)-Number(o)},${g}`,x=Object.entries(m).map(([d,h])=>`${d}="${h}"`).join(" ");return``})}function L(t){let e=/]*)\/?>/g;return t.replace(e,(s,n)=>{let a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={},l;for(;(l=a.exec(n))!==null;){let[,d,h]=l;i[d]=h}let{x1:c,y1:g,x2:o,y2:u,...m}=i,f=`M ${c},${g} L ${o},${u}`,x=Object.entries(m).map(([d,h])=>`${d}="${h}"`).join(" ");return``})}function M(t){return t.split(/\s+/).map(e=>Number(e))}function D(t){let e=/]*)\/?>/g;return t.replace(e,(s,n)=>{let a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={},l;for(;(l=a.exec(n))!==null;){let[,m,f]=l;i[m]=f}let{points:c,...g}=i,o=`M ${M(c).join(" ")} Z`,u=Object.entries(g).map(([m,f])=>`${m}="${f}"`).join(" ");return``})}function W(t){let e=/]*)\/?>/g;return t.replace(e,(s,n)=>{let a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={},l;for(;(l=a.exec(n))!==null;){let[,m,f]=l;i[m]=f}let{points:c,...g}=i,o=`M ${M(c).join(" ")}`,u=Object.entries(g).map(([m,f])=>`${m}="${f}"`).join(" ");return``})}function Q(t){return t=V(t),t=B(t),t=L(t),t=D(t),t=W(t),t}function _(t){let e=["g","defs","metadata","title","desc","marker"];for(let r of e){let s=new RegExp(`<${r}\\b[^>]*>`,"g"),n=new RegExp(``,"g");t=t.replace(s,""),t=t.replace(n,"")}return t}function G(t){return t.replace(/>\s+<").replace(/\s+(?==|>)/g,"").replace(/(\r\n|\n|\r|\t)/g,"")}function Z(t){return t=G(t),t=_(t),t.replace(/<(\w+)><\/\1>/g,"").replace(/"/g,"'").replace(/([a-zA-Z-]+)\s*=\s*([a-zA-Z0-9-]+)/g,"$1=$2").replace(/style="([^"]+)"/g,(e,r)=>`style="${r.replace(/(\r\n|\n|\r|\t)/gm,"").replace(/ +/g," ")}"`)}function X(t){return t=Z(t),t=Q(t),t}function I(t,e){switch(e){case"best":t=X(t);break;case"standard":case void 0:t=Z(t);break;case"fast":t=G(t);break;default:break}return t}function O(t){return t.length===1?"file":"files"}function q(){if(b.length>0){console.log(` -\x1B[42m parsed ${S.length} SVG ${O(S)} \x1B[0m`);let t=[` -\x1B[33m(!) Please provide a standard svg ${O(b)}.`,`\x1B[1m${O(b)}:\x1B[22m ${JSON.stringify(b,null,2)}`,` \x1B[1mNot a valid SVG file.\x1B[22m -`,`\x1B[33m- To remove the warning information, delete or remove the SVG ${O(b)}.`,`- Visit \x1B[4mhttps://developer.mozilla.org/en-US/docs/Web/SVG\x1B[24m know more about SVG.\x1B[0m -`];console.log(`${t.join(` -`)}`)}}function C(t,e){p.stat(t,(r,s)=>{if(r){F.error("Could not read file information:",`${r}`);return}let a=s.size/1024;F.info(`\x1B[2mCompleted in ${j}ms.\x1B[22m`),q(),F.success(`\x1B[32mgenerated\x1B[0m 'sprite.svg' ${a}(kb).`)})}async function z(t,e){let r;return e?r=`${t.replaceAll(` -`,"")}`:r=` + `}),t+="",t}function L(e,t){if(!$.existsSync(e)){const s=e.substr(0,e.lastIndexOf("/"));$.existsSync(s)||$.mkdirSync(s,{recursive:!0}),$.writeFileSync(e,"","utf-8")}$.writeFileSync(e,t,"utf-8")}function D(e){const t=/]*)\/?>/g;return e.replace(t,(r,n)=>{const a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={};let c;for(;(c=a.exec(n))!==null;){const[,d,p]=c;i[d]=p}const{cx:l,cy:u,r:o,...g}=i,m=`M${Number(l)-Number(o)},${u}A${o},${o} 0 1,0 ${Number(l)+Number(o)},${u}A${o},${o} 0 1,0 ${Number(l)-Number(o)},${u}`,f=Object.entries(g).map(([d,p])=>`${d}="${p}"`).join(" ");return``})}function G(e){const t=/]*)\/?>/g;return e.replace(t,(r,n)=>{const a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={};let c;for(;(c=a.exec(n))!==null;){const[,p,h]=c;i[p]=h}const{cx:l,cy:u,rx:o,ry:g,...m}=i,f=`M${Number(l)-Number(o)},${u}A${o},${g} 0 1,0 ${Number(l)+Number(o)},${u}A${o},${g} 0 1,0 ${Number(l)-Number(o)},${u}`,d=Object.entries(m).map(([p,h])=>`${p}="${h}"`).join(" ");return``})}function W(e){const t=/]*)\/?>/g;return e.replace(t,(r,n)=>{const a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={};let c;for(;(c=a.exec(n))!==null;){const[,p,h]=c;i[p]=h}const{x1:l,y1:u,x2:o,y2:g,...m}=i,f=`M ${l},${u} L ${o},${g}`,d=Object.entries(m).map(([p,h])=>`${p}="${h}"`).join(" ");return``})}function M(e){return e.split(/\s+/).map(t=>Number(t))}function Z(e){const t=/]*)\/?>/g;return e.replace(t,(r,n)=>{const a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={};let c;for(;(c=a.exec(n))!==null;){const[,m,f]=c;i[m]=f}const{points:l,...u}=i,o=`M ${M(l).join(" ")} Z`,g=Object.entries(u).map(([m,f])=>`${m}="${f}"`).join(" ");return``})}function H(e){const t=/]*)\/?>/g;return e.replace(t,(r,n)=>{const a=/(\w+)\s*=\s*["']([^"']*)["']/g,i={};let c;for(;(c=a.exec(n))!==null;){const[,m,f]=c;i[m]=f}const{points:l,...u}=i,o=`M ${M(l).join(" ")}`,g=Object.entries(u).map(([m,f])=>`${m}="${f}"`).join(" ");return``})}function U(e){return e=D(e),e=G(e),e=W(e),e=Z(e),e=H(e),e}function _(e){const t=["g","defs","metadata","title","desc","marker"];for(const s of t){const r=new RegExp(`<${s}\\b[^>]*>`,"g"),n=new RegExp(``,"g");e=e.replace(r,""),e=e.replace(n,"")}return e}function R(e){return e.replace(/>\s+<").replace(/\s+(?==|>)/g,"").replace(/(\r\n|\n|\r|\t)/g,"")}function j(e){return e=R(e),e=_(e),e.replace(/<(\w+)><\/\1>/g,"").replace(/"/g,"'").replace(/([a-zA-Z-]+)\s*=\s*([a-zA-Z0-9-]+)/g,"$1=$2").replace(/style="([^"]+)"/g,(t,s)=>`style="${s.replace(/(\r\n|\n|\r|\t)/gm,"").replace(/ +/g," ")}"`)}function q(e){return e=j(e),e=U(e),e}function J(e,t){switch(t){case"best":e=q(e);break;case"standard":case void 0:e=j(e);break;case"fast":e=R(e);break;default:break}return e}function v(e){return e.length===1?"file":"files"}function K(){if(b.length>0){console.log(` +\x1B[42m parsed ${x.length} SVG ${v(x)} \x1B[0m`);const e=[` +\x1B[33m(!) Please provide a standard svg ${v(b)}.`,`\x1B[1m${v(b)}:\x1B[22m ${JSON.stringify(b,null,2)}`,` \x1B[1mNot a valid SVG file.\x1B[22m +`,`\x1B[33m- To remove the warning information, delete or remove the SVG ${v(b)}.`,`- Visit \x1B[4mhttps://developer.mozilla.org/en-US/docs/Web/SVG\x1B[24m know more about SVG.\x1B[0m +`];console.log(`${e.join(` +`)}`)}}function Q(e,t){$.stat(e,(s,r)=>{if(s){S.error("Could not read file information:",`${s}`);return}const a=r.size/1024;S.info(`\x1B[2mCompleted in ${y}ms.\x1B[22m`),K(),S.success(`\x1B[32mgenerated\x1B[0m 'sprite.svg' ${a}(kb).`)})}async function X(e,t){let s;return t?s=`${e.replaceAll(` +`,"")}`:s=` -${t.replace(/(? - `,{name:"vite-plugin-spriter",enforce:"pre",transform(s){try{return s.replace("",`${r}`)}catch(n){throw n}}}}var tt={emitFile:!0},et={include:"./src/assets/images/sprite",mode:"verbose",emitFile:{compress:"standard",path:"assets/images"},...tt},j;function rt(t={}){var g,o;let e,r,s={...et,...t},n=H(t.include),a=J((g=t.emitFile)==null?void 0:g.path),i=N(n),l=I(E(i),(o=s.emitFile)==null?void 0:o.compress);function c(){$(n)&&(t.emitFile||s.emitFile)&&(i.length!==0&&R(e,l),s.mode!=="quiet"&&C(e,a))}return{name:P,hooks:{"astro:config:setup":async({updateConfig:u,config:m})=>{((t==null?void 0:t.emitFile)!==void 0||(t==null?void 0:t.emitFile)===!1)&&u({vite:{plugins:[z(l,m.compressHTML)]}})},"astro:config:done":async({config:u})=>{r=u,e=`${r.publicDir.pathname}${a}/sprite.svg`},"astro:server:start":async()=>{j=v(c)},"astro:build:start":async()=>{j=v(c)}}}}export{rt as default,j as executionTime}; + `,{name:"vite-plugin-spriter",enforce:"pre",transform(r){try{return r.replace("",`${s}`)}catch(n){throw n}}}}import{fileURLToPath as Y}from"url";var ee={emitFile:!0},te={include:"./src/assets/images/sprite",mode:"verbose",emitFile:{compress:"standard",path:"assets/images"},...ee},y;function se(e={}){var t,s;let r,n;const a={...te,...e},i=k(e.include),c=V((t=e.emitFile)==null?void 0:t.path),l=C(i),u=J(I(l),(s=a.emitFile)==null?void 0:s.compress);function o(){A(i)&&(e.emitFile||a.emitFile)&&(l.length!==0&&L(r,u),a.mode!=="quiet"&&Q(r,c))}return{name:B,hooks:{"astro:config:setup":async({updateConfig:g,config:m})=>{(e?.emitFile!==void 0||e?.emitFile===!1)&&g({vite:{plugins:[X(u,m.compressHTML)]}})},"astro:config:done":async({config:g})=>{n=g,r=`${Y(n.publicDir)}${c}/sprite.svg`},"astro:server:start":async()=>{y=T(o)},"astro:build:start":async()=>{y=T(o)}}}}export{se as default,y as executionTime}; diff --git a/src/index.ts b/src/index.ts index 82732b5..14933e2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { packageName } from './data/pkg-name'; import { parseSvgs, generateSprite, optimizeSvgContent, writeFile, printFileStats, vitePluginSvgSprite } from './core'; import { hasSvgFilesInDirectory, measureExecutionTime } from './utils'; import { getEntryPath, getOutputPath } from './paths'; +import { fileURLToPath } from 'url' export interface PluginConfig { /** @@ -93,7 +94,7 @@ export default function svgSprite(astroConfig: PluginConfig = {}): AstroIntegrat }, 'astro:config:done': async ({ config: cfg }) => { config = cfg; - filePath = `${config.publicDir.pathname}${output}/sprite.svg`; + filePath = `${fileURLToPath(config.publicDir)}${output}/sprite.svg`; }, 'astro:server:start': async () => { executionTime = measureExecutionTime(emitFile);