javascript - 当我输入 npm start 时如何启动 Gulp watch 任务

标签 javascript node.js gulp package.json

我有一个 gulp.js 文件,其中包括:

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

哪个启动了监视任务

gulp.task('watch', function(){
  gulp.watch(productionScripts, ['autoConcat']);
});

然后在 productionScripts 中对文件的任何保存更改,监视任务将连接文件。

我想做的是在我的 package.json 中,我想在我输入 npm start 时启动这个 watch (这已经启动了我的 Node 服务器)。

package.json

    "start": "node server.js",

更新--------

Ben(b3nj4m.com),我试过你所说的。 watch 和服务器启动。但是,一切都运行了两次(可能是由于编辑器,不相关),但是当我使用 gulp 启动它时,我确实丢失了我的服务器日志。

[15:31:18] Starting 'autoConcat'...
[15:31:18] Finished 'autoConcat' after 147 ms
[15:31:19] Starting 'autoConcat'...
[15:31:19] Finished 'autoConcat' after 138 ms
[15:31:20] Starting 'autoConcat'...
[15:31:20] Finished 'autoConcat' after 127 ms
[15:31:23] Starting 'autoConcat'...

这就像在服务器因更改而重新启动和级联文件更改之间存在一个循环。

最佳答案

你可以从你的 gulpfile 运行你的服务器:

var child = require('child_process');
var fs = require('fs');

gulp.task('default', ['server', 'watch']);

gulp.task('server', function() {
  var server = child.spawn('node', ['server.js']);
  var log = fs.createWriteStream('server.log', {flags: 'a'});
  server.stdout.pipe(log);
  server.stderr.pipe(log);
});

gulp.task('watch', function(){
  gulp.watch(productionScripts, ['autoConcat']);
});

然后将您的 npm start 定义更改为:

"scripts": {
  "start": "gulp"
}

关于javascript - 当我输入 npm start 时如何启动 Gulp watch 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28029929/

相关文章:

javascript - 无法让 gulp watch 触发所谓监视文件夹内文件更改的构建功能

javascript - 有没有更好的方法来编写下面的 gulp.js (使用 browserify)?

javascript - 最佳文件上传方式

javascript - 按日期范围从和到过滤表列表

javascript - 自定义切换功能不适用于 Ionic 应用程序中的 ng-click

node.js - 如何处理express nodejs中的body-parser错误

node.js - TypeError : Object #<IncomingMessage> has no method 'flash' error in node. js 即使在安装 connect-flash 之后

node.js - MAC OS X Yosemite 10.10.5 上的 npm、node-gyp 和 gulp 问题

javascript - Ember JS,在状态为 root.loaded.updated.uncommitted 的 Transformer 处理事件 `didCommit` 中解析数据时出错。”

node.js - 从 Google Apps 脚本中的 https 调用获取状态文本