-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor plugins #740
Merged
Merged
Refactor plugins #740
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api.register('addHtmlMeta', () => {
return { charset: 'utf-8' };
}) 是不是等于: api.addHtmlMeta({ charset: 'utf-8' }); |
|
整理下 api 接口方法,全部加在 api 上,开发者不需要感知到 service 。 方法
build 时存在的方法
dev 时存在的方法
属性
utils
|
…addMiddleware, api.addMiddlewareAhead
refact: umi-plugin-locale to new umi api
doc: update plugin doc
feat: add renderer wrapper with module
sorrycc
pushed a commit
that referenced
this pull request
Jun 23, 2022
xierenyuan
pushed a commit
to xierenyuan/umi
that referenced
this pull request
Jun 23, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
插件解析
插件分:
单个插件的格式为:
注册坑位(hooks)
语法:
实际上是往 service.pluginHooks 注册一个方法,pluginHooks 的格式为:
注册方法(在 api 上)
语法:
type 和 apply 二选一,type 为
api.API_TYPE.ADD
|api.API_TYPE.MODIFY
|api.API_TYPE.EVENT
的一项。比如:
注册方法,
调用,
执行插件,
背后实现,
配置变更
比如用户配置了,
解析为:
注册到 plugins 里。
初始化时,执行 apply 方法,如果是 dev 模式且有 api.onOptionChange 方法,添加到 plugin 对象里,即:
默认的 onOptionChange 行为是重新 devServer 。
然后用户修改配置,比如:
解析 plugins 配置为 newUserPlugins 列表,和之前的 plugins 过滤出来的 userPlugins 比较,
上面的例子,先设置 opts,
然后执行
onOptionChange(opts)
。额外注册插件及其配置变更
plugins 初始化执行 apply 时,如有
api.registerPlugin
,比如:则添加到 extraPlugins 里。
在 plugins 初始化 apply 完之后,对于 extraPlugins 再执行一遍初始化,初始化时添加到 plugins 里。递归执行,知道没有生成新的 extraPlugins 为止。(递归层级超过 10 级,报错提醒用户可能死循环)
然后检测到配置有变化,对比之前的配置,通过
api.changePluginOption(pluginId, option)
修改插件的 opts 。这会覆盖 plugins 里对应 pluginObject 之前的 opts,然后执行 onOptionChange,参数为新的 opts。使用举例,