当用户所需的任务不在gulp文件中时运行一个特定的任务
当用户提供的任务不在gulpfile
时,是否有任何方法来运行特定的任务。
例如,如果用户运行gulp build
并且build
任务不在gulpfile
那么应该运行一个特定的任务(或者默认的任务,对我来说不重要)。
作为一个类比,考虑指定的任务为gulp
的404页面 。
gulp
从orchestrator
inheritance,其中包含一个tasks
实例variables,该variables是一个普通对象,其中的键是添加到该实例的任务的名称。 如果使用proxy
replace了tasks
,可以使用get
trap处理程序返回默认任务,如default
任务:
const gulp = require('gulp') gulp.tasks = new Proxy(gulp.tasks, { get (target, property) { if (target.hasOwnProperty(property)) { return target[property] } return target.default } })
这些代码可以在任务开始之前的任何时候运行,即使在一些任务已经被添加到gulp
实例之后。
帕特里克的回答非常有帮助。 自从我收到ESLint警告和以下错误,我不得不稍微修改一下 –
TypeError: this.tasks.hasOwnProperty is not a function
所以我想也发布我的更改。 这是我的最终代码 –
gulp.tasks = new Proxy(gulp.tasks, { get: function(target, property) { if (undefined !== target[property]) { return target[property]; } return target.default; //or target["<Custom task name>"] } });