in the snippet like this:
gulp.task \"coffee\", ->
gulp.src(\"src/server/**/*.coffee\")
.pipe(coffee {bare: true}).on(\"error\",gutil.log)
According to the Gulp docs:
Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints: take in a callback or return a promise or event stream.
To run your sequence of tasks synchronously:
gulp.src
) to gulp.task
to inform
the task of when the stream ends.gulp.task
.See the revised code:
gulp.task "coffee", ->
return gulp.src("src/server/**/*.coffee")
.pipe(coffee {bare: true}).on("error",gutil.log)
.pipe(gulp.dest "bin")
gulp.task "clean", ['coffee'], ->
return gulp.src("bin", {read:false})
.pipe clean
force:true
gulp.task 'develop',['clean','coffee'], ->
console.log "run something else"
Try this hack :-) Gulp v3.x Hack for Async bug
I tried all of the "official" ways in the Readme, they didn't work for me but this did. You can also upgrade to gulp 4.x but I highly recommend you don't, it breaks so much stuff. You could use a real js promise, but hey, this is quick, dirty, simple :-) Essentially you use:
var wait = 0; // flag to signal thread that task is done
if(wait == 0) setTimeout(... // sleep and let nodejs schedule other threads
Check out the post!
run-sequence is the most clear way (at least until Gulp 4.0 is released)
With run-sequence, your task will look like this:
var sequence = require('run-sequence');
/* ... */
gulp.task('develop', function (done) {
sequence('clean', 'coffee', done);
});
But if you (for some reason) prefer not using it, gulp.start
method will help:
gulp.task('develop', ['clean'], function (done) {
gulp.on('task_stop', function (event) {
if (event.task === 'coffee') {
done();
}
});
gulp.start('coffee');
});
Note: If you only start task without listening to result, develop
task will finish earlier than coffee
, and that may be confusing.
You may also remove event listener when not needed
gulp.task('develop', ['clean'], function (done) {
function onFinish(event) {
if (event.task === 'coffee') {
gulp.removeListener('task_stop', onFinish);
done();
}
}
gulp.on('task_stop', onFinish);
gulp.start('coffee');
});
Consider there is also task_err
event you may want to listen to.
task_stop
is triggered on successful finish, while task_err
appears when there is some error.
You may also wonder why there is no official documentation for gulp.start()
. This answer from gulp member explains the things:
gulp.start
is undocumented on purpose because it can lead to complicated build files and we don't want people using it
(source: https://github.com/gulpjs/gulp/issues/426#issuecomment-41208007)
By default, gulp runs tasks simultaneously, unless they have explicit dependencies. This isn't very useful for tasks like clean
, where you don't want to depend, but you need them to run before everything else.
I wrote the run-sequence plugin specifically to fix this issue with gulp. After you install it, use it like this:
var runSequence = require('run-sequence');
gulp.task('develop', function(done) {
runSequence('clean', 'coffee', function() {
console.log('Run something else');
done();
});
});
You can read the full instructions on the package README — it also supports running some sets of tasks simultaneously.
Please note, this will be (effectively) fixed in the next major release of gulp, as they are completely eliminating the automatic dependency ordering, and providing tools similar to run-sequence
to allow you to manually specify run order how you want.
However, that is a major breaking change, so there's no reason to wait when you can use run-sequence
today.
I was having this exact same problem and the solution turned out to be pretty easy for me. Basically change your code to the following and it should work. NOTE: the return before gulp.src made all the difference for me.
gulp.task "coffee", ->
return gulp.src("src/server/**/*.coffee")
.pipe(coffee {bare: true}).on("error",gutil.log)
.pipe(gulp.dest "bin")
gulp.task "clean",->
return gulp.src("bin", {read:false})
.pipe clean
force:true
gulp.task 'develop',['clean','coffee'], ->
console.log "run something else"
I generated a node/gulp app using the generator-gulp-webapp Yeoman generator. It handled the "clean conundrum" this way (translating to the original tasks mentioned in the question):
gulp.task('develop', ['clean'], function () {
gulp.start('coffee');
});