返回介绍

task()

发布于 2020-01-02 21:01:17 字数 3822 浏览 1056 评论 0 收藏 0

提醒: 这个API不再是推荐的模式了 - export your tasks。因此就不翻译了!

在任务系统中定义任务。然后可以从命令行和 series()parallel()lastRun() api 访问该任务。

Usage

Register a named function as a task:

const { task } = require('gulp');

function build(cb) {
  // body omitted
  cb();
}

task(build);

Register an anonymous function as a task:

const { task } = require('gulp');

task('build', function(cb) {
  // body omitted
  cb();
});

Retrieve a task that has been registered previously:

const { task } = require('gulp');

task('build', function(cb) {
  // body omitted
  cb();
});

const build = task('build');

Signature

task([taskName], taskFunction)

Parameters

If the taskName is not provided, the task will be referenced by the name property of a named function or a user-defined displayName property. The taskName parameter must be used for anonymous functions missing a displayName property.

Since any registered task can be run from the command line, avoid using spaces in task names.

parametertypenote
taskNamestringAn alias for the task function within the the task system. Not needed when using named functions for taskFunction.
taskFunction
(required)
functionA task function or composed task - generated by series() and parallel(). Ideally a named function. Returns

When registering a task, nothing is returned.

When retrieving a task, a wrapped task (not the original function) registered as taskName will be returned. The wrapped task has an unwrap() method that will return the original function.

Errors

When registering a task where taskName is missing and taskFunction is anonymous, will throw an error with the message, "Task name must be specified".

Task metadata

propertytypenote
namestringA special property of named functions. Used to register the task.
Note: name is not writable; it cannot be set or changed.
displayNamestringWhen attached to a taskFunction creates an alias for the task. If using characters that aren't allowed in function names, use this property.
descriptionstringWhen attached to a taskFunction provides a description to be printed by the command line when listing tasks.
flagsobjectWhen 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文