2

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

});
Colin Marshall
  • 2,142
  • 2
  • 21
  • 31

2 Answers2

3

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'));
});
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Thank you! This helps a lot, I'm new to gulp and the tutorial I did on it didn't return anything in the tasks. – Colin Marshall Jun 26 '15 at 03:04
  • 1
    Returning the stream is one way to make your tasks asynchronous. You should make your tasks asynchronous if you ever want to use them as dependencies in other tasks. See https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support – Phil Jun 26 '15 at 03:07
  • Thank you for explaining that! – Colin Marshall Jun 26 '15 at 03:25
0

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.

just_a_dude
  • 2,745
  • 2
  • 26
  • 30
  • I edited/reworded my question for clarity. I want to have the concatenated scripts output to two files: all.js and all.min.js. For some reason all.js ends up minified too. – Colin Marshall Jun 26 '15 at 02:14