-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
doc: add plugin doc #224
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
doc: add plugin doc #224
Conversation
|
@gxcsoccer, thanks for your PR! By analyzing the history of the files in this pull request, we identified @popomore to be a potential reviewer. |
97021d6 to
44570f4
Compare
|
就叫 plugin.md? |
b853a8e to
d5e638b
Compare
docs/source/zh-cn/advanced/plugin.md
Outdated
| @@ -0,0 +1,361 @@ | |||
|
|
|||
| title: 插件机制 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件开发
docs/source/zh-cn/advanced/plugin.md
Outdated
|
|
||
| ## 前言 | ||
|
|
||
| 插件机制是我们框架的一大特色。它不但可以保证框架核心的足够精简、稳定、高效,还可以促进业务逻辑的复用,生态圈的形成。有同学可能会问了 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有同学可能会问了
有人可能会问了
docs/source/zh-cn/advanced/plugin.md
Outdated
| 接下来我们就来逐一讨论 | ||
|
|
||
| ## 为什么要插件? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
问号可以去掉
docs/source/zh-cn/advanced/plugin.md
Outdated
| │ | ├── context.js (可选) | ||
| │ | ├── application.js (可选) | ||
| │ | └── agent.js (可选) | ||
| │ ├── proxy (可选) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proxy 开源版本不自带
docs/source/zh-cn/advanced/plugin.md
Outdated
|
|
||
| 那区别在哪儿呢? | ||
|
|
||
| 1. 插件没有独立的 router 和 controller。这主要出于几点考虑: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多了空格
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. 插
docs/source/zh-cn/advanced/plugin.md
Outdated
| - `{String} name` - 插件名(必须配置),具有唯一性,配置依赖关系时会指定依赖插件的 name。 | ||
| - `{Array} dependencies` - 当前插件强依赖的插件列表(如果依赖的插件没找到,应用启动失败) | ||
| - `{Array} optionalDependencies` - 当前插件的可选依赖插件列表(如果依赖的插件未开启,只会 warning,不会影响应用启动) | ||
| - `{Array} env` - 只有在指定运行环境才能开启,具体有哪些环境可以参考 [运行环境](../basics/env.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里重点说明一下,一般情况下都不需要配置
| - `app/extend/response.js` - 扩展 koa#Response 对象 | ||
| - `app/extend/context.js` - 扩展 koa#Context 对象 | ||
| - `app/extend/application.js` - 扩展 app 对象 | ||
| - `app/extend/agent.js` - 扩展 agent 对象 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
helper.js 也是可以 extend 的
docs/source/zh-cn/advanced/plugin.md
Outdated
|
|
||
| 1. 首先在 `app/middleware` 目录下定义好中间件实现 | ||
|
|
||
| ```js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码块缩进有点大,应该2空格即可
docs/source/zh-cn/advanced/plugin.md
Outdated
| return staticCache(options); | ||
| }; | ||
| ``` | ||
| 2. 在 `${plugin_root}/app.js` 中将中间件插入到合适的位置(例如:下面将 static 中间件放到 bodyParser 之前) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${plugin_root}/ 不需要写了,直接写 app.js
docs/source/zh-cn/advanced/plugin.md
Outdated
| // plugins/static/app.js | ||
| const assert = require('assert'); | ||
|
|
||
| module.exports = function (app) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module.exports = app => {
docs/source/zh-cn/advanced/plugin.md
Outdated
| 2. 在 `${plugin_root}/app.js` 中将中间件插入到合适的位置(例如:下面将 static 中间件放到 bodyParser 之前) | ||
|
|
||
| ```js | ||
| // plugins/static/app.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个注释可以删除了。
docs/source/zh-cn/advanced/plugin.md
Outdated
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| module.exports = function(app) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrow
docs/source/zh-cn/advanced/plugin.md
Outdated
| }; | ||
| ``` | ||
|
|
||
| - 如果有异步启动逻辑,可以使用 generator 方式 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要提同步异步了,都告诉读者写 generator function 吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不行,这里得改改。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里改一下:如果是异步启动逻辑,那么可以在 app/init.js 通过 generator 来实现
edab2ba to
5372b15
Compare
|
都改了,请 review |
|
|
||
| ```js | ||
| // ${baseDir}/agent.js | ||
| // ${app_root}/agent.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${app_root}/ 可以不用写的。
docs/source/zh-cn/advanced/plugin.md
Outdated
| - `{String} name` - 插件名(必须配置),具有唯一性,配置依赖关系时会指定依赖插件的 name。 | ||
| - `{Array} dependencies` - 当前插件强依赖的插件列表(如果依赖的插件没找到,应用启动失败) | ||
| - `{Array} optionalDependencies` - 当前插件的可选依赖插件列表(如果依赖的插件未开启,只会 warning,不会影响应用启动) | ||
| - `{Array} env` - 只有在指定运行环境才能开启,具体有哪些环境可以参考 [运行环境](../basics/env.md)。改配置是可选的,一般情况下都不需要配置 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改配置 => 此配置
docs/source/zh-cn/advanced/plugin.md
Outdated
| module.exports = app => { | ||
| // 将 static 中间件放到 bodyParser 之前 | ||
| const index = app.config.coreMiddleware.indexOf('bodyParser'); | ||
| assert(index >= 0, 'bodyParser 中间件必须存在'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
缩进是2空格
|
其他 ok 了。 |
fengmk2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@gxcsoccer 需要 rebase 一下 |
Current coverage is 97.75% (diff: 100%)
|
eae86a1 to
b410bec
Compare
|
搞定 |
Checklist
npm testpassesAffected core subsystem(s)
Description of change