UNPKG

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) 5.21 kB
"use strict";const n=require("../common-utils/index-Bp2I1MnI.cjs"),c=require("../integration-utils/index-CyvitpnO.cjs"),u=new Map,l=new Map,_=class extends HTMLElement{constructor(){super(),this._state="uninitialized",this._initialValues={};let t=this._getMeta();t||(this._ctor()._initClass(),t=this._getMeta());for(const e in t.props){if(!Object.hasOwn(this,e))continue;const s=this[e];typeof s=="string"&&(this._initialValues[e]=s)}queueMicrotask(this._init.bind(this))}_init(){if(this._state!=="uninitialized")return;this._state="initialization";const{props:t,attrs:e}=this._getMeta();for(const s in t)delete this[s],this[s]=this._initialValues[s]??super.getAttribute(n.s(s))??t[s].default;if(delete this._initialValues,this._state="attrs-first-change",this.attributeChangedCallback){const s=i=>this.attributeChangedCallback(i,null,this.getAttribute(i));for(const i in e)s(i);for(const{name:i}of this.attributes)i in e||s(i)}this._state="normal"}connectedCallback(){this._init()}getAttribute(t){return this._init(),super.getAttribute(t)??this._getMeta().attrs[t]?.default??null}setAttribute(t,e){this._setAttr(t,e)}removeAttribute(t){this._setAttr(t,null)}toggleAttribute(t,e){return this._setAttr(t,e||typeof this.getAttribute(t)=="string"?null:""),!!this.getAttribute(t)}_setAttr(t,e){this._init();const s=this._getMeta().attrs[t],i=this._state!=="initialization",a=this.getAttribute(t);e??=s?.default??null,!(i&&!this._isFirstObserverCall&&e===a)&&(typeof e=="string"?super.setAttribute.call(this,t,e):super.removeAttribute.call(this,t),i&&this.attributeChangedCallback&&this.attributeChangedCallback(t,a,e))}_ctor(){return Object.getPrototypeOf(this).constructor}_getMeta(){return u.get(this._ctor())}static define(t){const{tag:e,registry:s=customElements,noRedefinitionError:i}=t;s.get(e)!==this&&(i&&l.get(s)?.has(this)||(this._initClass(),s.define(e,this)))}static _initClass(){if(u.has(this))return;this.props=Object.getPrototypeOf(this).props.concat(this.props);const t={props:{},attrs:{}};u.set(this,t);for(const e of this.props){const{name:s,...i}=typeof e=="string"?{name:e}:e;if(t.props[s])continue;const a=n.s(s);t.props[s]=i,t.attrs[a]=i,Object.defineProperty(this.prototype,s,{get(){return this.getAttribute(a)??i.default},set(m){this._setAttr(a,m??null)}})}}};_.props=[];let o=_;const g=o.prototype,v=["innerHTML","innerText","outerHTML","attributes"];for(const r of v){const t=Object.getOwnPropertyDescriptor(Element.prototype,r);if(!t||!t.get&&!t.set)continue;const e={};t.get&&(e.get=function(){return this._init(),t.get.call(this)}),t.set&&(e.set=function(s){this._init(),t.set.call(this,s)}),Object.defineProperty(g,r,e)}const A=["hasAttribute","hasAttributes","getAttributeNames"];for(const r of A){const t=g[r];g[r]=function(...e){return this._init(),t.call(this,...e)}}const C=CustomElementRegistry.prototype.define;CustomElementRegistry.prototype.define=function(r,t,...e){const s=()=>C.call(this,r,t,...e);return t.prototype instanceof o&&(l.has(this)||l.set(this,new Set),l.get(this).add(t),delete t.observedAttributes),s()};const E="svg-image{display:inline-block}svg-image svg{display:block}",p={viewBox:!0,width:!0,height:!0},b='<svg viewBox="0 0 0 0" width="0" height="0"></svg>',f=class extends o{constructor(){super(),this._updateSrcRes={},this._svgEl=document.createElementNS("http://www.w3.org/2000/svg","svg"),this._useEl=document.createElementNS("http://www.w3.org/2000/svg","use"),this._svgEl.appendChild(this.useEl)}get useEl(){return this._useEl}get svgEl(){return this._svgEl}connectedCallback(){super.connectedCallback(),n.l(this.getStyleId(),E.replaceAll("svg-image",this.localName)),this.childElementCount||this.appendChild(this._svgEl)}disconnectedCallback(){c.T(this._updateSrcRes.id)}attributeChangedCallback(t,e,s){if(!p[t]){if(t==="src"){this._handleSrcUpdate(e,s);return}if(t.startsWith("use-")){this._handleUseAttrUpdate(t.substring(4),s);return}t.startsWith("svg-")&&this._handleSvgAttrUpdate(t.substring(4),s)}}getStyleId(){return"svg-image-web-component__"+this.localName}_handleSrcUpdate(t,e){this._updateSrcRes=c.z(t??void 0,e||b),this._updateSrcRes.id&&n.c(this.useEl,"href","#"+this._updateSrcRes.id),this._updateSrcRes.attrs&&n.u(this.svgEl,this._updateSrcRes.attrs)}_handleSvgAttrUpdate(t,e){p[t]||n.c(this._svgEl,t,e)}_handleUseAttrUpdate(t,e){t!=="href"&&n.c(this.useEl,t,e)}static define(t={}){const e=t.tag||"svg-image";super.define({...t,tag:e})}};f.props=[{name:"src",default:b}];let d=f;const y={size:!0,color:!0,"color-transition":!0},h=class extends o{constructor(){super(),c.N(),d.define({noRedefinitionError:!0}),this._svgImage=new d,this._svgImage.setAttribute("svg-aria-hidden","true")}connectedCallback(){super.connectedCallback(),this.childElementCount||(this.classList.add("vite-awesome-svg-loader-icon"),this.appendChild(this._svgImage))}attributeChangedCallback(t,e,s){t==="src"?this._svgImage.src=s||"":y[t]&&this.style.setProperty("--"+t,s||"")}static define(t={}){super.define({...t,tag:t.tag||"svg-icon"})}};h.DEFAULT_COLOR_TRANSITION=c.A,h.props=["src","size","color",{name:"colorTransition",default:h.DEFAULT_COLOR_TRANSITION}];let w=h;exports.C=w;exports.a=o;exports.c=d; //# sourceMappingURL=index-DSZOdBhc.cjs.map