gulp-uglify minifying script that it is not supposed to

1.2k views Asked by At

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'))

});
2

There are 2 answers

2
Phil On BEST ANSWER

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'));
});
1
just_a_dude On

What's happening is that you're concatenating the two scripts

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

Before minifying them

concatted.pipe(uglify())
         .pipe(rename('all.min.js')
         .pipe(gulp.dest('js'))

One possible solution could be:

gulp.src('js/first.js')
    .pipe(uglify())
    .pipe(rename('all.min.js'))
    .pipe(gulp.dest('js'));

Hope it helps.