简体   繁体   中英

Gulp: Convert cloned images to webp, but keep the original file extension

I have the following gulp worklow: I clone jpg and png images of a source folder, convert the cloned images to webp, and copy both (original and webp) to the dest folder.

So after I let it run, the dest folder contains the following files:

example.jpg
example.webp
foo.png
foo.webp

So far, so good. But what I would like to achieve ist that the original file extension (.jpg or.png) should be maintained in the webp version, and the.webp extension should simply be appended. So the dest folder should look like this:

example.jpg
example.jpg.webp
foo.png
foo.png.webp

How would that be possible in the following worklow?

import * as clone from "gulp-clone";
import * as size from "gulp-size";
import * as webp from "gulp-webp";

function copyImages() {
    var sink = clone.sink();  // init sink

    return src(conf.images.src + '**/*')
        .pipe(sink)        // clone image
        .pipe(webp())      // convert cloned image to webp
        .pipe(sink.tap())  // restore original image
        .pipe(dest(conf.images.dest))
        .pipe(size({
            title: 'images'
        }));
}

It's probably not the most efficient solution, but it works:

import * as clone from "gulp-clone";
import * as webp from "gulp-webp";
import * as revertPath from "gulp-revert-path";
import * as rename from "gulp-rename";
import * as size from "gulp-size";

function copyImages() {
    var sink = clone.sink();  // init sink

    return src(conf.images.src + '**/*')
        .pipe(sink)        // clone image
        .pipe(webp())      // convert cloned image to webp
        .pipe(revertPath())
        .pipe(rename(function (path) {
            path.extname = path.extname + '.webp';
        }))
        .pipe(sink.tap())  // restore original image
        .pipe(dest(conf.images.dest))
        .pipe(size({
            title: 'images'
        }));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM