How to set gulp.dest() in same directory as pipe inputs?

前端 未结 3 1322
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 02:14

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

相关标签:
3条回答
  • 2020-12-13 02:21

    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;
        }));
    
    0 讨论(0)
  • 2020-12-13 02:35

    Here you go:

    gulp.task('optimizeJpg', function () {
    
        return gulp.src('./images/**/**/*.jpg')
            .pipe(imageminJpegtran({ progressive: true })())
            .pipe(gulp.dest('./images/'));
    });
    

    Gulp takes everything that's a wildcard or a globstar into its virtual file name. So all the parts you know you want to select (like ./images/) have to be in the destination directory.

    0 讨论(0)
  • 2020-12-13 02:47

    You can use the base parameter:

    gulp.task('uglify', function () {
      return gulp.src('./dist/**/*.js', { base: "." })
        .pipe(uglify())
        .pipe(gulp.dest('./'));
    });
    
    0 讨论(0)
提交回复
热议问题