0

From this StackOverflow post, I learned that it is generally not a good idea not to return anything in a gulp task. I want the caller to wait for the asynchronous tasks.

This is what my gulpfile.js looks like currently:

'use strict';

var gulp = require('gulp'),
    uglify = require('gulp-uglify'),
    livereload = require('gulp-livereload'),
    del = require('del'),
    util = require('gulp-util'),

    paths = {
        scripts: {
            main: 'app.js',
            controllers: 'controllers/*.js',
            services: 'services/*.js'
        },
        css: 'css/*.css',
        html: {
            index: 'index.html',
            views: 'views/*.html',
            directives: 'directives/*.html'
        },
        bower_components: 'bower_components/*.*'
    };

gulp.task('build-js', function() {
    var destination;

    for (var key in paths.scripts) {
        util.log('Building ' + key);

        if (/\*\.js$/.test(paths.scripts[key])) { // just so I catch app.js correctly in the paths
            destination = 'build/' + paths.scripts[key].slice(0, -4);
        } else {
            destination = 'build/';
        }

        gulp.src(paths.scripts[key])
            .pipe(uglify())
            .pipe(gulp.dest(destination));
    }

    util.log('returning..');

    return;

});

I am not sure what should I be returning in my build-js task.

What is the best practice to do this? I do not want to install another module to resolve this.

Can anyone please point me in the right direction?

Community
  • 1
  • 1
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138

1 Answers1

0

Gulp will iterate over all the files for you without needing a loop. If you want to do conditional stuff then use gulp-if. Here's the basic idea:

gulp.task('build-js', function() {
    var destination;

    return gulp.src(paths.scripts)
            //.pipe(gulp-if(....))
            .pipe(uglify())
            .pipe(gulp.dest(destination));
    }

});
Simon H
  • 20,332
  • 14
  • 71
  • 128