Gulp task()
task()
提醒: 这个 API 不再推荐使用!
在任务系统中定义任务。然后可以从命令行和 series()
、parallel()
和 lastRun()
api 访问该任务。
Usage
将命名函数注册为任务
const { task } = require('gulp');
function build(cb) {
// body omitted
cb();
}
task(build);
将匿名函数注册为任务:
const { task } = require('gulp');
task('build', function(cb) {
// body omitted
cb();
});
检索先前已注册的任务:
const { task } = require('gulp');
task('build', function(cb) {
// body omitted
cb();
});
const build = task('build');
Signature
task([taskName], taskFunction)
Parameters
如果未提供taskName
,则该任务将由命名函数的name
属性或用户定义的displayName
属性引用。 taskName
参数必须用于缺少displayName
属性的匿名函数。
由于可以从命令行运行任何已注册的任务,因此请避免在任务名称中使用空格。
parameter | type | note |
---|---|---|
taskName | string | An alias for the task function within the the task system. Not needed when using named functions for taskFunction . |
taskFunction (required) |
function | A task function or composed task - generated by series() and parallel() . Ideally a named function. Task metadata can be attached to provide extra information to the command line. |
Returns
注册任务时,不返回任何内容。
检索任务时,将返回注册为taskName
的包装任务(不是原始函数)。 包装的任务有一个unwrap()
方法,该方法将返回原始函数。
Errors
当注册一个缺少taskName
并且taskFunction
是匿名的任务时,将引发错误消息“必须指定任务名称”。
Task metadata
property | type | note |
---|---|---|
name | string | A special property of named functions. Used to register the task. Note: name is not writable; it cannot be set or changed. |
displayName | string | When attached to a taskFunction creates an alias for the task. If using characters that aren’t allowed in function names, use this property. |
description | string | When attached to a taskFunction provides a description to be printed by the command line when listing tasks. |
flags | object | When attached to a taskFunction provides flags to be printed by the command line when listing tasks. The keys of the object represent the flags and the values are their descriptions. |
const { task } = require('gulp');
const clean = function(cb) {
// body omitted
cb();
};
clean.displayName = 'clean:all';
task(clean);
function build(cb) {
// body omitted
cb();
}
build.description = 'Build the project';
build.flags = { '-e': 'An example flag' };
task(build);