iLe*_*ing 393 javascript node.js coffeescript gulp
在这样的代码片段中:
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"
Run Code Online (Sandbox Code Playgroud)
在develop任务中我想要运行clean并在完成后运行coffee,当完成时,运行其他东西.但我无法弄明白.这件作品不起作用.请指教.
Ove*_*ous 411
默认情况下,gulp同时运行任务,除非它们具有显式依赖关系.这对于clean您不想依赖的任务非常有用,但是您需要它们才能在其他任何事情之前运行.
我专门写了这个run-sequence插件来解决这个问题.安装后,使用它如下:
var runSequence = require('run-sequence');
gulp.task('develop', function(done) {
runSequence('clean', 'coffee', function() {
console.log('Run something else');
done();
});
});
Run Code Online (Sandbox Code Playgroud)
您可以阅读自述文件包README上的完整说明 - 它还支持同时运行一些任务集.
请注意,这将在gulp的下一个主要版本中(有效)修复,因为它们完全消除了自动依赖性排序,并提供了类似的工具run-sequence,允许您手动指定运行顺序的方式.
然而,这是一个重大的突破性变化,所以没有理由等待你run-sequence今天可以使用.
Mat*_*eré 371
这个问题的唯一好方法可以在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']);
Run Code Online (Sandbox Code Playgroud)
mas*_*shi 118
它尚未正式发布,但即将推出的Gulp 4.0可让您轻松地与gulp.series进行同步任务.你可以这样做:
gulp.task('develop', gulp.series('clean', 'coffee'))
Run Code Online (Sandbox Code Playgroud)
我发现了一篇很好的博客文章,介绍了如何升级和利用这些简洁的功能: 通过示例迁移到gulp 4
Ste*_*eve 54
我使用generator-gulp-webapp Yeoman生成器生成了一个node/gulp app .它以这种方式处理"干净的难题"(转换为问题中提到的原始任务):
gulp.task('develop', ['clean'], function () {
gulp.start('coffee');
});
Run Code Online (Sandbox Code Playgroud)
thy*_*bzi 32
run-sequence是最清晰的方式(至少在Gulp 4.0发布之前)
使用运行顺序,您的任务将如下所示:
var sequence = require('run-sequence');
/* ... */
gulp.task('develop', function (done) {
sequence('clean', 'coffee', done);
});
Run Code Online (Sandbox Code Playgroud)
但是如果你(出于某种原因)不想使用它,gulp.start方法将有助于:
gulp.task('develop', ['clean'], function (done) {
gulp.on('task_stop', function (event) {
if (event.task === 'coffee') {
done();
}
});
gulp.start('coffee');
});
Run Code Online (Sandbox Code Playgroud)
注意: 如果您只是在不收听结果的情况下启动任务,则develop任务将早于完成coffee,这可能会令人困惑.
您也可以在不需要时删除事件侦听器
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');
});
Run Code Online (Sandbox Code Playgroud)
考虑也有task_err活动,你可能要听.
task_stop成功完成task_err时触发,当出现错误时出现.
您可能也想知道为什么没有官方文档gulp.start().来自gulp成员的答案解释了这些事情:
gulp.start是故意的,因为它可能导致复杂的构建文件,我们不希望人们使用它
(来源:https://github.com/gulpjs/gulp/issues/426#issuecomment-41208007)
sen*_*tor 26
根据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"
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,解决方案对我来说非常简单.基本上将您的代码更改为以下,它应该工作.注意: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"
Run Code Online (Sandbox Code Playgroud)
尝试了所有提出的解决方案,似乎都有自己的问题.
如果您实际查看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" ));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
224748 次 |
| 最近记录: |