Skip to content

Commit efc19f5

Browse files
committed
feat: add before register
1 parent 740b8bb commit efc19f5

5 files changed

Lines changed: 92 additions & 12 deletions

File tree

src/core/Constants/SharedProps.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
'use strict';
22

3-
// TODO 可以增加注释
43
// service 对 pluginAPI 暴露的所有方法
5-
module.exports = [
4+
5+
// 关于注册的方法
6+
const registerKeys = [
7+
'registerCommand',
8+
'registerMethod',
9+
];
10+
const extendKeys = [
11+
'extendConfig',
12+
'extendMethod',
13+
];
14+
15+
module.exports = [].concat(registerKeys, extendKeys, [
616

717
'root',
818
'mode',
@@ -29,11 +39,7 @@ module.exports = [
2939
'findPlugin',
3040
// plugin method
3141
'register',
32-
'registerMethod',
33-
'registerCommand',
3442
'changeCommandOption',
35-
'extendConfig',
36-
'extendMethod',
3743
// new v0.3
3844
'resolve',
3945
'resolveWorkspace',
@@ -48,4 +54,7 @@ module.exports = [
4854
'API_TYPE',
4955
'hasKey',
5056

51-
];
57+
]);
58+
59+
module.exports.REGISTER_KEYS = registerKeys;
60+
module.exports.EXTEND_KEYS = extendKeys;

src/core/Service/Service.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Service extends PluginService {
1919
// 先判断是否存在 symlink, 如果存在则不需要走这个.
2020
const microsPaths = Object.values(microsConfig)
2121
.filter(item => item.hasSoftLink && microsExtraConfig[item.key] && !!microsExtraConfig[item.key].link)
22-
.map(item => item.nodeModulesPath);
22+
.map(item => item.nodeModulesPath)
23+
.sort();
2324
moduleAlias.addPaths(microsPaths);
2425
}
2526

src/core/Service/libs/MethodService.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,15 @@ class MethodService extends BaseService {
203203

204204
setState(key, value) {
205205
this.state[key] = value;
206+
return value;
206207
}
207208

208209
getState(key, value) {
209-
return this.state[key] || value;
210+
const v = this.state[key];
211+
if (_.isUndefined(v)) {
212+
return value;
213+
}
214+
return v;
210215
}
211216

212217
resolve(..._paths) {
@@ -217,6 +222,10 @@ class MethodService extends BaseService {
217222
return this.resolve(CONSTANTS.MICRO_APP_DIR, ..._paths);
218223
}
219224

225+
resolveTemp(..._paths) {
226+
return this.resolve(this.tempDir, ..._paths);
227+
}
228+
220229
assertExtendOptions(name, opts, fn) {
221230
assert(typeof name === 'string', 'name must be string.');
222231
assert(name || /^_/i.test(name), `${name} cannot begin with '_'.`);

src/core/Service/libs/PluginService.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

3-
const { logger, _, assert, tryRequire, virtualFile, dedent } = require('@micro-app/shared-utils');
3+
const { logger, _, assert, tryRequire, virtualFile, dedent, yUnParser } = require('@micro-app/shared-utils');
44

55
const MethodService = require('./MethodService');
66
const PluginAPI = require('../../PluginAPI');
77
const DEFAULT_METHODS = require('../methods');
88
const PreLoadPlugins = require('../../../plugins/register');
9-
const { API_TYPE } = require('../../Constants');
9+
const { API_TYPE, SHARED_PROPS: { REGISTER_KEYS, EXTEND_KEYS } } = require('../../Constants');
10+
const BEFORE_INIT_METHODS = [].concat(REGISTER_KEYS, EXTEND_KEYS);
11+
const BEFORE_INIT_METHODS_SYMBOL = Symbol('$$beforeInitMethods$$');
1012

1113
class PluginService extends MethodService {
1214
constructor(context) {
@@ -89,13 +91,14 @@ class PluginService extends MethodService {
8991
* @return {Boolean} true-enabled, false-disabled
9092
*/
9193
_checkPluginEnabled(plugin) {
92-
const { id, alias, mode, target, skipTarget, dependencies } = plugin;
94+
const { id, alias, mode, target, skipTarget, dependencies, skipContext } = plugin;
9395
const key = `${id}${alias ? ' (' + alias + ')' : ''}`;
9496

9597
const params = { // function params
9698
id, alias,
9799
mode: this.mode,
98100
target: this.target,
101+
context: this.context,
99102
};
100103

101104
// 判断依赖插件库是否存在
@@ -152,6 +155,22 @@ class PluginService extends MethodService {
152155
}
153156
}
154157

158+
// 某个 context 为 true 时,跳过此插件
159+
if (skipContext) {
160+
let _skipContext = skipContext;
161+
if (_.isFunction(_skipContext)) { // 支持方法判断
162+
_skipContext = _skipContext(params);
163+
}
164+
_skipContext = [].concat(_skipContext);
165+
const unCtx = yUnParser(this.context); // 解压
166+
const args = _skipContext.find(item => unCtx.includes(item));
167+
if (args) {
168+
// 当前 target 与插件不匹配,需要跳过
169+
logger.info('[Plugin]', `has args: { ${args} } - initPlugin() skip "${key}".`);
170+
return false;
171+
}
172+
}
173+
155174
return true; // OK
156175
}
157176

@@ -172,6 +191,26 @@ class PluginService extends MethodService {
172191
return;
173192
}
174193

194+
// 收集提前注册的内容
195+
const _register = {};
196+
BEFORE_INIT_METHODS.forEach(method => {
197+
_register[method] = _register[method] || [];
198+
const item = plugin[BEFORE_INIT_METHODS_SYMBOL][method];
199+
if (item && _.isPlainObject(item)) {
200+
Object.keys(item).forEach(key => {
201+
const opts = Object.assign({}, item[key] || {});
202+
let fn;
203+
if (_.isFunction(opts.fn)) {
204+
fn = opts.fn;
205+
delete opts.fn;
206+
}
207+
// 参数依次为:name,opts,fn
208+
_register[method].push([ key, opts, fn ]);
209+
});
210+
}
211+
});
212+
213+
175214
assert(typeof apply === 'function',
176215
dedent`plugin "${id}" must export a function,
177216
e.g.
@@ -189,6 +228,15 @@ class PluginService extends MethodService {
189228
);
190229
plugin._onOptionChange = fn;
191230
};
231+
232+
// 将收集的内容进行分销
233+
Object.keys(_register).forEach(method => {
234+
const _argss = _register[method];
235+
_argss.forEach(args => {
236+
api[method].apply(api, args);
237+
});
238+
});
239+
192240
return api;
193241
}
194242

@@ -560,8 +608,10 @@ module.exports = PluginService;
560608
function resolvePluginResult(item, { apply, link, opts }) {
561609
const _apply = apply.default || apply;
562610
const defaultConfig = apply.configuration || {};
611+
const beforeInitMethods = _.pick(apply, BEFORE_INIT_METHODS) || {};
563612
return Object.assign({}, defaultConfig, {
564613
...item,
614+
[BEFORE_INIT_METHODS_SYMBOL]: beforeInitMethods, // 内部方法提前
565615
link: link ? require.resolve(link) : null,
566616
apply: _apply,
567617
opts,

test/testPlugin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ module.exports = function(api, opts) {
2121
api.onInitDone(() => {
2222
console.log('init Done2', api.getState('webpackConfig'));
2323
});
24+
25+
api.runCommand('test');
26+
};
27+
28+
module.exports.registerCommand = {
29+
test: {
30+
ccc: '123',
31+
fn: async arg => {
32+
console.warn('arg: ', arg);
33+
},
34+
},
2435
};

0 commit comments

Comments
 (0)