pinus 与 pomelo加载插件的机制有所不同
pinus的 app.use 需要实现 IPlugin 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
export interface IPlugin extends ILifeCycle {
name: string;
components ?: ComponentContructor[];
events ?: ApplicationEventContructor[];
handlerPath ?: string;
remoterPath?: string;
cronPath ?: string; }
|
pomelo的app.use ,插件需要 实现这样的结构:
1 2 3 4
| module.exports = { components: __dirname + '/lib/components/', events: __dirname + '/lib/events' };
|
通过以上的对比可以看到插件的主要区别是 components
和 events
两个结构的区别,pinus需要的是 数组,而pomelo只是需要一个文件路径,内部去加载路径内的文件。
所以pinus要使用pomelo的插件 需要做的就是 把 components 与 events字段转换一下就行了
下面以 pomelo-status-plugin 插件为例。 当然最后也可以写一个通用的转换工具函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const pomeloStatusPlugin = require('pomelo-status-plugin')
const componentsPath = pomeloStatusPlugin.components pomeloStatusPlugin.components = [require(componentsPath+'/status')] const eventsPath = pomeloStatusPlugin.events pomeloStatusPlugin.events = [require(eventsPath+'/event')] pomeloStatusPlugin.name = 'pomelo-status-plugin'
let app = pinus.createApp();
app.use(pomeloStatusPlugin)
|
PS: 以上代码手写,未经过测试。理论上应该没问题。