gulpgulp-replace

gulp-replace creating an unwanted directory


I created this simple gulp task:

gulp.task('default', function () {
  gulp.src(['test.txt'])
    .pipe(replace('aaa', 'bbb'))
    .pipe(gulp.dest('result.txt'))
})

The test.txt source file stands in the same directory as the gulpfile.

The problem is that it's creating the resulting file in result.txt/test.txt.

How can I make it create this config.js file without this unwanted result.txt/ folder?


Solution

  • Per the documentation, gulp.dest takes a target directory, not a filename.

    If you want to put in the same directory as your gulpfile.js, then use .:

    gulp.task('default', function () {
      gulp.src(['test.txt'])
        .pipe(replace('aaa', 'bbb'))
        .pipe(gulp.dest('.'))
    });
    

    But beware as that will overwrite the original file! You can't rename a file with gulp.dest, for that you'll need to use another package, such as gulp-rename and doing something like this:

    gulp.task('default', function () {
      gulp.src(['test.txt'])
        .pipe(replace('aaa', 'bbb'))
        .pipe(rename('target.txt'))
        .pipe(gulp.dest('.'))
    });