Compress huge image files without suffix via Gulp

var gulp = require(‘gulp’);
const imagemin = require(‘gulp-imagemin’);
var once = require(‘async-once’);
var imageminMozjpeg = require(‘imagemin-mozjpeg’);
var rename = require(“gulp-rename”);
const del = require(‘del’);
var tap = require(“gulp-tap”);
var replaceName = require(‘gulp-replace-name’);
const source_dir = ‘img_source’;
const target_dir = ‘img_gulp’;
gulp.task(‘clean’, done => {
del(target_dir + ‘/*.jpg’); // returns a promise
done();
});
gulp.task(‘prepare’, done => {
del(target_dir + ‘/*’); // returns a promise
done();
});
gulp.task(‘rename’, done => {
gulp.src(source_dir + “/*”)
.pipe(rename(function (path) {
//path.dirname += “/ciao”;
//path.basename += “-change”;
path.extname = “.jpg”;
}))
.pipe(gulp.dest(target_dir));
done();
});
// command: gulp images
gulp.task(‘images’, done => {
// source path
gulp.src(target_dir + ‘/*.jpg’)
// compress image
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({
plugins: [
{removeViewBox: true},
{cleanupIDs: false}
]
}),
//jpg very light lossy, use vs jpegtran
imageminMozjpeg({
quality: 70
})
]))
// delete old jpg file
.pipe(tap(function(file, t) {
del(file.path);
}))
//rename/copy the old image file without suffix
.pipe(rename(function (path) {
//path.dirname += “/ciao”;
//path.basename += “-new”;
path.extname = “”;
}))
// save to new file
.pipe(gulp.dest(target_dir))
;
done();
});
gulp.task(‘default’, gulp.series(‘rename’, ‘images’, ‘clean’));

Leave a Reply

Your email address will not be published. Required fields are marked *