本文介绍了如何在与管道输入相同的目录中设置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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:25