下面是一个Gulp ES6转换任务。它工作正常,但我试图用gulp-watch插件替换gulp.watch,以便捕获新文件。问题在于,gulp-watch并没有给我gulp.watch在回调中做的事情,我不知道该怎么做。与babel.js一起使用gulp-watch
这是我原来的工作任务:
var gulp = require('gulp'),
rename = require('gulp-rename'),
plumber = require('gulp-plumber'),
gprint = require('gulp-print'),
notify = require('gulp-notify'),
babel = require('gulp-babel');
gulp.task('default', function() {
return gulp.watch('../**/**-es6.js', function(obj){
if (obj.type === 'changed') {
gulp.src(obj.path, { base: './' })
.pipe(plumber({
errorHandler: function (error) { /* elided */ }
}))
.pipe(babel())
.pipe(rename(function (path) {
path.basename = path.basename.replace(/-es6$/, '');
}))
.pipe(gulp.dest(''))
.pipe(gprint(function(filePath){ return "File processed: " + filePath; }));
}
});
});
而这里的一切,我至今与一饮而尽手表:
var gulp = require('gulp'),
rename = require('gulp-rename'),
plumber = require('gulp-plumber'),
gprint = require('gulp-print'),
notify = require('gulp-notify'),
babel = require('gulp-babel'),
gWatch = require('gulp-watch');
gulp.task('default', function() {
return gWatch('../**/**-es6.js', function(obj){
console.log('watch event - ', Object.keys(obj).join(','));
console.log('watch event - ', obj.event);
console.log('watch event - ', obj.base);
return;
if (obj.type === 'changed') {
gulp.src(obj.path, { base: './' })
.pipe(plumber({
errorHandler: function (error) { /* elided */ }
}))
.pipe(babel())
.pipe(rename(function (path) {
path.basename = path.basename.replace(/-es6$/, '');
}))
.pipe(gulp.dest(''))
.pipe(gprint(function(filePath){ return "File processed: " + filePath; }));
}
});
});
日志的输出是这样的:
关注事件 - 历史,联合王国,基地,stat,_contents,事件
手表事件 - 改变
手表事件 - ..
我如何吞下手表给我的信息,我有过,或者,我怎样才能改变我的任务代码得到这个工作了咕嘟咕嘟地看着?
不应该只是'obj.event ==='change''吗? –
@Ben - 当然 - 我可以克服*驼峰,但我仍然缺少obj.path,所以我可以识别gulp.src的特定文件。我还应该注意到,我对Gulp比较陌生 - 所以答案可能很简单... –