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 committed Dec 11, 2017
1 parent efab0d4 commit bf03986
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 @@ -44,7 +44,8 @@
"babel-traverse": "^6.15.0",
"babylon": "^6.10.0",
"lodash.isplainobject": "^4.0.6",
"promise-synchronizer": "^1.0.6",
"resolve-from": "^2.0.0",
"svgo": "^0.7.0"
"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 @@ -54,15 +55,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 bf03986

Please sign in to comment.