Gulp appending to files, not overwriting
Asked Answered
P

1

6

I'm trying to concatenate my JS files and run them through Babel for a new project, but instead of overwriting the destination file on each task run, my gulpfile only appends changes to the file. So my destination file ends up looking like this:

console.log('hello');

//# sourceMappingURL=app.js.map
console.log('goodbye');

//# sourceMappingURL=app.js.map

What am I missing? Below is my gulpfile.

Thanks in advance.

var gulp        = require('gulp');
var sourcemaps  = require("gulp-sourcemaps");
var uglify      = require('gulp-uglify');
var rename      = require('gulp-rename');
var concat      = require('gulp-concat');
var babel       = require('gulp-babel');    
var browserSync = require('browser-sync').create();
var reload      = browserSync.reload;

gulp.task('js', function(){
  return gulp.src("./app/js/*.js")
    .pipe(sourcemaps.init())
    .pipe(concat("app.js"))
    .pipe(babel())
    .pipe(sourcemaps.write("."))
    .pipe(gulp.dest("./app/js/"));  
});

gulp.task('js-reload', ['js'], reload);

gulp.task('serve', ['js'], function() {
    browserSync.init({
        server: "./app"
    });
    gulp.watch("./app/js/*.js").on('change', ['js-reload']);
    gulp.watch("./app/*.html").on('change', reload);
});

gulp.task('default', ['js', 'serve']);
Pelting answered 24/5, 2015 at 23:51 Comment(1)
If I remember correct, you have to add ./ to overwrite files.Grizzle
M
3

You're reading and writing to the same destination directory. Therefore the file app.js is first read, some stuff is added to it, and then the result is written to app.js, causing this appending behaviour. You should output to a different directory than you are reading from.

Massey answered 25/5, 2015 at 0:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.