site/docs/basics/plugin.md
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
Here are some of the issues we think that can arise when using Koa middleware:
To sum up, we need a more powerful mechanism to manage, orchestrate those relatively independent business logic.
A plugin is actually a "mini-application", almost the same as an app:
plugin.js, it could only define dependencies with others, but could not deside whether other plugin is enable or not.Their relationship is:
Plugins are usually added via the npm module:
$ npm i egg-mysql --save
Note: We recommend introducing dependencies in the ^ way, and locking versions are strongly discouraged.
{
"dependencies": {
"egg-mysql": "^ 3.0.0"
}
}
Then you need to declare it in the config / plugin.js application or framework:
// config / plugin.js
// Use mysql plugin
exports.mysql = {
enable: true,
package: 'egg-mysql',
};
You can directly use the functionality provided by the plugin:
app.mysql.query(sql, values);
Each configuration item in plugin.js supports:
{Boolean} enable - Whether to enable this plugin, the default is true{String} package -npm module name, plugin is imported via npm module{String} path - The plugin's absolute path, mutually exclusive with package configuration{Array} env - Only enable plugin in the specified runtime (environment), overriding the plugin's own configuration in package.jsonThe 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:
// For the built-in plugin, you can use the following simple way to turn on or off
exports.onerror = false;
We also support plugin.{Env}.js, which will load plugin configurations based on Runtime.
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.
// npm i egg-dev --save-dev
// package.json
{
"devDependencies": {
"egg-dev": "*"
}
}
Then declare in plugin.local.js:
// config / plugin.local.js
exports.dev = {
enable: true,
package: 'egg-dev',
};
In this way, npm i --production in the production environment does not need to download theegg-dev package.
Note:
plugin.default.js does not exists. Use local for dev environments.
Use this feature only in the application layer. Do not use it in the framework layer.
package is introduced in the npm style which is the most common way to importpath 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// config / plugin.js
const path = require('path');
exports.mysql = {
enable: true,
path: path.join(__dirname, '../lib/plugin/egg-mysql'),
};
The plugin will usually contain its own default configuration, you can overwrite this in config.default.js:
// config / config.default.js
exports.mysql = {
client: {
host: 'mysql.com',
port: '3306',
user: 'test_user',
password: 'test_password',
database: 'test',
},
};
Specific consolidation rules can be found in Configuration.
See the documentation plugin development.