简体   繁体   English

使用 gulp 将它们转换为 webp 后,如何删除常规(jpg、png)图像?

[英]How do I delete the regular(jpg, png ) images after converting them to webp usging gulp?

I'm new to gulp.我是 gulp 的新手。 Trying to optimize the images and convert them into webp format.尝试优化图像并将其转换为webp格式。 I was able to achieve that using gulp-webp .我能够使用gulp-webp实现这一目标。 But it seems there are now two version of the images inside my dist/img folder one is the original and a webp version.但现在我的dist/img文件夹中似乎有两个版本的图像,一个是原始版本,一个是 webp 版本。 So how do I get only the webp not the original one inside my dist/img folder?那么如何在dist/img文件夹中只获取 webp 而不是原始 webp 呢?

Here how my project directories look like:我的项目目录如下所示:

project
    |-dist
        |-css
        |-img
        |-js
    |-src
        |-css
        |-img
        |-js
    
    gulp.js
    ...

Here is webp conversion function:这里是webp转换function:

function webpImage() {
    return src('dist/img/**/*.{jpg,png}')
        .pipe(imagewebp())
        .pipe(dest('dist/img'))
}

I would first say you should not delete source file, it is ok to keep them.我首先要说你不应该删除源文件,保留它们是可以的。 What you want to do is having to different destinations.你想要做的是去不同的目的地。 One of which should be deployed (the compiled, minified and webp for example) and the other should be version and used in your cdci pipelines perhaps.其中一个应该被部署(例如编译、缩小和 webp),另一个应该是版本并且可能在你的 cdci 管道中使用。

But then, if you really want to remove the source file, you can use gulp-clean while being in a gulp script.但是,如果你真的想删除源文件,你可以在 gulp 脚本中使用 gulp -clean

Your gulp clean script could look like that:您的 gulp 清洁脚本可能如下所示:

const { src, task } = require('gulp');
const clean = require("gulp-clean");
const logger = require('node-color-log');

function cleanImagesTask() {
    const root = "path/to/images/you/want/to/delete";
    logger.color('yellow').log(`Clean images`);

    return src(root,{allowEmpty: true},{read: false}).pipe(clean({force:true}));
};

const cleanImagesFolder = task('clean:images', cleanImagesTask);
exports.cleanImagesFolder = cleanImagesFolder;

And if you want to deploy in a different dest, could use something similar to:如果你想部署在不同的 dest,可以使用类似的东西:

const { src, dest, task } = require( 'gulp' );
const logger = require('node-color-log');

function copyImagesToDest(callback) {
    const imagesSource = "path/to/your/images/**/*";
    const imagesDestination = "path/to/destination/";

    logger.color('green').log(`Copy images from ${artifactData} to: ${destination}`);

    return src(imagesSource)
            .pipe(dest(destination))
            .on('end', function () { 
                logger.color('green').log(`Copy to: ${destination}`);
                callback(); 
            });
};

const copyImages = task('copy:images', copyImagesToDest);
exports.copyImages = copyImages ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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