The tasks in Gulp can be divided into public
(public) and private
(private) types.
- Public tasks:
gulpfile
are called public tasks, which can be directly calledgulp
- Private task: used internally, usually as part of the
series()
orparallel()
combination.
A private type of task is the same as other tasks in appearance and behavior, but cannot be called directly by the user. If you need to register a task as a public type, you only need to gulpfile.js
the task through export
from the 0609b518823885 file.
How to export tasks
We can use the export
gulpfile.js
file, so that the exported task becomes a public task, which can be directly called gulp
Example:
Look at the following example:
const { series } = require('gulp');
// clean函数并未被导出,因此clean是私有任务,可以被用在 series() 组合中
function clean(cb) {
cb();
}
// build 函数被导出了,因此它是一个公有任务,可以被 gulp 命令直接调用,它也可用在series()组合中
function build(cb) {
cb();
}
// 导出 build 函数
exports.build = build;
exports.default = series(clean, build);
Execute the gulp --tasks
command:
In previous gulp
versions, task()
method is used to register functions for the task. Although the API
can still be used, export will be the main registration mechanism, unless it is encountered that export
does not work.
Combination tasks
Gulp provides two powerful combination methods: series()
and parallel()
methods, these two methods allow multiple independent tasks to be combined into a larger operation. Both methods can accept any number of task functions or combined operations. series()
and parallel()
methods can be nested to any depth.
If you need to execute tasks in sequence, you can use the series()
method.
Example:
const { series } = require('gulp');
function one(cb) {
console.log("one")
cb();
}
function two(cb) {
console.log("two")
cb();
}
exports.build = series(one, two);
Execute the gulp build
command:
As you can see in the picture, the order in which the tasks are executed is one > two > build
.
If you want to run tasks with maximum concurrency, you can use the parallel()
method to combine them:
const { parallel } = require('gulp');
function one(cb) {
console.log("one")
cb();
}
function two(cb) {
console.log("two")
cb();
}
exports.build = parallel(one, two);
When series()
or parallel()
is called, the tasks are immediately grouped together. This allows changes to be made in the combination without the need for conditional judgments in a single task.
Example:
const { series } = require('gulp');
function one(cb) {
cb();
}
function two(cb) {
cb();
}
function three(cb) {
cb();
}
if (process.env.NODE_ENV === 'production') {
exports.build = series(two, one);
} else {
exports.build = series(two, three);
}
The above code indicates that if it is executed in a production environment, run exports.build = series(two, one)
, otherwise run exports.build = series(two, three)
, let's take a look at the execution result:series()
and parallel()
can be nested to any depth:
const { series, parallel } = require('gulp');
function one(cb) {
cb();
}
function two(cb) {
cb();
}
function three(cb) {
cb();
}
function four(cb) {
cb();
}
function five(cb) {
cb();
}
exports.build = series(
one,
parallel(
two,
series(three, four)
),
five
);
Excuting an order:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。