From 15bc6d0900ffc22ed393369abec445249b9cbde7 Mon Sep 17 00:00:00 2001 From: fabian Date: Mon, 11 Dec 2017 15:25:04 +0100 Subject: [PATCH] Updating to svgo ^1.0.3 and synchronizing promise --- package.json | 3 ++- src/optimize.js | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 46c1bdb..bbc31a9 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/optimize.js b/src/optimize.js index 08c86e0..f432bea 100644 --- a/src/optimize.js +++ b/src/optimize.js @@ -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']; @@ -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; }