gulp 用 babel 观看然后 pm2 重启

gulp watch with babel then pm2 restart

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

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

我遇到的问题是我这辈子都想不出如何添加一个回调来观察,以便重启 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
});

如有任何帮助,我们将不胜感激!

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

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

资源: