如何一個接一個地順序運行Gulp任務

本文翻譯自:How to run Gulp tasks sequentially one after the other

in the snippet like this: 在這樣的代碼片段中:

gulp.task "coffee", ->
    gulp.src("src/server/**/*.coffee")
        .pipe(coffee {bare: true}).on("error",gutil.log)
        .pipe(gulp.dest "bin")

gulp.task "clean",->
    gulp.src("bin", {read:false})
        .pipe clean
            force:true

gulp.task 'develop',['clean','coffee'], ->
    console.log "run something else"

In develop task I want to run clean and after it's done, run coffee and when that's done, run something else. develop任務中,我希望運行clean並在完成後運行coffee ,當完成後,運行其他東西。 But I can't figure that out. 但我無法弄明白。 This piece doesn't work. 這件作品不起作用。 Please advise. 請指教。


#1樓

參考:https://stackoom.com/question/1Xlhq/如何一個接一個地順序運行Gulp任務


#2樓

By default, gulp runs tasks simultaneously, unless they have explicit dependencies. 默認情況下,gulp同時運行任務,除非它們具有顯式依賴關係。 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. 這對於像clean這樣的任務來說並不是很有用,你不想依賴它,但是你需要它們才能在其他任何事情之前運行。

I wrote the run-sequence plugin specifically to fix this issue with gulp. 我寫了一個run-sequence插件,專門用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. 您可以閱讀自述文件包README上的完整說明 - 它還支持同時運行一些任務集。

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. 請注意,這將在gulp的下一個主要版本中(有效)修復 ,因爲它們完全消除了自動依賴性排序,並提供類似於run-sequence工具,允許您手動指定運行順序的方式。

However, that is a major breaking change, so there's no reason to wait when you can use run-sequence today. 但是,這是一個重大的突破性變化,因此沒有理由等待今天可以使用run-sequence


#3樓

The only good solution to this problem can be found in the gulp documentation which can be found here 這個問題的唯一好方法可以在gulp文檔中找到,可以在這裏找到

var gulp = require('gulp');

// takes in a callback so the engine knows when it'll be done
gulp.task('one', function(cb) {
  // do stuff -- async or otherwise
  cb(err); // if err is not null and not undefined, the orchestration will stop, and 'two' will not run
});

// identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
  // task 'one' is done now
});

gulp.task('default', ['one', 'two']);
// alternatively: gulp.task('default', ['two']);

#4樓

Try this hack :-) Gulp v3.x Hack for Async bug 試試這個黑客:-) 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. 你也可以升級到gulp 4.x,但我強烈建議你不要,它打破了很多東西。 You could use a real js promise, but hey, this is quick, dirty, simple :-) Essentially you use: 你可以使用真正的js承諾,但是嘿,這很快,很髒,很簡單:-)基本上你使用:

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! 查看帖子!


#5樓

I generated a node/gulp app using the generator-gulp-webapp Yeoman generator. 我使用generator-gulp-webapp Yeoman生成器生成了一個node / gulp app。 It handled the "clean conundrum" this way (translating to the original tasks mentioned in the question): 它以這種方式處理“乾淨的難題”(轉換爲問題中提到的原始任務):

gulp.task('develop', ['clean'], function () {
  gulp.start('coffee');
});

#6樓

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.src之前的返回對我來說完全不同。

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"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章