与babel.js一起使用gulp-watch
下面是一个Gulp ES6的编译任务。 它工作正常,但我试图用gulp-watch插件replacegulp.watch,以便捕获新文件。 问题是,gulp-watch并没有给我gulp.watch做的callback,我不知道该怎么办。
这是我原来的工作任务:
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; })); } }); });
日志的输出是这样的:
观看事件 – 历史,cwd,基地,统计,_contents,事件
观看事件 – 改变
观看比赛 – ..
我如何得到我以前的信息,或者,我怎样才能改变我的任务的代码,让这个工作再次与gulp-watch?
根据testing , obj.relative
应该包含相对文件名, obj.path
仍然保存绝对文件path,就像它在原始代码中一样。 此外,callback接受乙烯基对象,这是logging在这里: https : //github.com/wearefractal/vinyl
您可能无法在您的日志中看到它们,因为Object.keys
不枚举原型链中的属性。
使用for..in
循环,你应该能够看到所有的属性。