vite-awesome-svg-loader
Version:
A universal Vite SVG loader. Imports SVGs as source code, base64 and data URI. Preserves stroke width. Replaces colors with currentColor or custom colors. Creates SVG sprites. Optimizes SVGs.
3 lines (2 loc) • 2.59 kB
JavaScript
;const z=require("debounce"),v=require("imurmurhash"),h=require("../common-utils/index-Bp2I1MnI.cjs"),x="#vite-awesome-svg-loader-symbols{position:fixed;top:-99999px;left:-99999px;z-index:0;opacity:0}",d="vite-awesome-svg-loader-symbols",p="vite-awesome-svg-loader-symbols-styles",u="svg-",l="data-count";function A(e,t){if(typeof window>"u"||e===t)return{};h.l(p,x);let o=document.getElementById(d);if(o||(o=document.createElementNS("http://www.w3.org/2000/svg","svg"),o.id=d,o.setAttribute("aria-hidden","true"),document.body.appendChild(o)),e){const s=u+new v(e).result();f(document.getElementById(s))}const i=u+new v(t).result(),c=document.getElementById(i);if(c)return c.setAttribute(l,m(c)+1+""),{id:i,attrs:w(c)};const n=new DOMParser().parseFromString(t,"application/xml").firstElementChild;if(n?.querySelector("parsererror"))return console.error("Source code is not a valid SVG: "+t),{id:i};if(!n)return console.error("Missing child in SVG: "+t),{id:i};const r=document.createElementNS("http://www.w3.org/2000/svg","symbol");for(let s=0;s<n.attributes.length;s++){const g=n.attributes[s];r.setAttribute(g.name,g.value)}for(r.id=i,r.setAttribute(l,"1");n.children.length;)r.appendChild(n.children[0]);return o.appendChild(r),{id:i,attrs:w(r)}}function E(e){!e||typeof window>"u"||f(typeof e=="string"?document.getElementById(e):e)}function f(e){if(!e)return;const t=m(e,1)-1;e.setAttribute(l,t+""),t<=0&&(a.push(e),B())}const a=[],B=z(()=>{for(let e=a.length-1;e>=0;e--){const t=a[e];t.parentElement&&m(t)<=0&&t.parentElement.removeChild(t),a.pop()}},5e3);function m(e,t=0){if(!e)return t;const o=parseInt(e.getAttribute(l)||"1");return isNaN(o)?t:o}function w(e){let t=e.getAttribute("viewBox")||"";if(!t)for(const o of["x","y","width","height"]){const i=e.getAttribute(o);t+=(i||"0")+" "}return{viewBox:t,width:"100%",height:"100%"}}const S=".vite-awesome-svg-loader-icon{display:inline-block;min-width:var(--size);width:var(--size);max-width:var(--size);min-height:var(--size);height:var(--size);max-height:var(--size);color:var(--color)}.vite-awesome-svg-loader-icon svg{vertical-align:top}.vite-awesome-svg-loader-icon use{transition:color var(--color-transition)}",y="vite-awesome-svg-loader-icons-styles",b="0.3s linear";function N(){typeof window<"u"&&h.l(y,S)}function C(e){const t={};return e.size&&(t["--size"]=e.size),e.color&&(t["--color"]=e.color),t["--color-transition"]=e.colorTransition||b,t}exports.A=b;exports.B=C;exports.N=N;exports.T=E;exports._=y;exports.c=l;exports.f=u;exports.m=d;exports.y=p;exports.z=A;
//# sourceMappingURL=index-CyvitpnO.cjs.map