Skip to content

Commit c410e4f

Browse files
committed
enhance: plugin config
1 parent 0401b84 commit c410e4f

4 files changed

Lines changed: 60 additions & 42 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"jest": "^24.9.0"
5656
},
5757
"dependencies": {
58-
"@micro-app/shared-utils": "^0.1.22",
58+
"@micro-app/shared-utils": "^0.1.24",
5959
"dotenv": "^8.2.0",
6060
"dotenv-expand": "^5.1.0"
6161
},

src/core/MicroAppConfig/libs/BaseConfig.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { _, loadFile, logger, stringifyObject, path, pluginResolution } = require('@micro-app/shared-utils');
44
const CONSTANTS = require('../../Constants');
55
const validateSchema = require('../../../utils/validateSchema');
6+
const parsePlugin = require('../../../utils/parsePlugin');
67

78
const SCHEMA = require('./configSchema');
89

@@ -331,35 +332,7 @@ class BaseConfig {
331332
pkg.devDependencies || {});
332333
_plugins = Object.keys(deps).filter(pluginResolution.isPlugin);
333334
}
334-
return _plugins.map(p => {
335-
let opts;
336-
let id;
337-
let others;
338-
if (Array.isArray(p)) {
339-
opts = p[1];
340-
if (_.isPlainObject(p[0])) {
341-
others = p[0];
342-
id = p[0].id;
343-
p = p[0].link;
344-
} else {
345-
p = id = p[0];
346-
}
347-
} else if (_.isPlainObject(p)) {
348-
others = p;
349-
id = p.id;
350-
p = p.link;
351-
}
352-
id = id || p;
353-
if (p && id === p) {
354-
p = null; // 不希望相等
355-
}
356-
return {
357-
...(others || {}),
358-
id,
359-
link: p,
360-
opts: opts || {},
361-
};
362-
});
335+
return _plugins.map(item => parsePlugin(item));
363336
}
364337

365338
// 这是一个参与合并,不进行校验的对象(提供参数扩展使用)

src/core/Service/libs/PluginService.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const PluginAPI = require('../../PluginAPI');
77
const DEFAULT_METHODS = require('../methods');
88
const PreLoadPlugins = require('../../../plugins/register');
99
const { API_TYPE, SHARED_PROPS: { BEFORE_INIT_METHODS } } = require('../../Constants');
10+
const parsePlugin = require('../../../utils/parsePlugin');
11+
1012
const SYMBOL_BEFORE_INIT_METHODS = Symbol('$$beforeInitMethods$$');
1113

1214
class PluginService extends MethodService {
@@ -31,14 +33,18 @@ class PluginService extends MethodService {
3133
const allplugins = micros.map(key => {
3234
return this.microsConfig[key].plugins || [];
3335
}).concat(plugins);
34-
const pluginsObj = allplugins.reduce((arr, item) => {
35-
if (Array.isArray(item)) {
36-
return arr.concat(item.reduce((_arr, _item) => {
37-
return _arr.concat(this.resolvePlugin(_item));
38-
}, []));
36+
const pluginsObj = [];
37+
while (allplugins.length) {
38+
const item = allplugins.shift();
39+
if (Array.isArray(item)) { // presets
40+
allplugins.unshift(...item);
41+
} else {
42+
const res = this.resolvePlugin(item);
43+
if (res) {
44+
pluginsObj.push(...[].concat(res));
45+
}
3946
}
40-
return arr.concat(this.resolvePlugin(item));
41-
}, []).filter(item => !!item);
47+
}
4248
const collection = new Set();
4349
const result = pluginsObj.filter(item => {
4450
// 去除已经注册的插件
@@ -578,12 +584,17 @@ class PluginService extends MethodService {
578584
if (apply) {
579585
const _apply = apply.default || apply;
580586
if (Array.isArray(_apply)) { // 支持数组模式
581-
return _apply.map(_applyItem => {
582-
if (_applyItem) {
583-
return this._resolvePluginResult(item, { apply: _applyItem, link, opts });
587+
return _apply.reduce((arr, _applyItem) => {
588+
if (_.isFunction(_applyItem)) {
589+
const res = this._resolvePluginResult(item, { apply: _applyItem, link, opts });
590+
return arr.concat(res || []);
591+
} else if (_applyItem) {
592+
const _parsePlugin = parsePlugin(_applyItem);
593+
const res = this.resolvePlugin(Object.assign(item, _parsePlugin));
594+
return arr.concat(res || []);
584595
}
585-
return false;
586-
}).filter(_it => !!_it);
596+
return arr;
597+
}, []).filter(_it => !!_it);
587598
}
588599
return this._resolvePluginResult(item, { apply, link, opts });
589600
}

src/utils/parsePlugin/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const { _ } = require('@micro-app/shared-utils');
4+
5+
module.exports = function parsePlugin(p) {
6+
let opts;
7+
let id;
8+
let others;
9+
if (Array.isArray(p)) {
10+
opts = p[1];
11+
if (_.isPlainObject(p[0])) {
12+
others = p[0];
13+
id = p[0].id;
14+
p = p[0].link;
15+
} else {
16+
p = id = p[0];
17+
}
18+
} else if (_.isPlainObject(p)) {
19+
others = p;
20+
id = p.id;
21+
p = p.link;
22+
}
23+
id = id || p;
24+
if (p && id === p) {
25+
p = null; // 不希望相等
26+
}
27+
return {
28+
...(others || {}),
29+
id,
30+
link: p,
31+
opts: opts || {},
32+
};
33+
};
34+

0 commit comments

Comments
 (0)