rollup-plugin-svg-icons
Version:
Bundles all svg icons from the speciefed folder to the single spritesheet svg file
3 lines (2 loc) • 4.04 kB
JavaScript
;function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var fs=_interopDefault(require("fs")),path=_interopDefault(require("path")),cheerio=_interopDefault(require("cheerio")),pretty=_interopDefault(require("pretty")),htmlMinifier=require("html-minifier"),SVGO=_interopDefault(require("svgo"));function _toConsumableArray(t){return _arrayWithoutHoles(t)||_iterableToArray(t)||_nonIterableSpread()}function _arrayWithoutHoles(t){if(Array.isArray(t)){for(var e=0,r=new Array(t.length);e<t.length;e++)r[e]=t[e];return r}}function _iterableToArray(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _await(t,e,r){return r?e?e(t):t:(t&&t.then||(t=Promise.resolve(t)),e?t.then(e):t)}function _invoke(t,e){var r=t();return r&&r.then?r.then(e):e(r)}var _iteratorSymbol="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function _settle(t,e,r){if(!t.s){if(r instanceof _Pact){if(!r.s)return void(r.o=_settle.bind(null,t,e));1&e&&(e=r.s),r=r.v}if(r&&r.then)return void r.then(_settle.bind(null,t,e),_settle.bind(null,t,2));t.s=e,t.v=r;var n=t.o;n&&n(t)}}var _Pact=function(){function t(){}return t.prototype.then=function(e,r){var n=new t,o=this.s;if(o){var i=1&o?e:r;if(i){try{_settle(n,1,i(this.v))}catch(t){_settle(n,2,t)}return n}return this}return this.o=function(t){try{var o=t.v;1&t.s?_settle(n,1,e?e(o):o):r?_settle(n,1,r(o)):_settle(n,2,o)}catch(t){_settle(n,2,t)}},n},t}();function _isSettledPact(t){return t instanceof _Pact&&1&t.s}function _forTo(t,e,r){var n,o,i=-1;return function a(u){try{for(;++i<t.length&&(!r||!r());)if((u=e(i))&&u.then){if(!_isSettledPact(u))return void u.then(a,o||(o=_settle.bind(null,n=new _Pact,2)));u=u.v}n?_settle(n,1,u):n=u}catch(t){_settle(n||(n=new Pact),2,t)}}(),n}function _forOf(t,e,r){if("function"==typeof t[_iteratorSymbol]){var n,o,i,a=t[_iteratorSymbol]();if(function t(u){try{for(;!((n=a.next()).done||r&&r());)if((u=e(n.value))&&u.then){if(!_isSettledPact(u))return void u.then(t,i||(i=_settle.bind(null,o=new _Pact,2)));u=u.v}o?_settle(o,1,u):o=u}catch(t){_settle(o||(o=new Pact),2,t)}}(),a.return){var u=function(t){try{n.done||a.return()}catch(t){}return t};if(o&&o.then)return o.then(u,function(t){throw u(t)});u()}return o}if(!("length"in t))throw new TypeError("Object is not iterable");for(var l=[],c=0;c<t.length;c++)l.push(t[c]);return _forTo(l,function(t){return e(l[t])},r)}function _continue(t,e){return t&&t.then?t.then(e):e(t)}function _async(t){return function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];try{return Promise.resolve(t.apply(this,e))}catch(t){return Promise.reject(t)}}}function parseCode(t){return cheerio.load(t,{xmlMode:!0})}function createSymbol(t,e){var r=parseCode(e)("svg"),n=parseCode("<symbol/>");return n("symbol").append(r.children()).attr("id",t).attr("viewBox",r.attr("viewBox")),n.html()}function createSprite(t,e){var r=parseCode('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" />');return t.forEach(function(t){r("svg").append(t)}),e?htmlMinifier.minify(r.html(),{collapseWhitespace:!0}):pretty(r.html())}function svgicons(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.minify||!1,r=t.inputFolder||"src/icons",n=t.output||"dist/bundle.svg",o=t.svgo&&new SVGO(t.svgo);return{name:"svelte-svgicons",generateBundle:_async(function(){var t=[],i=path.resolve(r);return _continue(_forOf(fs.readdirSync(i),function(e){var r=path.parse(e).name,n=path.join(i,e),a=fs.readFileSync(n,"utf8");return _invoke(function(){if(o)return _await(o.optimize(a,{path:n}),function(t){var e=t.data;a=e})},function(){t=[].concat(_toConsumableArray(t),[createSymbol(r,a)])})}),function(){t.length>0&&fs.writeFileSync(path.resolve(n),createSprite(t,e))})})}}module.exports=svgicons;
//# sourceMappingURL=index.min.js.map