【问题标题】:gulp watch with babel then pm2 restartgulp watch with babel 然后 pm2 重启
【发布时间】:2015-05-07 23:09:54
【问题描述】:

\嘿伙计们,我完全被这个卡住了。

基本上我希望我的本地开发人员能够让 gulp 监视我的 src js 文件并使用 babel 转换它们并将它们输出到我的 dist 文件夹,然后在完成之后让 pm2 restart 节点加载最新的更改。

我遇到的问题是我一生都无法弄清楚如何添加一个回调来监视,以便重新启动 pm2 的调用仅在 babel 完成其魔术转换文件之后发生。

var gulp = require("gulp");
var babel = require("gulp-babel");
var pm2 = require("pm2");
var watch = require("gulp-watch");
var plumber = require("gulp-plumber");

var SRC = "src/**/*js";
var DIST = "dist/";

function restartPM2() {
  //restart pm2 code in here
}

gulp.task("default", function () {
  return gulp.src(SRC)
    .pipe(watch(SRC))
    .pipe(plumber())
    .pipe(babel())
    .pipe(plumber.stop())
    .pipe(gulp.dest(DIST));
    // somewhere in here need a call back after babel has transformed  
    // the code and saved it to dist/ to then call restartPM2
});

任何帮助将不胜感激!

【问题讨论】:

  • 你不能让 pm2 监视构建文件夹吗?
  • 我最初是这样做的,但是当 gulp 将文件复制到目录中时,我得到了大量的重启
  • 可能有点晚了,但是对我来说 nodemon 有用的是 -d (去抖动,延迟),也许 pm2 有这样的选项?

标签: javascript gulp watch pm2 babeljs


【解决方案1】:

首先,您的观看方式不对。然后,你应该把事情分开。我就是这样做的:

var paths = {
  babel: './somedir'
}

//basic babel task
gulp.task('babel', function() {
  return gulp.src(paths.babel)
  .pipe(babel())
  .pipe(gulp.dest('./'))
})

//see below for some links about programmatic pm2
gulp.task('pm2', function(cb) {
  pm2.connect(function() {
    pm2.restart('echo', function() { 
      return cb()
    })
  })
})

gulp.task('default', ['babel']) //I don't restart pm2 with the default task but you could

//the watch task
gulp.task('watch', function() {
  //their could be more watchers here ofc
  gulp.watch(paths.babel, ['babel', 'pm2'])
})

如果您启动 gulp watch,它将监视 paths.babel,并在更改时执行两个任务(babel,pm2)。 如果您只执行gulp(或本例中的gulp babel),它将启动相应的任务。你也可以启动gulp pm2

资源:

【讨论】:

  • 您正在运行babel 任务,它将重新编译所有文件。想知道我们可以只对那个非常具体的文件进行 babelize 吗?
  • 我不明白你为什么不能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2015-12-22
  • 2015-03-05
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多