\嘿伙计们我完全坚持这个。
基本上,我希望我的本地开发人员能够让 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
。
资源:
关于javascript - gulp watch with babel 然后 pm2 restart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897213/