問題描述
我無法讓 gulp run-sequence
執行我給它的兩個函數(它只會執行其中一個.我有這樣的事情:
gulp.task('wire', function(){gulp.src('./index.html').pipe(wiredep()).pipe(gulp.dest('.'));});var filesToInject = ['./app/**/*.js', './app/**/*.css'];gulp.task('注入', function (){var target = gulp.src('./index.html');var sources = gulp.src(filesToInject, {read: false});返回target.pipe(注入(來源)).pipe(gulp.dest('.'));});gulp.task('wire-inject', function(callback){序列('wire','inject',回調);});
那只會運行 inject
任務.只有當我在 wire
任務中的 gulp.src
之前添加一個 return
時,它才會執行.我還在兩個函數參數中添加了回調,并將其傳遞給每個 文檔的兩個任務中的最后一個管道一個>.
gulp.task('wire', function(callback){gulp.src('./index.html').pipe(wiredep()).pipe(gulp.dest('.'), 回調);});var filesToInject = ['./app/**/*.js', './app/**/*.css'];gulp.task('inject', 函數(回調){var target = gulp.src('./index.html');var sources = gulp.src(filesToInject, {read: false});返回target.pipe(注入(來源)).pipe(gulp.dest('.'), 回調);});
這并沒有引發任何錯誤,但是如果我把它拿出來它不會改變任何東西.這個 return 語句做了什么神奇地使我的序列完全運行.文檔中的這些回調是什么,我只是通過不帶括號的引用來執行它們?即使它們看似什么都不做,我是否仍然需要它們?
Gulp 需要知道異步任務何時完成.您有 3 種方法可以做到這一點:
返回一個事件流,這就是當你的任務函數中有
return gulp.src(...).pipe(...);
時你所做的.p>返回一個承諾.
讓定義你任務的函數帶一個參數.Gulp 將使用回調函數調用您的函數,您應該在任務結束時調用該回調函數.
如果您不這樣做,那么 Gulp 將不會知道該任務是異步的.因此,它會在其函數返回后立即認為任務已完成.這會帶來各種后果.Gulp 可以在任務完成之前退出.或者它可以在任務 A 完成之前啟動依賴于任務 A 的任務 B.在某些情況下,您可能立即看不到問題,但隨著您的 gulpfile 變得越來越復雜,您出現不良行為的機會也越來越多.run-sequence
并不特殊:它需要知道任務何時完成才能正確完成工作.
請注意,您在問題中顯示的此代碼中的 callback
是無用的:
return target.pipe(inject(sources)).pipe(gulp.dest('.'), 回調);
callback
不會被調用.將其替換為 function () { console.log("Hello!");打回來();}代碼>.您不會在控制臺上看到
Hello!
,因為未調用回調.但是,代碼確實可以正常工作,因為您返回了流.Gulp 使用您返回的流來確定任務結束,而不是回調(無論如何都不會調用).
I was having trouble getting gulp run-sequence
to execute both functions that i gave it (it would only execute one of them. I had something like this:
gulp.task('wire', function(){
gulp.src('./index.html')
.pipe(wiredep())
.pipe(gulp.dest('.'));
});
var filesToInject = ['./app/**/*.js', './app/**/*.css'];
gulp.task('inject', function (){
var target = gulp.src('./index.html');
var sources = gulp.src(filesToInject, {read: false});
return target.pipe(inject(sources))
.pipe(gulp.dest('.'));
});
gulp.task('wire-inject', function(callback){
sequence('wire', 'inject', callback);
});
That would only run the inject
task. Only when I added a return
before gulp.src
in the wire
task would it execute. I also added callback in both function params and passed it to the last pipe in both tasks per the documentation.
gulp.task('wire', function(callback){
gulp.src('./index.html')
.pipe(wiredep())
.pipe(gulp.dest('.'), callback);
});
var filesToInject = ['./app/**/*.js', './app/**/*.css'];
gulp.task('inject', function (callback){
var target = gulp.src('./index.html');
var sources = gulp.src(filesToInject, {read: false});
return target.pipe(inject(sources))
.pipe(gulp.dest('.'), callback);
});
That did not throw any errors, however it doesn't change anything if I take it out. What does this return statement do that magically makes my sequence run completely. What are these callbacks in the documentation that I just pass a reference to with out parentheses to execute them? Do I still need them even though they seemingly do nothing?
Gulp needs to know when an asynchronous task is done. You have 3 ways to do this:
Return an event stream, which is what you do when you have
return gulp.src(...).pipe(...);
in your task's function.Return a promise.
Have the function that defines you task take a parameter. Gulp will call your function with a callback that you should call when the task is over.
If you do not do this, then Gulp will not know that the task is asynchronous. So it will consider the task done as soon as its function returns. This has all kinds of consequences. Gulp could exit before a task is done. Or it could start a task B that depends on task A before task A is complete. In some cases, you may not see a problem immediately but as your gulpfile gets more complex, you run more chances of getting bad behavior. run-sequence
is not special: it needs to know when a task is complete to do its job correctly.
Note that callback
in this code which you show in your question is useless:
return target.pipe(inject(sources))
.pipe(gulp.dest('.'), callback);
callback
won't be called. Replace it with function () { console.log("Hello!"); callback(); }
. You won't see Hello!
on the console because the callback is not called. However, the code does work overall because you return the stream. Gulp uses the stream you return to determine that the task is over, not the callback (which is never called anyway).
這篇關于Gulp 任務中`return` 語句和`callback` 參考的用途的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!