java顺序任务,如何依次顺序运行Gulp任务

回答(13)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

它还没有正式发布,但即将推出的Gulp 4.0可让您轻松地与gulp.series进行同步任务 . 你可以这样做:

gulp.task('develop', gulp.series('clean', 'coffee'))

我发现了一篇很好的博客文章,介绍了如何升级和利用这些简洁的功能:migrating to gulp 4 by example

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

默认情况下,gulp同时运行任务,除非它们具有显式依赖关系 . 这对于像 clean 这样你不想依赖的任务非常有用,但是你需要它们才能在其他所有事情之前运行 .

我写了the run-sequence plugin专门解决这个问题与gulp . 安装后,使用它如下:

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

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

runSequence('clean', 'coffee', function() {

console.log('Run something else');

done();

});

});

您可以阅读README包中的完整说明 - 它还支持同时运行一些任务集 .

请注意,这将是(effectively) fixed in the next major release of gulp,因为它们完全消除了自动依赖性排序,并提供类似于 run-sequence 的工具,允许您手动指定运行顺序的方式 .

然而,这是一个重大的突破性变化,因此没有理由等待今天你可以使用 run-sequence .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这个问题的唯一好方法可以在gulp文档中找到,可以找到here

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']);

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我使用generator-gulp-webapp Yeoman生成器生成了一个node / gulp应用程序 . 它以这种方式处理"clean conundrum"(转换为问题中提到的原始任务):

gulp.task('develop', ['clean'], function () {

gulp.start('coffee');

});

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

run-sequence是最明确的方式(至少在Gulp 4.0发布之前)

With run-sequence ,您的任务将如下所示:

var sequence = require('run-sequence');

/* ... */

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

sequence('clean', 'coffee', done);

});

但如果你(出于某种原因)不想使用它, 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: 如果您只是在没有收听结果的情况下启动任务, develop 任务将在 coffee 之前完成,这可能会令人困惑 .

您可能还需要 remove event listener

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');

});

考虑 there is also task_err event 你可能想听 . 成功完成后会触发 task_stop ,而当出现错误时会出现 task_err .

您可能也想知道为什么没有 gulp.start() 的官方文档 . 来自gulp成员的答案解释了这些事情:

gulp.start有意无证,因为它可能导致复杂的构建文件,我们不希望人们使用它

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

根据Gulp文档:

在依赖项完成之前,您的任务是否正在运行?确保您的依赖项任务正确使用异步运行提示:接受回调或返回承诺或事件流 .

要同步运行您的任务序列:

将事件流(例如 gulp.src )返回到 gulp.task ,以通知任务流何时结束 .

在 gulp.task 的第二个参数中声明任务依赖项 .

查看修订后的代码:

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"

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我遇到了同样的问题,解决方案对我来说非常简单 . 基本上将您的代码更改为以下,它应该工作 . 注意: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"

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

尝试了所有提出的解决方案,似乎都有自己的问题 .

如果您实际查看Orchestrator源代码,特别是 .start() 实现,您将看到如果最后一个参数是函数,它将把它视为回调 .

我为自己的任务写了这个片段:

gulp.task( 'task1', () => console.log(a) )

gulp.task( 'task2', () => console.log(a) )

gulp.task( 'task3', () => console.log(a) )

gulp.task( 'task4', () => console.log(a) )

gulp.task( 'task5', () => console.log(a) )

function runSequential( tasks ) {

if( !tasks || tasks.length <= 0 ) return;

const task = tasks[0];

gulp.start( task, () => {

console.log( `${task} finished` );

runSequential( tasks.slice(1) );

} );

}

gulp.task( "run-all", () => runSequential([ "task1", "task2", "task3", "task4", "task5" ));

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

只需使 coffee 依赖于 clean ,而 develop 依赖于 coffee :

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

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

派遣现在是连续的: clean → coffee → develop . 请注意 clean 的实现和 coffee 的实现 must 接受回调,"so the engine knows when it'll be done":

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

del(['dist/*'], callback);

});

总之,下面是 simple gulp pattern for a synchronous clean followed by asynchronous build dependencies :

//build sub-tasks

gulp.task('bar', ['clean'], function(){...});

gulp.task('foo', ['clean'], function(){...});

gulp.task('baz', ['clean'], function(){...});

...

//main build task

gulp.task('build', ['foo', 'baz', 'bar', ...], function(){...})

Gulp很聪明,每 build 只运行一次 clean ,无论多少 build 的依赖依赖于 clean . 如上所述, clean 是同步障碍,然后 build 的所有依赖项并行运行,然后 build 运行 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我有一段时间正在寻找这个答案 . 现在我在官方的gulp文档中得到了它 .

如果要在最后一个任务完成时执行gulp任务,则必须返回一个流:

gulp.task('wiredep', ['dev-jade'], function () {

var stream = gulp.src(paths.output + '*.html')

.pipe($.wiredep())

.pipe(gulp.dest(paths.output));

return stream; // execute next task when this is completed

});

// First will execute and complete wiredep task

gulp.task('prod-jade', ['wiredep'], function() {

gulp.src(paths.output + '**/*.html')

.pipe($.minifyHtml())

.pipe(gulp.dest(paths.output));

});

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

对我来说,连接后没有运行minify任务,因为它期望连接输入并且有时没有生成 .

我尝试按执行顺序添加到默认任务,但它没有奏效 . 在为每个任务添加 return 并在 gulp.start() 中进行缩小之后,它起作用如下 .

/**

* Concatenate JavaScripts

*/

gulp.task('concat-js', function(){

return gulp.src([

'js/jquery.js',

'js/jquery-ui.js',

'js/bootstrap.js',

'js/jquery.onepage-scroll.js',

'js/script.js'])

.pipe(maps.init())

.pipe(concat('ux.js'))

.pipe(maps.write('./'))

.pipe(gulp.dest('dist/js'));

});

/**

* Minify JavaScript

*/

gulp.task('minify-js', function(){

return gulp.src('dist/js/ux.js')

.pipe(uglify())

.pipe(rename('ux.min.js'))

.pipe(gulp.dest('dist/js'));

});

gulp.task('concat', ['concat-js'], function(){

gulp.start('minify-js');

});

gulp.task('default',['concat']);

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

Gulp和Node使用 promises .

所以你可以这样做:

// ... require gulp, del, etc

function cleanTask() {

return del('./dist/');

}

function bundleVendorsTask() {

return gulp.src([...])

.pipe(...)

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

}

function bundleAppTask() {

return gulp.src([...])

.pipe(...)

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

}

function tarTask() {

return gulp.src([...])

.pipe(...)

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

}

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

// 1. Run the clean task

cleanTask().then(function () {

// 2. Clean is complete. Now run two tasks in parallel

Promise.all([

bundleVendorsTask(),

bundleAppTask()

]).then(function () {

// 3. Two tasks are complete, now run the final task.

tarTask();

});

});

});

如果返回gulp流,则可以使用 then() 方法添加回调 . 或者,您可以使用Node的本机 Promise 来创建自己的承诺 . 在这里,我使用 Promise.all() 来获得一个在所有承诺时触发的回调解决 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我在自述文件中尝试了所有“官方”方式,它们对我不起作用,但确实如此 . 你也可以升级到gulp 4.x,但我强烈建议你不要,它会打破这么多东西 . 你可以使用真正的js承诺,但是嘿,这很快,很脏,很简单:-)基本上你使用:

var wait = 0; // flag to signal thread that task is done

if(wait == 0) setTimeout(... // sleep and let nodejs schedule other threads

看看帖子!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值