javascriptgulpgulp-uglifygulp-rename

gulp-uglify minifying script that it is not supposed to


When I run gulp scripts, both all.js and all.min.js end up minified. Anybody know why this happens?

gulp.task("scripts", function() {

    var concatted = gulp.src(['js/first.js', 'js/second.js'])
                        .pipe(concat('all.js'));

    // output concatenated scripts to js/all.js
    concatted.pipe(gulp.dest('js'));

    // minify concatenated scripts and output to js/all.min.js
    concatted.pipe(uglify())
             .pipe(rename('all.min.js')
             .pipe(gulp.dest('js'))

});

Solution

  • The problem is here

    // output concatenated scripts to js/all.js
    concatted.pipe(gulp.dest('js'));
    

    You aren't returning the modified stream into concatted.

    You could change this to

    // output concatenated scripts to js/all.js
    concatted = concatted.pipe(gulp.dest('js'));
    

    but this also works as expected

    gulp.task("scripts", function() {
        return gulp.src(['js/first.js', 'js/second.js'])
            .pipe(concat('all.js'))
            .pipe(gulp.dest('js'))
            .pipe(uglify())
            .pipe(rename('all.min.js')
            .pipe(gulp.dest('js'));
    });