【问题标题】:gulp 4 - use watch for server job instead of compiler jobgulp 4 - 使用监视服务器作业而不是编译器作业
【发布时间】:2019-07-09 17:48:48
【问题描述】:

如果您有一个开始和结束的任务,则需要在每次文件更改时运行,这里有很好的记录https://gulpjs.com/docs/en/getting-started/watching-files

const { watch } = require('gulp');

exports.default = function() {
  // The task will be executed upon startup
  watch('src/*.js', { ignoreInitial: false }, function(cb) {
    // body omitted
    cb();
  });
};

但是有没有办法像 nodemon 一样使用 gulp watch?在哪里让任务运行,然后在监视列表中的文件发生更改时停止并启动它?

--- 更多 ---

被要求提供一些示例,所以这里有一些不起作用的示例

问题是我不知道如何设置它,以便在触发手表时停止现有服务器。

---- 示例 #1 - 在进程中运行服务器 -----

exports.default = function() {
  watch('src/*.js', { ignoreInitial: false }, function(cb) {
    // this will call back but when the watch is triggered again
    // it will try to start another instance
    app.listen(3000, cb);
  });
};

---- 示例#2 - 在自己的进程进程中运行服务器-----

exports.default = function() {
  watch('src/*.js', { ignoreInitial: false }, function(cb) {
    const proc = spawn('node', ['server.js']);
    proc.stdout.pipe(process.stdout);
    proc.stderr.pipe(process.stderr);
    // this will never call the call back
    // so never complete
    cb();
  });
};

【问题讨论】:

  • 我不太明白您的用例与 Gulp 中的常规观看任务有何不同。可以举个例子吗?
  • 你查看gulp-nodemon了吗?
  • 我在使用 nodemon 时遇到了问题,因此试图找出是否有一种足够简单的方法可以只用 gulp 和 watch 来做到这一点

标签: gulp gulp-4


【解决方案1】:

好吧,至少你可以这样做:

% gulp someTask >/dev/null 2>&1 &
[1] _pid_

如果它是像watch 这样的任务,你的 gulp 任务将无限期地运行。

但是这种方法很肮脏。你应该使用nodemon 之类的东西来实现这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多