2014-12-30 125 views
0

如何在一個吞嚥任務中運行多個操作?以下情況不起作用,因爲事情似乎不按順序運行,並導致各種奇怪的錯誤。我試過event-streammergeHow to perform multiple gulp commands in one task,但這似乎不適用於del。如何在一個gulp任務中運行多個操作?

我知道我可以在鏈接的問題,我不希望我的臃腫與Gulpfile永遠不會單獨運行,不要使外部感任務劃分成多個任務,並使用run-sequence插件,但像給定的上下文。

gulp.task('task', function() { 

    del('....'); 

    gulp.src('....') 
     .pipe(gulp.dest('....')); 

    gulp.src('....') 
     .pipe(gulp.dest('....')); 

}); 
+0

但爲什麼不'run-sequence'?如果你關心「膨脹Gulpfile」,你可以創建額外的文件並「需要」它們。 – Ginden

回答

3

您需要將數據流分配到變量,然後用es.merge一起運行它們(或者你可以使用merge-stream如果你並不需要所有的事件流)。至於與del運行,看看建立一個相關的任務做你的清潔操作:

https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md

gulp.task('clean', function (cb) { 
    del([ 
    'dist/report.csv', 
    // here we use a globbing pattern to match everything inside the `mobile` folder 
    'dist/mobile/**', 
    // we don't want to clean this file though so we negate the pattern 
    '!dist/mobile/deploy.json' 
    ], cb); 
}); 

然後,您可以這樣定義你的其他任務:

var merge = require('merge-stream'); 

gulp.task('task', ['clean'], function() { 
    var someOperation = gulp.src('./').pipe(gulp.dest('out')); 
    var someOtherOperation = gulp.src('./assets').pipe(gulp.dest('out/assets')); 

    return merge(someOperation, someOtherOperation); 
}); 

這將首先完成清理,等到完成,然後運行其他操作。