mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
Promisify SVGO
This commit is contained in:
@ -12,7 +12,7 @@ FS.readFile(filepath, 'utf8', function(err, data) {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
svgo.optimize(data, function(result) {
|
svgo.optimize(data).then(function(result) {
|
||||||
|
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|
||||||
|
53
lib/svgo.js
53
lib/svgo.js
@ -17,44 +17,42 @@ var CONFIG = require('./svgo/config.js'),
|
|||||||
JS2SVG = require('./svgo/js2svg.js');
|
JS2SVG = require('./svgo/js2svg.js');
|
||||||
|
|
||||||
var SVGO = module.exports = function(config) {
|
var SVGO = module.exports = function(config) {
|
||||||
|
|
||||||
this.config = CONFIG(config);
|
this.config = CONFIG(config);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SVGO.prototype.optimize = function(svgstr, callback) {
|
SVGO.prototype.optimize = function(svgstr) {
|
||||||
if (this.config.error) return callback(this.config);
|
return new Promise((resolve, reject) => {
|
||||||
|
if (this.config.error) {
|
||||||
|
reject(this.config.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var _this = this,
|
var config = this.config,
|
||||||
config = this.config,
|
maxPassCount = config.multipass ? 10 : 1,
|
||||||
maxPassCount = config.multipass ? 10 : 1,
|
counter = 0,
|
||||||
counter = 0,
|
prevResultSize = Number.POSITIVE_INFINITY,
|
||||||
prevResultSize = Number.POSITIVE_INFINITY,
|
optimizeOnceCallback = (svgjs) => {
|
||||||
optimizeOnceCallback = function(svgjs) {
|
if (svgjs.error) {
|
||||||
|
reject(svgjs.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (svgjs.error) {
|
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
|
||||||
callback(svgjs);
|
prevResultSize = svgjs.data.length;
|
||||||
return;
|
this._optimizeOnce(svgjs.data, optimizeOnceCallback);
|
||||||
}
|
} else {
|
||||||
|
resolve(svgjs);
|
||||||
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
|
}
|
||||||
prevResultSize = svgjs.data.length;
|
};
|
||||||
_this._optimizeOnce(svgjs.data, optimizeOnceCallback);
|
|
||||||
} else {
|
|
||||||
callback(svgjs);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
_this._optimizeOnce(svgstr, optimizeOnceCallback);
|
|
||||||
|
|
||||||
|
this._optimizeOnce(svgstr, optimizeOnceCallback);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
SVGO.prototype._optimizeOnce = function(svgstr, callback) {
|
SVGO.prototype._optimizeOnce = function(svgstr, callback) {
|
||||||
var config = this.config;
|
var config = this.config;
|
||||||
|
|
||||||
SVG2JS(svgstr, function(svgjs) {
|
SVG2JS(svgstr, function(svgjs) {
|
||||||
|
|
||||||
if (svgjs.error) {
|
if (svgjs.error) {
|
||||||
callback(svgjs);
|
callback(svgjs);
|
||||||
return;
|
return;
|
||||||
@ -63,7 +61,6 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) {
|
|||||||
svgjs = PLUGINS(svgjs, config.plugins);
|
svgjs = PLUGINS(svgjs, config.plugins);
|
||||||
|
|
||||||
callback(JS2SVG(svgjs, config.js2svg));
|
callback(JS2SVG(svgjs, config.js2svg));
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,7 +71,5 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) {
|
|||||||
* @returns {JSAPI} content item
|
* @returns {JSAPI} content item
|
||||||
*/
|
*/
|
||||||
SVGO.prototype.createContentItem = function(data) {
|
SVGO.prototype.createContentItem = function(data) {
|
||||||
|
|
||||||
return new JSAPI(data);
|
return new JSAPI(data);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -285,12 +285,7 @@ function optimizeFromString(svgstr, config, datauri, input, output) {
|
|||||||
outBytes,
|
outBytes,
|
||||||
svgo = new SVGO(config);
|
svgo = new SVGO(config);
|
||||||
|
|
||||||
svgo.optimize(svgstr, function(result) {
|
svgo.optimize(svgstr).then(function(result) {
|
||||||
if (result.error) {
|
|
||||||
console.error(result.error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (datauri) {
|
if (datauri) {
|
||||||
result.data = encodeSVGDatauri(result.data, datauri);
|
result.data = encodeSVGDatauri(result.data, datauri);
|
||||||
}
|
}
|
||||||
@ -314,7 +309,7 @@ function optimizeFromString(svgstr, config, datauri, input, output) {
|
|||||||
|
|
||||||
saveFileAndPrintInfo(config, result.data, output, inBytes, outBytes, time);
|
saveFileAndPrintInfo(config, result.data, output, inBytes, outBytes, time);
|
||||||
}
|
}
|
||||||
});
|
}, error => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveFileAndPrintInfo(config, data, path, inBytes, outBytes, time) {
|
function saveFileAndPrintInfo(config, data, path, inBytes, outBytes, time) {
|
||||||
@ -439,12 +434,7 @@ function optimizeFolder(dir, config, output) {
|
|||||||
inBytes = Buffer.byteLength(data, 'utf8'),
|
inBytes = Buffer.byteLength(data, 'utf8'),
|
||||||
outBytes;
|
outBytes;
|
||||||
|
|
||||||
svgo.optimize(data, function(result) {
|
svgo.optimize(data).then(function(result) {
|
||||||
if (result.error) {
|
|
||||||
console.error(result.error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
outBytes = Buffer.byteLength(result.data, 'utf8');
|
outBytes = Buffer.byteLength(result.data, 'utf8');
|
||||||
time = Date.now() - startTime;
|
time = Date.now() - startTime;
|
||||||
|
|
||||||
@ -482,7 +472,7 @@ function optimizeFolder(dir, config, output) {
|
|||||||
optimizeFile(files[i]);
|
optimizeFile(files[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}, error => console.error(error));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
//move on to the next file
|
//move on to the next file
|
||||||
|
@ -44,9 +44,8 @@ describe('plugins tests', function() {
|
|||||||
js2svg : { pretty: true }
|
js2svg : { pretty: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
svgo.optimize(orig, function(result) {
|
svgo.optimize(orig).then(function(result) {
|
||||||
|
//FIXME: results.data has a '\n' at the end while it should not
|
||||||
//FIXME: results.data has a '\n' at the end while it should not
|
|
||||||
normalize(result.data).should.be.equal(should);
|
normalize(result.data).should.be.equal(should);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
@ -30,7 +30,7 @@ describe('indentation', function() {
|
|||||||
js2svg : { pretty: true, indent: 2 }
|
js2svg : { pretty: true, indent: 2 }
|
||||||
});
|
});
|
||||||
|
|
||||||
svgo.optimize(orig, function(result) {
|
svgo.optimize(orig).then(function(result) {
|
||||||
normalize(result.data).should.be.equal(should);
|
normalize(result.data).should.be.equal(should);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user