Skip to content

Commit 593b3bf

Browse files
authored
Update SVG optimization workflow (#1215)
* Update optimize svg script * Update tests * Update error message * Remove extra newline * Update test scripts * Optimize svgs
1 parent 4dd8062 commit 593b3bf

295 files changed

Lines changed: 328 additions & 4283 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,16 @@ jobs:
2727

2828
- name: Lint
2929
run: npm run lint
30+
31+
- name: Optimize SVGs
32+
run: |
33+
npm run optimize-svgs
34+
if git diff --quiet; then
35+
echo "All SVGs are optimized ✔︎"
36+
else
37+
echo "The following SVGs are not optimized:"
38+
git diff --name-only
39+
echo
40+
echo "Please run 'npm run optimize-svgs' and commit the changes"
41+
exit 1
42+
fi
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`optimizes 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>"`;
4+
5+
exports[`rejects when passed unparsable SVG string 1`] = `
6+
[Error: Error in parsing SVG: Unclosed root tag
7+
Line: 0
8+
Column: 10
9+
Char: ]
10+
`;

bin/__tests__/__snapshots__/process-svg.test.js.snap

Lines changed: 0 additions & 26 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-env jest */
2-
import processSvg from '../process-svg';
2+
import optimizeSvg from '../optimize-svg';
33

4-
test('processes SVG correctly', () => {
4+
test('optimizes SVG correctly', () => {
55
const SVG =
66
'<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>';
77

8-
expect(processSvg(SVG)).resolves.toMatchSnapshot();
8+
expect(optimizeSvg(SVG)).resolves.toMatchSnapshot();
99
});
1010

1111
test('rejects when passed unparsable SVG string', () => {
1212
const UNPARSABLE_SVG = '<svg></svg';
1313

14-
expect(processSvg(UNPARSABLE_SVG)).rejects.toMatchSnapshot();
14+
expect(optimizeSvg(UNPARSABLE_SVG)).rejects.toMatchSnapshot();
1515
});

bin/build.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#!/bin/bash
22

3-
# Process SVG files
4-
# npx babel-node bin/process-svgs.js
5-
63
# Create dist directory
74
npx rimraf dist
85
mkdir dist
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
import Svgo from 'svgo';
22
import cheerio from 'cheerio';
3-
import { format } from 'prettier';
43

54
import DEFAULT_ATTRS from '../src/default-attrs.json';
65

76
/**
8-
* Process SVG string.
7+
* Optimize SVG string.
98
* @param {string} svg - An SVG string.
109
* @returns {Promise<string>}
1110
*/
12-
function processSvg(svg) {
13-
return (
14-
optimize(svg)
15-
.then(setAttrs)
16-
.then(format)
17-
// remove semicolon inserted by prettier
18-
// because prettier thinks it's formatting JSX not HTML
19-
.then(svg => svg.replace(/;/g, ''))
20-
);
11+
function optimizeSvg(svg) {
12+
return svgo(svg).then(setAttrs);
2113
}
2214

2315
/**
24-
* Optimize SVG with `svgo`.
16+
* Run SVGO on SVG string.
2517
* @param {string} svg - An SVG string.
2618
* @returns {Promise<string>}
2719
*/
28-
function optimize(svg) {
29-
const svgo = new Svgo({
20+
function svgo(svg) {
21+
const s = new Svgo({
3022
plugins: [
3123
{ convertShapeToPath: false },
3224
{ mergePaths: false },
@@ -36,7 +28,7 @@ function optimize(svg) {
3628
});
3729

3830
return new Promise(resolve => {
39-
svgo.optimize(svg, ({ data }) => resolve(data));
31+
s.optimize(svg, ({ data }) => resolve(data));
4032
});
4133
}
4234

@@ -55,4 +47,4 @@ function setAttrs(svg) {
5547
return $('body').html();
5648
}
5749

58-
export default processSvg;
50+
export default optimizeSvg;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import fs from 'fs';
22
import path from 'path';
33

4-
import processSvg from './process-svg';
4+
import optimizeSvg from './optimize-svg';
55

66
const IN_DIR = path.resolve(__dirname, '../icons');
77

8-
console.log(`Processing SVGs in ${IN_DIR}...`);
8+
console.log(`Optimizing SVGs in ${IN_DIR}...`);
99

1010
fs.readdirSync(IN_DIR)
1111
.filter(file => path.extname(file) === '.svg')
1212
.forEach(svgFile => {
1313
const svg = fs.readFileSync(path.join(IN_DIR, svgFile));
14-
processSvg(svg).then(svg =>
14+
optimizeSvg(svg).then(svg =>
1515
fs.writeFileSync(path.join(IN_DIR, svgFile), svg),
1616
);
1717
});

icons/activity.svg

Lines changed: 1 addition & 13 deletions
Loading

icons/airplay.svg

Lines changed: 1 addition & 14 deletions
Loading

icons/alert-circle.svg

Lines changed: 1 addition & 15 deletions
Loading

0 commit comments

Comments
 (0)