gulpgulp-rename

Remove space from filename using Gulp Rename


I'm trying to create a Gulp task that replaces spaces from filename with underscores. Here's what I have:

gulp.task("underscore", function (done) {
  gulp.src("src/**/*.{jpg,jpeg}")
    .pipe(changed("dist"))

    //Remove Space
    .pipe(rename(function(opt) {
      opt.basename = opt.basename.replace(/ /, '_');
      return opt;
    }))

    //Copy to destination
    .pipe(gulp.dest("dest"));
    done();
});

It doesn't seem to reliably remove spaces. Is there something I'm missing? It seems to work about 99% of the time, which is good, but not really acceptable with the number of images I'm processing.


Solution

  • you can use this .split(' ').join('_') instead of .replace(/ /, '_')