New setup for new NPM package

This commit is contained in:
Eric Fennis
2020-07-17 23:07:51 +02:00
parent d051a09232
commit de6906e242
19 changed files with 21787 additions and 9 deletions
+11
View File
@@ -0,0 +1,11 @@
{
"xmlns": "http://www.w3.org/2000/svg",
"width": 24,
"height": 24,
"viewBox": "0 0 24 24",
"fill": "none",
"stroke": "currentColor",
"stroke-width": 2,
"stroke-linecap": "round",
"stroke-linejoin": "round"
}
+32
View File
@@ -0,0 +1,32 @@
import DEFAULT_ATTRS from './default-attrs.json';
import { parseDOM } from 'htmlparser2';
export default (iconsObject) => {
const iconVnodes = {}
for(const icon in iconsObject) {
const svgString = iconsObject[icon];
const dom = parseDOM(svgString);
const children = [
...dom.map(element => ([
element.name,
{
...element.attribs,
}
])),
];
iconVnodes[icon] = [
'svg',
{
...DEFAULT_ATTRS,
},
children
]
}
return iconVnodes;
}
+35
View File
@@ -0,0 +1,35 @@
import path from 'path';
import cheerio from 'cheerio';
import { minify } from 'html-minifier';
/**
* Get contents between opening and closing `<svg>` tags.
* @param {string} svg
* @returns {string}
*/
function getSvgContents(svg) {
const $ = cheerio.load(svg);
return minify($('svg').html(), { collapseWhitespace: true });
}
/**
* Build an object in the format: `{ <name>: <contents> }`.
* @param {string[]} svgFiles - A list of filenames.
* @param {Function} getSvg - A function that returns the contents of an SVG file given a filename.
* @returns {Object}
*/
export default (svgFiles, getSvg) => {
return svgFiles
.map(svgFile => {
const name = path.basename(svgFile, '.svg');
const svg = getSvg(svgFile);
const contents = getSvgContents(svg);
return { name, contents };
})
.reduce((icons, icon) => {
icons[icon.name] = icon.contents;
return icons;
}, {});
}