-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (63 loc) · 1.85 KB
/
index.js
File metadata and controls
81 lines (63 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
const Svgo = require('svgo');
const extend = require('extend-shallow');
let encodeSVGDatauri = null;
exports.name = 'svgo';
exports.inputFormat = ['svg', 'svgo'];
exports.outputFormat = 'svg';
exports.render = function(str, options, locals) {
let error;
let returnValue;
let done = false;
const svgo = new Svgo(extend({}, options, locals));
optimizeSyncCallback(svgo, str, undefined, (err, result) => {
error = err;
returnValue = result.data;
done = true;
});
if (!done) {
throw new Error('svgo did not complete sychronously');
}
if (error) {
throw error;
}
return returnValue;
};
exports.renderAsync = function(str, options, locals) {
const svgo = new Svgo(extend({}, options, locals));
return svgo.optimize(str).then(result => {
return result.data;
});
};
function optimizeSyncCallback(svgo, svgstr, info, cb) {
// Coppied from https://github.com/svg/svgo/blob/d6e462b679a11376694c948c81e5c984c3db8bae/lib/svgo.js#L24-L57
if (svgo.config.error) {
cb(svgo.config.error);
return;
}
const {config} = svgo;
const maxPassCount = config.multipass ? 10 : 1;
let counter = 0;
let prevResultSize = Number.POSITIVE_INFINITY;
function optimizeOnceCallback(svgjs) {
if (svgjs.error) {
cb(svgjs.error);
return;
}
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
prevResultSize = svgjs.data.length;
svgo._optimizeOnce(svgjs.data, info, optimizeOnceCallback);
} else {
if (config.datauri) {
encodeSVGDatauri =
encodeSVGDatauri || require('svgo/lib/svgo/tools').encodeSVGDatauri;
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
}
if (info && info.path) {
svgjs.path = info.path;
}
cb(null, svgjs);
}
}
svgo._optimizeOnce(svgstr, info, optimizeOnceCallback);
}