下面是一项ES6溢出任务。它工作得很好,但我正在尝试用gulp.watch替换它,这样就可以捕捉到新的文件。问题是,gulp.watch并没有告诉我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; }));
}
});
});日志记录的输出如下:
监视事件-历史记录,cwd,base,stat,_contents,event 手表事件-更改 看比赛- ..。
我怎样才能让手表给我以前的信息,或者,我怎样才能改变我的任务代码,让这个任务再次工作呢?
发布于 2015-04-07 14:05:21
根据测试,obj.relative应该包含相对文件名,而obj.path仍然保持绝对文件路径,就像它在原始代码中所做的那样。此外,回调还接受Vinyl对象,这在这里有文档:https://github.com/wearefractal/vinyl
您可能无法在日志中看到它们,因为Object.keys没有枚举原型链中的属性。
使用for..in循环,您应该能够看到所有属性。
https://stackoverflow.com/questions/29493057
复制相似问题