创建自定义注册表#

¥Creating Custom Registries

允许将自定义注册表插入到任务系统中,该系统可以提供共享任务或增强功能。注册表是使用 registry() 注册的。

¥Allows custom registries to be plugged into the task system, which can provide shared tasks or augmented functionality. Registries are registered using registry().

结构#

¥Structure

为了被 gulp 接受,自定义注册表必须遵循特定的格式。

¥In order to be accepted by gulp, custom registries must follow a specific format.

// as a function
function TestRegistry() {}
TestRegistry.prototype.init = function (gulpInst) {}
TestRegistry.prototype.get = function (name) {}
TestRegistry.prototype.set = function (name, fn) {}
TestRegistry.prototype.tasks = function () {}
// as a class
class TestRegistry {
init(gulpInst) {}
get(name) {}
set(name, fn) {}
tasks() {}
}

如果传递给 registry() 的注册表实例没有全部四个方法,则会抛出错误。

¥If a registry instance passed to registry() doesn't have all four methods, an error will be thrown.

注册#

¥Registration

如果我们想注册上面的示例注册表,我们需要将其实例传递给 registry()

¥If we want to register our example registry from above, we will need to pass an instance of it to registry().

const { registry } = require('gulp');
// ... TestRegistry setup code
// good!
registry(new TestRegistry())
// bad!
registry(TestRegistry())
// This will trigger an error: 'Custom registries must be instantiated, but it looks like you passed a constructor'

方法#

¥Methods

init(gulpInst)#

注册表的 init() 方法在 registry() 函数的最后调用。作为唯一参数 (gulpInst) 传递的 gulp 实例可用于使用 gulpInst.task(taskName, fn) 预定义任务。

¥The init() method of a registry is called at the very end of the registry() function. The gulp instance passed as the only argument (gulpInst) can be used to pre-define tasks using gulpInst.task(taskName, fn).

参数#

¥Parameters

参数typenote
gulpInstobjectgulp 的实例。

get(name)#

get() 方法接收任务 name 供自定义注册表解析并返回,如果不存在具有该名称的任务,则接收 undefined

¥The get() method receives a task name for the custom registry to resolve and return, or undefined if no task with that name exists.

参数#

¥Parameters

参数typenote
namestring要检索的任务的名称。

set(name, fn)#

set() 方法接收任务 namefn。这由 task() 内部调用,以向自定义注册表提供用户注册的任务。

¥The set() method receives a task name and fn. This is called internally by task() to provide user-registered tasks to custom registries.

参数#

¥Parameters

参数typenote
namestring要设置的任务的名称。
fnfunction待设置的任务功能。

tasks()#

必须返回一个列出注册表中所有任务的对象。

¥Must return an object listing all tasks in the registry.

用例#

¥Use Cases

共享任务#

¥Sharing Tasks

要与所有项目共享常见任务,你可以在注册表上公开 init 方法,它将接收 gulp 实例作为唯一参数。然后你可以使用 gulpInst.task(name, fn) 来注册预定义任务。

¥To share common tasks with all your projects, you can expose an init method on the registry and it will receive an instance of gulp as the only argument. You can then use gulpInst.task(name, fn) to register pre-defined tasks.

例如,你可能想要共享 clean 任务:

¥For example, you might want to share a clean task:

const fs = require('fs');
const util = require('util');
const DefaultRegistry = require('undertaker-registry');
const del = require('del');
function CommonRegistry(opts){
DefaultRegistry.call(this);
opts = opts || {};
this.buildDir = opts.buildDir || './build';
}
util.inherits(CommonRegistry, DefaultRegistry);
CommonRegistry.prototype.init = function(gulpInst) {
const buildDir = this.buildDir;
const exists = fs.existsSync(buildDir);
if(exists){
throw new Error('Cannot initialize common tasks. ' + buildDir + ' directory exists.');
}
gulpInst.task('clean', function(){
return del([buildDir]);
});
}
module.exports = CommonRegistry;

然后在项目中使用它:

¥Then to use it in a project:

const { registry, series, task } = require('gulp');
const CommonRegistry = require('myorg-common-tasks');
registry(new CommonRegistry({ buildDir: '/dist' }));
task('build', series('clean', function build(cb) {
// do things
cb();
}));

共享功能#

¥Sharing Functionality

通过控制任务添加到注册表的方式,你可以装饰它们。

¥By controlling how tasks are added to the registry, you can decorate them.

例如,如果你希望所有任务共享某些数据,则可以使用自定义注册表将它们绑定到该数据。请务必返回更改后的任务,按照上面注册表方法的描述:

¥For example, if you wanted all tasks to share some data, you can use a custom registry to bind them to that data. Be sure to return the altered task, as per the description of registry methods above:

const { registry, series, task } = require('gulp');
const util = require('util');
const DefaultRegistry = require('undertaker-registry');
// Some task defined somewhere else
const BuildRegistry = require('./build.js');
const ServeRegistry = require('./serve.js');
function ConfigRegistry(config){
DefaultRegistry.call(this);
this.config = config;
}
util.inherits(ConfigRegistry, DefaultRegistry);
ConfigRegistry.prototype.set = function set(name, fn) {
var bound = fn.bind(this.config);
// Preserve internal properties and task metadata.
var task = Object.assign(bound, fn);
// The `DefaultRegistry` uses `this._tasks` for storage.
this._tasks[name] = task;
return task;
};
registry(new BuildRegistry());
registry(new ServeRegistry());
// `registry` will reset each task in the registry with
// `ConfigRegistry.prototype.set` which will bind them to the config object.
registry(new ConfigRegistry({
src: './src',
build: './build',
bindTo: '0.0.0.0:8888'
}));
task('default', series('clean', 'build', 'serve', function(cb) {
console.log('Server bind to ' + this.bindTo);
console.log('Serving' + this.build);
cb();
}));

示例#

¥Examples