【发布时间】:2015-04-07 13:41:48
【问题描述】:
下面是一个 Gulp ES6 转译任务。它工作正常,但我试图用 gulp-watch 插件替换 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; }));
}
});
});
这就是到目前为止我对 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'),
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; }));
}
});
});
日志的输出是这样的:
观看事件 - history,cwd,base,stat,_contents,event
观看事件 - 改变
观看活动 - ..
我如何让 gulp-watch 向我提供我以前的信息,或者,我如何更改我的任务代码以使用 gulp-watch 再次工作?
【问题讨论】:
-
不应该只是
obj.event === 'change'吗? -
@Ben - 当然 - 我可以克服 那个 驼峰,但我仍然缺少 obj.path,因此我可以识别 gulp.src 的特定文件。我还应该注意,我对 Gulp 比较陌生 - 所以答案很可能很简单......
标签: node.js gulp gulp-watch babeljs