创建任务#

¥Creating Tasks

每个 gulp 任务都是一个异步 JavaScript 函数 - 接受错误优先回调或返回流、promise、事件触发器、子进程或可观察对象的函数 (稍后会详细介绍)。由于某些平台限制,不支持同步任务,尽管有一个非常漂亮的 alternative

¥Each gulp task is an asynchronous JavaScript function - a function that accepts an error-first callback or returns a stream, promise, event emitter, child process, or observable (more on that later). Due to some platform limitations, synchronous tasks aren't supported, though there is a pretty nifty alternative.

导出#

¥Exporting

任务可以被视为公共或私有。

¥Tasks can be considered public or private.

  • 公共任务从 gulpfile 中导出,这允许它们通过 gulp 命令运行。

    ¥Public tasks are exported from your gulpfile, which allows them to be run by the gulp command.

  • 私有任务供内部使用,通常用作 series()parallel() 组合的一部分。

    ¥Private tasks are made to be used internally, usually used as part of series() or parallel() composition.

私有任务的外观和行为与任何其他任务类似,但终端用户无法独立执行它。要公开注册任务,请从 gulpfile 中导出它。

¥A private task looks and acts like any other task, but an end-user can't ever execute it independently. To register a task publicly, export it from your gulpfile.

const { series } = require('gulp');
// The `clean` function is not exported so it can be considered a private task.
// It can still be used within the `series()` composition.
function clean(cb) {
// body omitted
cb();
}
// The `build` function is exported so it is public and can be run with the `gulp` command.
// It can also be used within the `series()` composition.
function build(cb) {
// body omitted
cb();
}
exports.build = build;
exports.default = series(clean, build);

ALT TEXT MISSING

过去,`task()` 用于将你的函数注册为任务。虽然该 API 仍然可用,但导出应该是主要的注册机制,除非在导出不起作用的边缘情况下。

¥In the past, task() was used to register your functions as tasks. While that API is still available, exporting should be the primary registration mechanism, except in edge cases where exports won't work.

撰写任务#

¥Compose tasks

Gulp 提供了两种强大的组合方法,series()parallel(),允许将单个任务组合成更大的操作。两种方法都接受任意数量的任务函数或组合操作。series()parallel() 可以自行嵌套或彼此嵌套任意深度。

¥Gulp provides two powerful composition methods, series() and parallel(), allowing individual tasks to be composed into larger operations. Both methods accept any number of task functions or composed operations. series() and parallel() can be nested within themselves or each other to any depth.

要让任务按顺序执行,请使用 series() 方法。

¥To have your tasks execute in order, use the series() method.

const { series } = require('gulp');
function transpile(cb) {
// body omitted
cb();
}
function bundle(cb) {
// body omitted
cb();
}
exports.build = series(transpile, bundle);

要使任务以最大并发度运行,请将它们与 parallel() 方法结合起来。

¥For tasks to run at maximum concurrency, combine them with the parallel() method.

const { parallel } = require('gulp');
function javascript(cb) {
// body omitted
cb();
}
function css(cb) {
// body omitted
cb();
}
exports.build = parallel(javascript, css);

当调用 series()parallel() 时,任务立即组合。这允许组合的变化,而不是单个任务内的条件行为。

¥Tasks are composed immediately when either series() or parallel() is called. This allows variation in the composition instead of conditional behavior inside individual tasks.

const { series } = require('gulp');
function minify(cb) {
// body omitted
cb();
}
function transpile(cb) {
// body omitted
cb();
}
function livereload(cb) {
// body omitted
cb();
}
if (process.env.NODE_ENV === 'production') {
exports.build = series(transpile, minify);
} else {
exports.build = series(transpile, livereload);
}

series()parallel() 可以嵌套到任意深度。

¥series() and parallel() can be nested to any arbitrary depth.

const { series, parallel } = require('gulp');
function clean(cb) {
// body omitted
cb();
}
function cssTranspile(cb) {
// body omitted
cb();
}
function cssMinify(cb) {
// body omitted
cb();
}
function jsTranspile(cb) {
// body omitted
cb();
}
function jsBundle(cb) {
// body omitted
cb();
}
function jsMinify(cb) {
// body omitted
cb();
}
function publish(cb) {
// body omitted
cb();
}
exports.build = series(
clean,
parallel(
cssTranspile,
series(jsTranspile, jsBundle)
),
parallel(cssMinify, jsMinify),
publish
);

当运行组合操作时,每个任务将在每次引用时执行。例如,在两个不同任务之前引用的 clean 任务将运行两次并导致不期望的结果。相反,重构要在最终组合中指定的 clean 任务。

¥When a composed operation is run, each task will be executed every time it was referenced. For example, a clean task referenced before two different tasks would be run twice and lead to undesired results. Instead, refactor the clean task to be specified in the final composition.

如果你有这样的代码:

¥If you have code like this:

// This is INCORRECT
const { series, parallel } = require('gulp');
const clean = function(cb) {
// body omitted
cb();
};
const css = series(clean, function(cb) {
// body omitted
cb();
});
const javascript = series(clean, function(cb) {
// body omitted
cb();
});
exports.build = parallel(css, javascript);

迁移到这个:

¥Migrate to this:

const { series, parallel } = require('gulp');
function clean(cb) {
// body omitted
cb();
}
function css(cb) {
// body omitted
cb();
}
function javascript(cb) {
// body omitted
cb();
}
exports.build = series(clean, parallel(css, javascript));