Galan小屋

welcome my blog

tinypng--批量压缩图片

By galan99

发表于 2019-03-20

使用文档

安装依赖包

npm install --save-dev gulp-tinypng-nokey

配合gulp使用

在目录下新建gulpfile.js,然后写入以下代码,执行gulp tinypng命令压缩


var gulp = require('gulp');
var tiny = require('gulp-tinypng-nokey');
let date = new Date()
let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
let now = h + '' + m + '' + s

gulp.task('tinypng', function(cb) {
	gulp.src('img/*')
			.pipe(tiny())
			.pipe(gulp.dest('dist/'+now));
});