build: Add process-svgs script (#216)

This commit is contained in:
Cole Bemis
2017-11-13 08:13:11 -08:00
committed by GitHub
parent f2b7e1f313
commit 0dc2bf5c9d
249 changed files with 5198 additions and 890 deletions

View File

@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`processes SVG correctly 1`] = `
"<svg
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\\"
>
<line x1=\\"23\\" y1=\\"1\\" x2=\\"1\\" y2=\\"23\\" />
<line x1=\\"1\\" y1=\\"1\\" x2=\\"23\\" y2=\\"23\\" />
</svg>
"
`;
exports[`rejects when passed unparsable SVG string 1`] = `
[Error: Error in parsing SVG: Unclosed root tag
Line: 0
Column: 10
Char: ]
`;

View File

@@ -0,0 +1,15 @@
/* eslint-env jest */
import processSvg from '../process-svg';
test('processes SVG correctly', () => {
const SVG =
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><title>Title</title><line x1="23" y1="1" x2="1" y2="23" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/><line x1="1" y1="1" x2="23" y2="23" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/></svg>';
expect(processSvg(SVG)).resolves.toMatchSnapshot();
});
test('rejects when passed unparsable SVG string', () => {
const UNPARSABLE_SVG = '<svg></svg';
expect(processSvg(UNPARSABLE_SVG)).rejects.toMatchSnapshot();
});

59
bin/process-svg.js Normal file
View File

@@ -0,0 +1,59 @@
/* eslint-disable import/no-extraneous-dependencies */
import Svgo from 'svgo';
import cheerio from 'cheerio';
import { format } from 'prettier';
import DEFAULT_ATTRIBUTES from '../src/default-attributes.json';
/**
* Process SVG string.
* @param {string} svg - An SVG string.
* @param {Promise<string>}
*/
function processSvg(svg) {
return (
optimize(svg)
.then(setAttributes)
.then(format)
// remove semicolon inserted by prettier
// because prettier thinks it's formatting JSX not HTML
.then(svg => svg.replace(/;/g, ''))
);
}
/**
* Optimize SVG with `svgo`.
* @param {string} svg - An SVG string.
* @returns {Promise<string>}
*/
function optimize(svg) {
const svgo = new Svgo({
plugins: [
{ convertShapeToPath: false },
{ mergePaths: false },
{ removeAttrs: { attrs: '(fill|stroke.*)' } },
{ removeTitle: true },
],
});
return new Promise(resolve => {
svgo.optimize(svg, ({ data }) => resolve(data));
});
}
/**
* Set default attibutes on SVG.
* @param {string} svg - An SVG string.
* @returns {string}
*/
function setAttributes(svg) {
const $ = cheerio.load(svg);
Object.keys(DEFAULT_ATTRIBUTES).forEach(key =>
$('svg').attr(key, DEFAULT_ATTRIBUTES[key]),
);
return $('body').html();
}
export default processSvg;

15
bin/process-svgs.js Normal file
View File

@@ -0,0 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import fs from 'fs';
import path from 'path';
import processSvg from './process-svg';
const ICONS_DIR = path.resolve(__dirname, '../icons');
fs
.readdirSync(ICONS_DIR)
.filter(file => path.extname(file) === '.svg')
.forEach(svgFile => {
const svg = fs.readFileSync(path.join(ICONS_DIR, svgFile));
processSvg(svg).then(svg => fs.writeFileSync(path.join(ICONS_DIR, svgFile), svg));
});