Exemplo n.º 1
0
	return through.obj(function (file, enc, cb) {
		var ext = path.extname(file.path).slice(1).toLowerCase();

		if (file.isNull() || ['png', 'jpg', 'jpeg', 'tif', 'tiff', 'webp'].indexOf(ext) === -1) {
			cb(null, file);
			return;
		}

		if (file.isStream()) {
			cb(new gutil.PluginError('gulp-webp', 'Streaming not supported'));
			return;
		}

		exec
			.use(webp, args.concat([exec.src(), '-o', exec.dest()]))
			.run(file.contents, function (err, buf) {
				if (err) {
					cb(new gutil.PluginError('gulp-webp', err, {fileName: file.path}));
					return;
				}

				file.contents = buf;
		
				if(options.append) {
					file.path = file.path + '.webp';
				}
				else{
					file.path = gutil.replaceExtension(file.path, '.webp');
				}

				cb(null, file);
			});
	});
Exemplo n.º 2
0
	return through.ctor({objectMode: true}, function (file, enc, cb) {
		if (file.isNull()) {
			cb(null, file);
			return;
		}

		if (file.isStream()) {
			cb(new Error('Streaming is not supported'));
			return;
		}

		if (!isPng(file.contents)) {
			cb(null, file);
			return;
		}

		var execBuffer = new ExecBuffer();
		var args = ['-brute', '-force', '-q'];

		if (opts.reduce) {
			args.push('-reduce');
		}

		execBuffer
			.use(pngcrush, args.concat([execBuffer.src(), execBuffer.dest()]))
			.run(file.contents, function (err, buf) {
				if (err) {
					err.fileName = file.path;
					cb(err);
					return;
				}

				if (buf.length < file.contents.length) {
					file.contents = buf;
				}

				cb(null, file);
			});
	});