Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gxcsoccer
Copy link
Contributor

@gxcsoccer gxcsoccer commented Jan 10, 2017

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

@mention-bot
Copy link

@gxcsoccer, thanks for your PR! By analyzing the history of the files in this pull request, we identified @popomore to be a potential reviewer.

@popomore
Copy link
Member

就叫 plugin.md?

@popomore popomore mentioned this pull request Jan 10, 2017
31 tasks
@gxcsoccer gxcsoccer force-pushed the doc-plugin branch 6 times, most recently from b853a8e to d5e638b Compare January 10, 2017 09:36
@@ -0,0 +1,361 @@

title: 插件机制
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

插件开发


## 前言

插件机制是我们框架的一大特色。它不但可以保证框架核心的足够精简、稳定、高效,还可以促进业务逻辑的复用,生态圈的形成。有同学可能会问了
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有同学可能会问了

有人可能会问了

接下来我们就来逐一讨论

## 为什么要插件?
Copy link
Member

@fengmk2 fengmk2 Jan 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问号可以去掉

| ├── context.js (可选)
| ├── application.js (可选)
| └── agent.js (可选)
│ ├── proxy (可选)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proxy 开源版本不自带


那区别在哪儿呢?

1. 插件没有独立的 router 和 controller。这主要出于几点考虑:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多了空格

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1. 插

- `{String} name` - 插件名(必须配置),具有唯一性,配置依赖关系时会指定依赖插件的 name。
- `{Array} dependencies` - 当前插件强依赖的插件列表(如果依赖的插件没找到,应用启动失败)
- `{Array} optionalDependencies` - 当前插件的可选依赖插件列表(如果依赖的插件未开启,只会 warning,不会影响应用启动)
- `{Array} env` - 只有在指定运行环境才能开启,具体有哪些环境可以参考 [运行环境](../basics/env.md)
Copy link
Member

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 对象
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper.js 也是可以 extend 的


1. 首先在 `app/middleware` 目录下定义好中间件实现

```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码块缩进有点大,应该2空格即可

return staticCache(options);
};
```
2.`${plugin_root}/app.js` 中将中间件插入到合适的位置(例如:下面将 static 中间件放到 bodyParser 之前)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${plugin_root}/ 不需要写了,直接写 app.js

// plugins/static/app.js
const assert = require('assert');

module.exports = function (app) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module.exports = app => {

2.`${plugin_root}/app.js` 中将中间件插入到合适的位置(例如:下面将 static 中间件放到 bodyParser 之前)

```js
// plugins/static/app.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个注释可以删除了。

const fs = require('fs');
const path = require('path');

module.exports = function(app) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrow

};
```

- 如果有异步启动逻辑,可以使用 generator 方式
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要提同步异步了,都告诉读者写 generator function 吧

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不行,这里得改改。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改一下:如果是异步启动逻辑,那么可以在 app/init.js 通过 generator 来实现

@gxcsoccer gxcsoccer force-pushed the doc-plugin branch 2 times, most recently from edab2ba to 5372b15 Compare January 10, 2017 14:42
@gxcsoccer
Copy link
Contributor Author

都改了,请 review


```js
// ${baseDir}/agent.js
// ${app_root}/agent.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${app_root}/ 可以不用写的。

- `{String} name` - 插件名(必须配置),具有唯一性,配置依赖关系时会指定依赖插件的 name。
- `{Array} dependencies` - 当前插件强依赖的插件列表(如果依赖的插件没找到,应用启动失败)
- `{Array} optionalDependencies` - 当前插件的可选依赖插件列表(如果依赖的插件未开启,只会 warning,不会影响应用启动)
- `{Array} env` - 只有在指定运行环境才能开启,具体有哪些环境可以参考 [运行环境](../basics/env.md)。改配置是可选的,一般情况下都不需要配置
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改配置 => 此配置

module.exports = app => {
// 将 static 中间件放到 bodyParser 之前
const index = app.config.coreMiddleware.indexOf('bodyParser');
assert(index >= 0, 'bodyParser 中间件必须存在');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进是2空格

@fengmk2
Copy link
Member

fengmk2 commented Jan 10, 2017

其他 ok 了。

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fengmk2
Copy link
Member

fengmk2 commented Jan 10, 2017

@gxcsoccer 需要 rebase 一下

@codecov-io
Copy link

codecov-io commented Jan 11, 2017

Current coverage is 97.75% (diff: 100%)

No coverage report found for master at 01f9390.

Powered by Codecov. Last update 01f9390...8679743

@gxcsoccer gxcsoccer force-pushed the doc-plugin branch 2 times, most recently from eae86a1 to b410bec Compare January 11, 2017 03:21
@gxcsoccer gxcsoccer merged commit 1c65bde into master Jan 11, 2017
@gxcsoccer gxcsoccer deleted the doc-plugin branch January 11, 2017 03:25
@gxcsoccer
Copy link
Contributor Author

搞定

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants