Gulp watch 没有发出事件对象
Gulp watch not emitting event object
Gulp (4.0) 在我尝试 gulp.src
从 gulp.watch
获得的已更改文件的路径时抛出错误 "Error: Invalid glob argument: undefined"
。这两个:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html')
.on('change', function(file) {
gulp.src(file.path)
.pipe(gulp.dest('./output'));
});
});
和这个语法变体:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html', function(file) {
gulp.src(file.path)
.pipe(gulp.dest('./output'));
});
});
产生相同的结果。
在 gulp.src
之前记录 file.path 也会输出 "undefined"。
重现:
npm install -g gulp@github:gulpjs/gulp#4.0
并创建一个 gulpfile.js
包含:
const gulp = require('gulp');
后跟任一示例。
我的理解是,更改后的文件路径应该可以通过这种方式访问as per the docs,所以我不确定哪里出错了?
那个文档有各种各样的错误。例如它谈到 gaze
events, but gaze
is no longer used to watch for file changes in gulp 4.0; it has been replaced by chokidar
.
关于您的问题,文档关于您必须访问文件路径的方式是错误的。 .on('change')
回调函数没有传递一个 event
对象,所以没有 .path
属性.
而是将文件路径作为字符串传递,因此您的第一个示例应如下所示:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html')
.on('change', function(file) {
gulp.src(file)
.pipe(gulp.dest('./output'));
});
});
第二个示例中的回调函数根本没有接收到路径。它传递了一个 done
回调,以便于通过 gulp.series()
或 gulp.parallel()
.
执行任务
Gulp (4.0) 在我尝试 gulp.src
从 gulp.watch
获得的已更改文件的路径时抛出错误 "Error: Invalid glob argument: undefined"
。这两个:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html')
.on('change', function(file) {
gulp.src(file.path)
.pipe(gulp.dest('./output'));
});
});
和这个语法变体:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html', function(file) {
gulp.src(file.path)
.pipe(gulp.dest('./output'));
});
});
产生相同的结果。
在 gulp.src
之前记录 file.path 也会输出 "undefined"。
重现:
npm install -g gulp@github:gulpjs/gulp#4.0
并创建一个 gulpfile.js
包含:
const gulp = require('gulp');
后跟任一示例。
我的理解是,更改后的文件路径应该可以通过这种方式访问as per the docs,所以我不确定哪里出错了?
那个文档有各种各样的错误。例如它谈到 gaze
events, but gaze
is no longer used to watch for file changes in gulp 4.0; it has been replaced by chokidar
.
关于您的问题,文档关于您必须访问文件路径的方式是错误的。 .on('change')
回调函数没有传递一个 event
对象,所以没有 .path
属性.
而是将文件路径作为字符串传递,因此您的第一个示例应如下所示:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html')
.on('change', function(file) {
gulp.src(file)
.pipe(gulp.dest('./output'));
});
});
第二个示例中的回调函数根本没有接收到路径。它传递了一个 done
回调,以便于通过 gulp.series()
或 gulp.parallel()
.