Gulp在转换之前检查WebP图像是否存在

发布于 2025-02-13 19:23:22 字数 349 浏览 0 评论 0原文

我正在使用Gulp迭代图像目录,并创建每个包含的图像的WebP版本。

这效果很好,但是每次我运行构建过程时,都会重新转化每个图像。

如何获取该函数来检查每个文件的WebP版本是否已经存在?

    gulp.task('convert-webp', function () {
       return gulp.src('media/image/**/*')
          .pipe(webp())
          .pipe(gulp.dest(function (file) {
             return file.base;
          }));
    });

I am using gulp to iterate through my image directories and create a webp version of each of the images contained.

This works perfectly well but every time I run the build process it reconverts every image.

How can I get the function to check if a webp version of each file already exists before converting?

    gulp.task('convert-webp', function () {
       return gulp.src('media/image/**/*')
          .pipe(webp())
          .pipe(gulp.dest(function (file) {
             return file.base;
          }));
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2025-02-20 19:23:22

您可以使用Gulp软件包 gulp-if 或可能是其他其他文件来测试每个文件管道:

const gulpIF = require('gulp-if');

gulp.task('convert-webp', function () {
  return gulp.src('media/image/**/*')

       // use below for testing purposes
      .pipe(gulpIF("!**/*.webp", webp(), gulp.dest('onyWebp')))

      // use this version when satisfied it is working
      // .pipe(gulpIF("!**/*.webp", webp()))

    .pipe(gulp.dest(function (file) {
      return file.base;
    }));
});

gulpif(“!**/*。WebP”,webp(),, gulp.dest('Onywebp'))

以上将测试如果当前文件是不是 a .webp文件,如果不是是真的)这些文件将转到webp(),所有其他文件(即.webp文件)将转到beall> bealth weebp目录。因此,您可以看到这些文件没有通过webp()管道。

所有文件仍然转到最终Gulp.dest

它们是设置条件的其他方法,例如:

"!**/*.{png,jpg,bmp,jpeg,jpeg2,webp,svg}"   // exclude these
"*.{png,jpg}"                               // include these

You can use the gulp package gulp-if or probably others to test each file in the pipeline:

const gulpIF = require('gulp-if');

gulp.task('convert-webp', function () {
  return gulp.src('media/image/**/*')

       // use below for testing purposes
      .pipe(gulpIF("!**/*.webp", webp(), gulp.dest('onyWebp')))

      // use this version when satisfied it is working
      // .pipe(gulpIF("!**/*.webp", webp()))

    .pipe(gulp.dest(function (file) {
      return file.base;
    }));
});

gulpIF("!**/*.webp", webp(), gulp.dest('onyWebp'))

The above will test if the current file is not a .webp file, if it is not (so the condition is true) those files will go to webp(), all other files (i.e., the .webp files) will go to the onlyWebp directory. So you can see that those files are not going through the webp() pipe.

All files still go to the final gulp.dest.

They are other ways to set up the condition, like:

"!**/*.{png,jpg,bmp,jpeg,jpeg2,webp,svg}"   // exclude these
"*.{png,jpg}"                               // include these
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文