seinjs-png-compress-processor
Version:
Compress png files with webpack loaders for Sein.js.
54 lines (53 loc) • 1.87 kB
JavaScript
;
/**
* @File : index.ts
* @Author : dtysky (dtysky@outlook.com)
* @Date : 11/18/2019, 3:54:07 PM
* @Description:
*/
var PNG = require("upng-js");
function debug(msg) {
if (process.env.SEINJS_LOADER_DEBUG) {
console.log('seinjs-png-compress-processor', msg);
}
}
module.exports = /** @class */ (function () {
function SeinJSPNGCompressProcessor(options) {
this._psize = 256;
if (options) {
this.test = options.test || /\.png/g;
this._psize = options.psize || 256;
this._custom = options.custom || null;
}
}
SeinJSPNGCompressProcessor.prototype.process = function (options) {
var _this = this;
return new Promise(function (resolve) {
var data = options.data, filePath = options.filePath;
if (!(data instanceof Buffer)) {
data = new Buffer(data);
}
var cpOptions = { psize: _this._psize, skip: false };
if (_this._custom) {
Object.assign(cpOptions, _this._custom(filePath, data) || {});
}
debug(filePath + ", " + JSON.stringify(cpOptions));
if (cpOptions.skip) {
console.log("PNG Compress skip: " + filePath);
}
else {
try {
var png = PNG.decode(data);
var res = PNG.encode([PNG.toRGBA8(png)[0]], png.width, png.height, cpOptions.psize);
data = Buffer.from(res);
console.log("PNG Compress success: " + filePath);
}
catch (error) {
console.error("PNG Compress error: file " + filePath + ", " + error.message);
}
}
resolve(data);
});
};
return SeinJSPNGCompressProcessor;
}());