Search code examples
javascriptnode.jsgulpgulp-imagemin

Replace files in sub folders with gulp / imagemin


I've folder structure as

assets/images/foo/abc.jpg
assets/images/bar/def.jpg 
..

I want to imagemin them and place the images in the same sub folders, with gulp, how to do?

var paths = {
    images: 'assets/images/**/*'
};

gulp.task('images', function () {
    return gulp.src(paths.images)
        .pipe(imagemin({optimizationLevel: 5}))
        .pipe(gulp.dest(???));
});

What should I put in the gulp.dest?


Solution

  • Have a look at this future recipe :

    https://github.com/arvindr21/gulp/blob/master/docs/recipes/maintain-directory-structure-while-globbing.md

    If you want to maintain the structure, you need to pass {base: '.'} to gulp.src().