简体   繁体   English

从Grunt到Gulp

[英]From Grunt to Gulp

I am currently experimenting with converting my Grunt files to Gulp files. 我目前正在尝试将我的Grunt文件转换为Gulp文件。 My first try was with a quite simple file which simply runs JSHint and Mocha, and has a watch mode. 我的第一次尝试是使用一个非常简单的文件,它只运行JSHint和Mocha,并具有监视模式。 My first result was quite … well … disillusioning. 我的第一个结果是......好吧......幻想破灭。

I encountered several problems, and I hope that there is a way to solve them: 我遇到了几个问题,我希望有办法解决它们:

  • I realized that Gulp runs all the tasks asynchronously. 我意识到Gulp异步运行所有任务。 If I want to wait for a task to finish, the documentation tells me to use a callback, a promise or to return a stream. 如果我想等待任务完成,文档告诉我使用回调,承诺或返回流。 But how do I do this with gulp-mocha and gulp-jshint ? 但是如何使用gulp-jshint gulp-mochagulp-jshint执行此gulp-jshint Do these plugins support this? 这些插件是否支持此功能?
  • A failing gulp-jshint did not fail the build. 失败gulp-jshint并没有使构建失败。 How do I tell Gulp to stop proceeding if gulp-jshint failed? 如果gulp-jshint失败,我怎么告诉Gulp停止继续?
  • Using watch mode as described in Gulp's getting started guide resulted in a Too many open files error when running gulp . 使用Gulp入门指南中描述的watch模式导致运行gulp时出现Too many open files错误。 Any idea of what might be wrong? 什么可能是错的?

(Please note that I intentionally did not specify source code here, as the first two questions are general questions, and the last one refers to the default file.) (请注意,我有意在此处未指定源代码,因为前两个问题是一般问题,最后一个问题是默认文件。)

Any help on this? 对此有何帮助?

Regarding task dependencies, the simplest solution is to return the gulp stream from the task, and depend on that task. 关于任务依赖性,最简单的解决方案是从任务返回gulp流,并依赖于该任务。

In the code below, if the "server" task does not return the stream, it would be run asynchronously, resulting in the "serve" task attempting to run a server using a non-existing file. 在下面的代码中,如果“server”任务未返回流,则它将异步运行,导致“serve”任务尝试使用不存在的文件运行服务器。

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

For the "stop and fail" part: 对于“停止和失败”部分:

Here is a functional example with jshint: 这是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');
});

thattask depends on the success of task myhinter . thattask取决于任务myhinter的成功。

In turn myhinter does jshint on files, reporter using stylish, and fails if at least one file failed hinting. 反过来myhinter会对文件进行jshint,使用时尚的记者,如果至少有一个文件没有提示,则会失败。

Here, this is implemented by returning the stream from the myhinter task. 这里,这是通过从myhinter任务返回流来实现的。

You can see more about that in the orchestrator documentation: https://github.com/robrich/orchestrator 您可以在orchestrator文档中查看更多相关信息: https//github.com/robrich/orchestrator

I don't use mocha, but will take a look later on to see how to do it. 我不使用摩卡,但稍后会看看如何做到这一点。

About the too many opened file, are on OSX? 关于太多打开的文件,是在OSX上? If so, maybe see this here if it helps. 如果是这样,如果它有帮助,也许可以在这里看到

我解决了“太多打开的文件..”在我的.bash_profile中添加以下行:

ulimit -n 10480

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM