From Grunt to Gulp



我目前正在尝试将Grunt文件转换为Gulp文件。我的第一次尝试是使用一个非常简单的文件,它只运行JSHint和Mocha,并具有监视模式。我的第一个结果是…嗯…幻灭了。

我遇到了几个问题,我希望有一种方法来解决它们:

  • 我意识到Gulp异步运行所有任务。如果我想等待任务完成,文档告诉我使用回调、promise或返回流。但是如何使用gulp-mochagulp-jshint?这些插件支持这个吗
  • 失败的gulp-jshint未使生成失败。如果gulp-jshint失败,我如何告诉Gulp停止继续
  • 在运行gulp时,使用Gulp的入门指南中描述的watch模式会导致Too many open files错误。你知道可能出了什么问题吗

(请注意,我故意没有在这里指定源代码,因为前两个问题是一般问题,最后一个问题指的是默认文件。)

有什么帮助吗?

关于任务依赖性,最简单的解决方案是从任务返回gump流,并依赖于该任务。

在下面的代码中,如果"服务器"任务不返回流,它将异步运行,导致"服务"任务尝试使用不存在的文件运行服务器。

gulp.task('server', function () {
return gulp.src('server/**/*.coffee')
.pipe(coffeescript())
.pipe(gulp.dest('./dist/'));
});
var expressServer;
gulp.task('serve', ['server'], function () {
var apiServer = require('./dist/server');
expressServer = apiServer(function (server) {
server.use(livereload({
port: livereloadport
}));
});
expressServer.listen(serverport);
//Set up your livereload server
lrserver.listen(livereloadport);
});

对于"停止和失败"部分:

以下是jshint的一个功能示例:

var reporter = require('jshint-stylish');
var jshintRc = readJSON('.jshintrc');
// Implement asynchronous support returning the stream
gulp.task('myhinter', function() {
// All js in src
return gulp.src('./src/*.js')
// options from file
.pipe(jshint(jshintRc))
// like stylish reporter
.pipe(jshint.reporter(reporter))
// Our turn!
.pipe(map(function (file, cb) {
// Notify the callback "true" if it failed on the file
cb(!file.jshint.success, file);
}));
})
// Now, define thattask to depend on myhinter
gulp.task('thattask', ['myhinter'], function(e) {
console.warn('thattask is executing');
});

CCD_ 8取决于任务CCD_。

反过来,myhinter对文件执行jshint,reporter使用时尚,如果至少有一个文件提示失败,则会失败。

在这里,这是通过从myhinter任务返回流来实现的。

您可以在编排器文档中看到更多相关信息:https://github.com/robrich/orchestrator

我不使用摩卡,但稍后会看一看如何做。

关于打开的文件太多,是在OSX上吗?如果是这样的话,如果有帮助的话,可以在这里看到。

我解决了"打开的文件太多"的问题,在我的.bash_profile中添加了以下行:

ulimit -n 10480

最新更新