Skip to content

Commit

Permalink
fix: Remove unecessary whitespace from SVG contents
Browse files Browse the repository at this point in the history
```js
/* BEFORE */
{ circle: "\n  <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\n" }

/* AFTER */
{ circle: "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle>" }
```
  • Loading branch information
Cole Bemis committed Nov 20, 2017
1 parent b814a2b commit c9552b6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bin/__tests__/build-icons-object.test.js
Expand Up @@ -3,9 +3,9 @@ import buildIconsObject from '../build-icons-object';

const SVG_FILES = {
'icon1.svg':
'<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>',
'<svg\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n>\n <line x1="23" y1="1" x2="1" y2="23" />\n <line x1="1" y1="1" x2="23" y2="23" />\n</svg>',
'icon2.svg':
'<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"><circle cx="12" cy="12" r="11" /></svg>',
'<svg\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n>\n <circle cx="12" cy="12" r="11" />\n</svg>',
};

function getSvg(svgFile) {
Expand Down
3 changes: 2 additions & 1 deletion bin/build-icons-object.js
@@ -1,6 +1,7 @@
/* eslint-disable import/no-extraneous-dependencies */
import path from 'path';
import cheerio from 'cheerio';
import { minify } from 'html-minifier';

/**
* Build an object in the format: `{ <name>: <contents> }`.
Expand Down Expand Up @@ -28,7 +29,7 @@ function buildIconsObject(svgFiles, getSvg) {
*/
function getSvgContents(svg) {
const $ = cheerio.load(svg);
return $('svg').html();
return minify($('svg').html(), { collapseWhitespace: true });
}

export default buildIconsObject;
110 changes: 110 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -31,6 +31,7 @@
"eslint": "^4.0.0",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-plugin-import": "^2.5.0",
"html-minifier": "^3.5.6",
"husky": "^0.13.4",
"jest": "^21.2.1",
"npm-run-all": "^4.1.2",
Expand Down

0 comments on commit c9552b6

Please sign in to comment.