Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating to svgo ^1.0.3 and synchronizing promise #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Collaborator

@ljharb ljharb Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a package i'm interested in using; making async things sync is a very dangerous choice to make in JS.

"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;
}