Colin Marshall
Colin Marshall

Reputation: 2142

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

});

Upvotes: 2

Views: 1272

Answers (2)

Phil
Phil

Reputation: 164980

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

Upvotes: 3

just_a_dude
just_a_dude

Reputation: 2735

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.

Upvotes: 0

Related Questions