【问题标题】:Gulp watch not emitting event objectGulp 手表不发出事件对象
【发布时间】:2016-09-12 14:03:23
【问题描述】:

当我尝试 gulp.srcgulp.watch 获得的已更改文件的路径时,Gulp (4.0) 会引发错误 "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 也输出“未定义”。

复制:

npm install -g gulp@github:gulpjs/gulp#4.0

并创建一个gulpfile.js,其中包含:

const gulp = require('gulp');

后面是两个例子。

我的理解是,这就是更改文件路径应该可以访问的方式as per the docs,所以我不确定我哪里出错了?

【问题讨论】:

    标签: gulp gulp-4


    【解决方案1】:

    那个文档有各种各样的错误。例如,它谈论 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'));
            });
    });
    

    第二个示例中的回调函数根本没有收到路径。它传递了一个done 回调,以便于通过gulp.series()gulp.parallel() 执行任务。

    【讨论】:

    • 维伦丹克!这确实是问题所在;非常感谢您花时间解释。希望这可以作为其他有同样头痛的人的资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多