Gulp run alternative

user1995781 picture user1995781 · Mar 3, 2015 · Viewed 7.3k times · Source

Everytime I run gulp, I see this message gulp.run() has been deprecated. Use task dependencies or gulp.watch task triggering instead.

Example code:

var watch = require('gulp-watch');
watch(['public/**/*.js','!public/**/*.min.js'],function(){
    gulp.run('compressjs');
    gulp.run('bs-reload');
});

How can I avoid using gulp.run() with gulp-watch?

Answer

the_5imian picture the_5imian · May 21, 2015

You shouldn't use run. Here is an alternative (to address that part of your answer), but not what you need to do:

gulp
    .start('default')
    .once('task_stop', function(){
        //do other stuff.
     });

If you really must fire an ad hoc task, but can literally use run...You can use .start with the task name, and also attach to the task_stop handler to fire something when the task is complete. This is nice when writing tests for gulp tasks, but that's really it.

however in day to day gulp usage, this is an antipattern.

Normally, you build smaller tasks and composite them. This is the right way. See this:

var gulp = require('gulp'),
    runSequence = require('run-sequence');

function a(){
  //gulpstuff
} 
function b(){
  //gulpstuff
}

function d(callback){
  runSequence('a', 'b', callback)
}

gulp
    .task('a', a) // gulp a -runs a
    .task('b', b) // gulp b runs b
    .task('c', ['a', 'b']) //gulp c runs a and b at the same time
    .task('d', d); //gulp d runs a, then b.

basically if c or d was a watch task, you'd achieve the same goal of firing the already registered smaller gulp tasks without .run