Home > Software design >  How to omit all sub directories when using gulp.dest()?
How to omit all sub directories when using gulp.dest()?

Time:05-10

I've got a task in Gulp:

// converts jpg/png images to webp
function webpImage() {
    return src('src/**/*.{jpg,png}')
        .pipe(imagewebp())
        .pipe(dest('dist/images'));
}

All I want is to put all the new .webp images directly into dist/images. I don't want any sub folders, paths like this: dist/images/header/header.jpg. I need it to be like this: /dist/images/header.jpg

I've tried to do something with the base option of src() and it didn't work out because I don't know how to get full path of located image.

CodePudding user response:

Look at the gulp-flatten package. I haven't used it for a while so make sure it works with gulp v4. It "flattens" the directory structure of the files passing through to eliminate as many parent directories as you need.

// other requires
var flatten = require('gulp-flatten');

function webpImage() {
    return src('src/**/*.{jpg,png}')
        .pipe(imagewebp())
        .pipe(flatten())
        .pipe(dest('dist/images'));
}
  • Related