gulp 3.0 & 4.0 | watch task has to be a function问题

Posted shenzs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gulp 3.0 & 4.0 | watch task has to be a function问题相关的知识,希望对你有一定的参考价值。

gulpfile.js

gulp.task(watch, function () {
    gulp.watch(./css/*.css,[css]);
    gulp.watch(./html/*.html,[html]);
    gulp.watch(./image/*.*,[image]);
    gulp.watch(./js/*.cjs,[js]);
});

报错:

[14:18:45] Error: watching css/*.css: watch task has to be a function (optionally generated by using gulp.parallel or gulp.series)
    at Gulp.watch (/Users/mac/Desktop/my_project/node_modules/gulp/index.js:31:11)
    at /Users/mac/Desktop/my_project/gulpfile.js:48:10
    at taskWrapper (/Users/mac/Desktop/my_project/node_modules/undertaker/lib/set-task.js:13:15)
    at bound (domain.js:425:14)
    at runBound (domain.js:438:12)
    at asyncRunner (/Users/mac/Desktop/my_project/node_modules/async-done/index.js:55:18)
    at processTicksAndRejections (internal/process/task_queues.js:79:9)

分析:

这个是由于require引入的包的版本问题导致的。在不同的版本里,接口参数发生了变化。在gulp4.0之后已经只能接受watch第二个参数必须为函数。详情请看官网

方法一:

改版本<4.0

方法二:

gulp.task(watch, function () {
    gulp.watch(./css/*.css,gulp.series([css]));这个列表只会匹配第一个
    gulp.watch(./html/*.html,gulp.series([html]));
    gulp.watch(./image/*.*,gulp.series([image])]);
    gulp.watch(./js/*.cjs,gulp.series([js]));
});

 

 

gulp.task(watch, function () {
    gulp.watch(./css/*.css,gulp.parallel([css]));这个列表可以 支持多个元素
    gulp.watch(./html/*.html,gulp.parallel([html]));
    gulp.watch(./image/*.*,gulp.parallel([image])]);
    gulp.watch(./js/*.cjs,gulp.parallel([js]));
});

 

以上是关于gulp 3.0 & 4.0 | watch task has to be a function问题的主要内容,如果未能解决你的问题,请参考以下文章

gulp#4.0

gulp

如何解决Gulp 4.0错误?

问题11:如何进行反向迭代 & 如何实现反向迭代

javascript Gulp#4.0:简单的gulpfile.js集合

我如何在 gulp 4.0 版中重写这个 gulpfile.js