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
55const MethodService = require ( './MethodService' ) ;
66const PluginAPI = require ( '../../PluginAPI' ) ;
77const DEFAULT_METHODS = require ( '../methods' ) ;
88const 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
1113class 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;
560608function 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,
0 commit comments