"Error: Invalid glob argument: undefined"当我尝试访问gulp.src从gulp.watch. 这两个:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html')
.on('change', function(file) {
gulp.src(file.path)
.pipe(gulp.dest('./output'));
});
});
Run Code Online (Sandbox Code Playgroud)
和这个语法变体:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html', function(file) {
gulp.src(file.path)
.pipe(gulp.dest('./output'));
});
});
Run Code Online (Sandbox Code Playgroud)
产生相同的结果。
在之前记录 file.pathgulp.src也会输出“未定义”。
重现:
npm install -g gulp@github:gulpjs/gulp#4.0
并创建一个gulpfile.js包含:
const gulp = require('gulp');
接下来是任一示例。
我的理解是,这是应该按照文档访问更改的文件路径的方式,所以我不确定我哪里出了问题?
该文档完全是错误的。例如,它谈论gaze事件,但gaze在 gulp 4.0 中不再用于监视文件更改;它已被替换为chokidar.
关于您的问题,文档关于您必须访问文件路径的方式是错误的。回调.on('change')函数没有传递event对象,因此没有.path属性。
相反,文件路径作为字符串传递,因此您的第一个示例应如下所示:
gulp.task('watch', function() {
gulp.watch('./web/**/*.html')
.on('change', function(file) {
gulp.src(file)
.pipe(gulp.dest('./output'));
});
});
Run Code Online (Sandbox Code Playgroud)
第二个示例中的回调函数根本不接收路径。它传递了一个done回调,以便通过gulp.series()或促进任务执行gulp.parallel()。
| 归档时间: |
|
| 查看次数: |
1369 次 |
| 最近记录: |