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

Skip to content

Commit d5f9059

Browse files
cemremengufengmk2
authored andcommitted
docs: translation for basic plugin (eggjs#2166)
1 parent 7afc7e2 commit d5f9059

1 file changed

Lines changed: 176 additions & 1 deletion

File tree

docs/source/en/basics/plugin.md

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,176 @@
1-
this document is still waiting for translation, see [Chinese Version](/zh-cn/basics/plugin.html)
1+
## title: Plugin
2+
3+
Plugin mechanism is a major feature of our framework. Not only can it ensure that the core of the framework is sufficiently streamlined, stable and efficient, but also can promote the reuse of business logic and the formation of an ecosystem. In the following sections, we will try to answer questions such as
4+
5+
* Koa already has a middleware mechanism, why plugins?
6+
* What are the differences/relationship between middleware and plugins?
7+
* How do I use a plugin?
8+
* How do I write a plugin?
9+
* ...
10+
11+
## Why plugins?
12+
13+
Here are some of the issues we think that can arise when using Koa middleware:
14+
15+
1. Middleware loading is sequential and it is the user's responsibility to setup the execution sequence since middleware mechanism can not manage the actual order. This, in fact, is not very friendly.
16+
When the order is not correct, it can lead to unexpected results.
17+
2. Middleware positioning is geared towards intercepting user requests to add additional logic before or after such as: authentication, security checks, logging and so on. However, in some cases, such functionality can be unrelated to the request, for example, timing tasks, message subscriptions, back-end logic and so on.
18+
3. Some features include very complex initialization logic that needs to be done at application startup. This is obviously not suitable for middleware to achieve.
19+
20+
To sum up, we need a more powerful mechanism to manage, orchestrate those relatively independent business logic.
21+
22+
### The relationship between middleware, plugins and application
23+
24+
A plugin is actually a "mini-application", almost the same as an app:
25+
26+
* It contains [Services](./service.md), [middleware] (./ middleware.md), [config](./config.md), [framework extensions] (./ extend.md), etc.
27+
* It does not have separate [Router] (./ router.md) and [Controller](./controller.md).
28+
29+
Their relationship is:
30+
31+
* Applications can be directly introduced into Koa's middleware.
32+
* When it comes to the scene mentioned in the previous section, the app needs to import the plugin.
33+
* The plugin itself can contain middleware.
34+
* Multiple plugins can be wrapped as an [upper frame] (../ advanced / framework.md).
35+
36+
## Using plugins
37+
38+
Plugins are usually added via the npm module:
39+
40+
```bash
41+
$ npm i egg-mysql --save
42+
```
43+
44+
** Note: We recommend introducing dependencies in the `^` way, and locking versions are strongly discouraged. **
45+
46+
```json
47+
{
48+
"dependencies": {
49+
"egg-mysql": "^ 3.0.0"
50+
}
51+
}
52+
```
53+
54+
Then you need to declare it in the `config / plugin.js` application or framework:
55+
56+
```js
57+
// config / plugin.js
58+
// Use mysql plug-in
59+
exports.mysql = {
60+
enable: true,
61+
package: 'egg-mysql'
62+
};
63+
```
64+
65+
You can directly use the functionality provided by the plugin:
66+
67+
```js
68+
app.mysql.query(sql, values);
69+
```
70+
71+
### Configuring plugins
72+
73+
Each configuration item in `plugin.js` supports:
74+
75+
* `{Boolean} enable` - Whether to enable this plugin, the default is true
76+
* `{String} package` -`npm` module name, plugin is imported via `npm` module
77+
* `{String} path` - The plugin's absolute path, mutually exclusive with package configuration
78+
* `{Array} env` - Only enable plugin in the specified runtime (environment), overriding the plugin's own configuration in `package.json`
79+
80+
### Enabling/Disabling plugins
81+
82+
The application does not need the package or path configuration when using the plugins built in the upper framework. You only need to specify whether they are enabled or not:
83+
84+
```js
85+
// For the built-in plugin, you can use the following simple way to turn on or off
86+
exports.onerror = false;
87+
```
88+
89+
### Environment configuration
90+
91+
We also support `plugin.{Env}.js`, which will load plugin configurations based on [Runtime](../basics/env.md).
92+
93+
For example, if you want to load the plugin `egg-dev` only in the local environment, you can install it to `devDependencies` and adjust the plugin configuration accordingly.
94+
95+
```js
96+
// npm i egg-dev --save-dev
97+
// package.json
98+
{
99+
  "devDependencies": {
100+
    "egg-dev": "*"
101+
  }
102+
}
103+
```
104+
105+
Then declare in `plugin.local.js`:
106+
107+
```js
108+
// config / plugin.local.js
109+
exports.dev = {
110+
enable: true,
111+
package: 'egg-dev'
112+
};
113+
```
114+
115+
In this way, `npm i --production` in the production environment does not need to download the`egg-dev` package.
116+
117+
** Note: **
118+
119+
* `plugin.default.js` does not exists. Use `local` for dev environments.
120+
121+
* Use this feature only in the application layer. Do not use it in the framework layer.
122+
123+
### Package name and path
124+
125+
* The `package` is introduced in the `npm` style which is the most common way to import
126+
* `path` is an absolute path introduced when you want to load the plugin from different location such as when a plugin is still at the development stage or not available on `npm`
127+
* To see the application of these two scenarios, please see [progressive development] (../ tutorials / progressive.md).
128+
129+
```js
130+
// config / plugin.js
131+
const path = require ('path');
132+
exports.mysql = {
133+
  enable: true,
134+
  package: path.join (__ dirname, '../lib/plugin/egg-mysql'),
135+
};
136+
```
137+
138+
## Plugin configuration
139+
140+
The plugin will usually contain its own default configuration, you can overwrite this in `config.default.js`:
141+
142+
```js
143+
// config / config.default.js
144+
exports.mysql = {
145+
client: {
146+
host: 'mysql.com',
147+
port: '3306',
148+
user: 'test_user',
149+
password: 'test_password',
150+
database: 'test'
151+
}
152+
};
153+
```
154+
155+
Specific consolidation rules can be found in [Configuration] (. / Config.md).
156+
157+
## Plugin list
158+
159+
* Framework has default built-in plugins for enterprise applications [Common plugins](https://eggjs.org/zh-cn/plugins/):
160+
  - [onerror](https://github.com/eggjs/egg-onerror) Uniform Exception Handling
161+
  - [Session](https://github.com/eggjs/egg-session) Session implementation
162+
  - [i18n](https://github.com/eggjs/egg-i18n) Multilingual
163+
  - [watcher](https://github.com/eggjs/egg-watcher) File and folder monitoring
164+
  - [multipart](https://github.com/eggjs/egg-multipart) File Streaming Upload
165+
  - [security](https://github.com/eggjs/egg-security) Security
166+
  - [development](https://github.com/eggjs/egg-development) Development Environment Configuration
167+
  - [logrotator](https://github.com/eggjs/egg-logrotator) Log segmentation
168+
  - [schedule](https://github.com/eggjs/egg-schedule) Timing tasks
169+
  - [static](https://github.com/eggjs/egg-static) Static server
170+
  - [jsonp](https://github.com/eggjs/egg-jsonp) jsonp support
171+
  - [view](https://github.com/eggjs/egg-view) Template Engine
172+
* More community plugins can be found on GitHub [egg-plugin](https://github.com/topics/egg-plugin).
173+
174+
## Developing a plugin
175+
176+
See the documentation [plugin development](../advanced/plugin.md).

0 commit comments

Comments
 (0)