如何将 gulp.dest() 设置在与管道输入相同的目录中? [英] How to set gulp.dest() in same directory as pipe inputs?
本文介绍了如何将 gulp.dest() 设置在与管道输入相同的目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要优化每个目录中找到的所有图像并将其记录到其中,而无需单独设置每个文件夹的路径.我不明白怎么做.
I need all the found images in each of the directories to be optimized and recorded into them without setting the path to the each folder separately. I don't understand how to make that.
var gulp = require('gulp');
var imageminJpegtran = require('imagemin-jpegtran');
gulp.task('optimizeJpg', function () {
return gulp.src('./images/**/**/*.jpg')
.pipe(imageminJpegtran({ progressive: true })())
.pipe(gulp.dest('./'));
});
推荐答案
这里有两个答案.
第一:它更长,灵活性较差,需要额外的模块,但它的运行速度提高了 20%,并为您提供了每个文件夹的日志.
Here are two answers.
First: It is longer, less flexible and needs additional modules, but it works 20% faster and gives you logs for every folder.
var merge = require('merge-stream');
var folders =
[
"./pictures/news/",
"./pictures/product/original/",
"./pictures/product/big/",
"./pictures/product/middle/",
"./pictures/product/xsmall/",
...
];
gulp.task('optimizeImgs', function () {
var tasks = folders.map(function (element) {
return gulp.src(element + '*')
.pipe(sometingToDo())
.pipe(gulp.dest(element));
});
return merge(tasks);
});
第二种解决方案:灵活优雅,但速度较慢.我更喜欢它.
Second solution: It's flexible and elegant, but slower. I prefer it.
return gulp.src('./pictures/**/*')
.pipe(somethingToDo())
.pipe(gulp.dest(function (file) {
return file.base;
}));
这篇关于如何将 gulp.dest() 设置在与管道输入相同的目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文