UNPKG

rollup-plugin-inline-svg

Version:
2 lines (1 loc) 1.99 kB
"use strict";var E=Object.defineProperty,I=Object.defineProperties;var N=Object.getOwnPropertyDescriptors;var O=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var k=(r,t,e)=>t in r?E(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,u=(r,t)=>{for(var e in t||(t={}))T.call(t,e)&&k(r,e,t[e]);if(O)for(var e of O(t))C.call(t,e)&&k(r,e,t[e]);return r},S=(r,t)=>I(r,N(t));var $=require("@rollup/pluginutils"),q=require("html-to-ast");const y=r=>{r.attrs={},r.name="element-marked-to-remove"},j=r=>{y(r),r.children=[]},L=r=>{let t=null;return c.traverse(r,e=>{var f,s;return((s=(f=e.name)==null?void 0:f.toLowerCase())!=null?s:"")==="svg"?(t=e,!0):!1}),t},R=r=>{for(const t of r){const e=L(t);if(e)return e}return null};class c{static process(t,e){const l=q.parse(t.trim()),f=R(l);if(f==null)throw new Error(`rollup-plugin-inline-svg: file ${e.fileName} is not a valid svg. The 'svg' node was not found.`);const s=e.forbidden!==void 0||e.traverse!==void 0;s&&c.traverse(f,a=>{var v,d,i,m,g,h,x,A,b;((v=e==null?void 0:e.forbidden)==null?void 0:v.attrs)!==void 0&&a.attrs!==void 0&&Object.keys(a.attrs).forEach(w=>{e.forbidden.attrs.indexOf(w)>=0&&delete a.attrs[w]}),((g=(m=(d=e==null?void 0:e.forbidden)==null?void 0:d.tags)==null?void 0:m.indexOf((i=a.name)!=null?i:""))!=null?g:-1)>=0&&y(a),((A=(x=(h=e==null?void 0:e.forbidden)==null?void 0:h.tags)==null?void 0:x.indexOf(a.name+":remove"))!=null?A:-1)>=0&&j(a),(b=e==null?void 0:e.traverse)==null||b.call(e,a)});let n=q.stringify([f]);return s&&(n=n.replace(/<\/?element-marked-to-remove>/g,"")),n}static traverse(t,e){!e(t)&&t.children&&t.children.forEach(l=>this.traverse(l,e))}}const U={include:["**/*.svg"]};function z(r={}){r=u(u({},U),r);const t=$.createFilter(r.include,r.exclude);return{name:"InlineSvg",transform(e,l){if(!!t(l))return{code:`export default '${c.process(e,S(u({},r),{fileName:l})).replace(/\n/g,"")}'`,map:{mappings:""}}}}}module.exports=z;