Skip to content

Commit

Permalink
Updating to svgo ^1.0.3 and synchronizing promise
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian authored and ljharb committed May 18, 2018
1 parent 7dc34fa commit 15bc6d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"babel-traverse": "^6.26.0",
"babylon": "^6.18.0",
"lodash.isplainobject": "^4.0.6",
"promise-synchronizer": "^1.0.6",
"resolve-from": "^2.0.0",
"svgo": "^0.7.2"
"svgo": "^1.0.3"
}
}
23 changes: 15 additions & 8 deletions src/optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// for the babylon JSX parser to work
import Svgo from 'svgo';
import isPlainObject from 'lodash.isplainobject';
import sync from 'promise-synchronizer';

const essentialPlugins = ['removeDoctype', 'removeComments'];

Expand Down Expand Up @@ -56,15 +57,21 @@ export default function optimize(content, opts = {}) {
validateAndFix(opts);
const svgo = new Svgo(opts);

// Svgo isn't _really_ async, so let's do it this way:
// Svgo isn't _really_ async, it however is async enough to require some handling:
let returnValue;
svgo.optimize(content, (response) => {
if (response.error) {
returnValue = response.error;
} else {
returnValue = response.data;
}
});
try {
sync(svgo.optimize(content)
.then((response) => {
if (response.error) {
throw response.error;
} else {
returnValue = response.data;
}
})
);
} catch (error) {
returnValue = error;
}

return returnValue;
}

0 comments on commit 15bc6d0

Please sign in to comment.