2015-04-07 28 views
2

下面是一个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,事件

手表事件 - 改变

手表事件 - ..

我如何吞下手表给我的信息,我有过,或者,我怎样才能改变我的任务代码得到这个工作了咕嘟咕嘟地看着?

+0

不应该只是'obj.event ==='change''吗? –

+0

@Ben - 当然 - 我可以克服*驼峰,但我仍然缺少obj.path,所以我可以识别gulp.src的特定文件。我还应该注意到,我对Gulp比较陌生 - 所以答案可能很简单... –

回答

2

根据tests,obj.relative应该包含相对文件名,并且obj.path仍然保持绝对文件路径,就像它在原始代码中一样。此外,回调接受乙烯基对象,这是记录在这里:https://github.com/wearefractal/vinyl

您可能无法看到它们在您的日志中,因为Object.keys不枚举原型链中的属性。使用for..in循环,您应该能够看到所有属性。

+0

非常感谢 - 我需要离开几个小时 - 但我会尽快给你看看因为我回来了,如果它有效的话,我会接受并接受。 –

+0

非常感谢 - 我做了一个小小的编辑 - 看起来像我应该只留下我的原始代码 - 我希望这被记录得更好! –

+0

呃 - 我猜我需要的文档是在这里: - | https://github.com/wearefractal/vinyl –