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.
86 lines (85 loc) • 3.04 kB
JavaScript
import h from "debounce";
import m from "imurmurhash";
import { l as w } from "../common-utils/index-YJ0rTXLH.js";
const y = "#vite-awesome-svg-loader-symbols{position:fixed;top:-99999px;left:-99999px;z-index:0;opacity:0}", g = "vite-awesome-svg-loader-symbols", b = "vite-awesome-svg-loader-symbols-styles", p = "svg-", c = "data-count";
function I(e, t) {
if (typeof window > "u" || e === t)
return {};
w(b, y);
let o = document.getElementById(g);
if (o || (o = document.createElementNS("http://www.w3.org/2000/svg", "svg"), o.id = g, o.setAttribute("aria-hidden", "true"), document.body.appendChild(o)), e) {
const s = p + new m(e).result();
f(document.getElementById(s));
}
const i = p + new m(t).result(), l = document.getElementById(i);
if (l)
return l.setAttribute(c, d(l) + 1 + ""), { id: i, attrs: v(l) };
const r = new DOMParser().parseFromString(t, "application/xml").firstElementChild;
if (r?.querySelector("parsererror"))
return console.error("Source code is not a valid SVG: " + t), { id: i };
if (!r)
return console.error("Missing child in SVG: " + t), { id: i };
const n = document.createElementNS("http://www.w3.org/2000/svg", "symbol");
for (let s = 0; s < r.attributes.length; s++) {
const u = r.attributes[s];
n.setAttribute(u.name, u.value);
}
for (n.id = i, n.setAttribute(c, "1"); r.children.length; )
n.appendChild(r.children[0]);
return o.appendChild(n), { id: i, attrs: v(n) };
}
function N(e) {
!e || typeof window > "u" || f(typeof e == "string" ? document.getElementById(e) : e);
}
function f(e) {
if (!e)
return;
const t = d(e, 1) - 1;
e.setAttribute(c, t + ""), t <= 0 && (a.push(e), x());
}
const a = [], x = h(() => {
for (let e = a.length - 1; e >= 0; e--) {
const t = a[e];
t.parentElement && d(t) <= 0 && t.parentElement.removeChild(t), a.pop();
}
}, 5e3);
function d(e, t = 0) {
if (!e)
return t;
const o = parseInt(e.getAttribute(c) || "1");
return isNaN(o) ? t : o;
}
function v(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 z = ".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)}", E = "vite-awesome-svg-loader-icons-styles", A = "0.3s linear";
function G() {
typeof window < "u" && w(E, z);
}
function M(e) {
const t = {};
return e.size && (t["--size"] = e.size), e.color && (t["--color"] = e.color), t["--color-transition"] = e.colorTransition || A, t;
}
export {
A,
M as B,
G as N,
N as T,
E as _,
c,
p as f,
g as m,
b as y,
I as z
};
//# sourceMappingURL=index-Bs_mjsj3.js.map