Gulp在转换之前检查WebP图像是否存在
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用Gulp软件包 gulp-if 或可能是其他其他文件来测试每个文件管道:
gulpif(“!**/*。WebP”,webp(),, gulp.dest('Onywebp'))
以上将测试如果当前文件是不是 a
.webp
文件,如果不是是真的)这些文件将转到webp()
,所有其他文件(即.webp
文件)将转到beall> bealth weebp
目录。因此,您可以看到这些文件没有通过webp()
管道。所有文件仍然转到最终
Gulp.dest
。它们是设置条件的其他方法,例如:
You can use the gulp package gulp-if or probably others to test each file in the pipeline:
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 towebp()
, all other files (i.e., the.webp
files) will go to theonlyWebp
directory. So you can see that those files are not going through thewebp()
pipe.All files still go to the final
gulp.dest
.They are other ways to set up the condition, like: