A core plugin framework based on @eggjs/koa. Support Commonjs and ESM both by tshy.
Don't use it directly, see egg.
Directory structure
├── package.json
├── app.ts (optional)
├── agent.ts (optional)
├── app
| ├── router.ts
│ ├── controller
│ │ └── home.ts
| ├── extend (optional)
│ | ├── helper.ts (optional)
│ | ├── filter.ts (optional)
│ | ├── request.ts (optional)
│ | ├── response.ts (optional)
│ | ├── context.ts (optional)
│ | ├── application.ts (optional)
│ | └── agent.ts (optional)
│ ├── service (optional)
│ ├── middleware (optional)
│ │ └── response_time.ts
│ └── view (optional)
| ├── layout.html
│ └── home.html
├── config
| ├── config.default.ts
│ ├── config.prod.ts
| ├── config.test.ts (optional)
| ├── config.local.ts (optional)
| ├── config.unittest.ts (optional)
│ └── plugin.ts
Then you can start with code below
import { EggCore as Application } from '@eggjs/core';
const app = new Application({
baseDir: '/path/to/app'
});
app.ready(() => {
app.listen(3000);
});
EggLoader can easily load files or directories in your egg project. In addition, you can customize the loader with low level APIs.
- {String} baseDir - current directory of application
- {Object} app - instance of egg application
- {Object} plugins - merge plugins for test
- {Logger} logger - logger instance,default is console
Load config/plugin.ts
Load config/config.ts and config/{serverEnv}.ts
If process.env.EGG_APP_CONFIG
is exists, then it will be parse and override config.
Load app/controller
Load app/middleware
Load app/extend/application.ts
Load app/extend/context.ts
Load app/extend/request.ts
Load app/extend/response.ts
Load app/extend/helper.ts
Load app.ts, if app.ts export boot class, then trigger configDidLoad
Load agent.ts, if agent.ts export boot class, then trigger configDidLoad
Load app/service
Retrieve application environment variable values via serverEnv
.
You can access directly by calling this.serverEnv
after instantiation.
serverEnv | description |
---|---|
default | default environment |
test | system integration testing environment |
prod | production environment |
local | local environment on your own computer |
unittest | unit test environment |
To get directories of the frameworks. A new framework is created by extending egg, then you can use this function to get all frameworks.
A loadUnit is a directory that can be loaded by EggLoader, cause it has the same structure.
This function will get add loadUnits follow the order:
- plugin
- framework
- app
loadUnit has a path and a type. Type must be one of those values: app, framework, plugin.
{
path: 'path/to/application',
type: 'app'
}
To get application name from package.json
Get the infomation of the application
- pkg:
package.json
- name: the application name from
package.json
- baseDir: current directory of application
- env: equals to serverEnv
- HOME: home directory of the OS
- root: baseDir when local and unittest, HOME when other environment
To load a single file. Note: The file must export as a function.
To load files from directory in the application.
Invoke this.loadToApp('$baseDir/app/controller', 'controller')
, then you can use it by app.controller
.
To load files from directory, and it will be bound the context.
// define service in app/service/query.ts
export default class Query {
constructor(ctx: Context) {
super(ctx);
// get the ctx
}
async get() {}
}
// use the service in app/controller/home.ts
export default async (ctx: Context) => {
ctx.body = await ctx.service.query.get();
};
Loader app/extend/xx.ts to target, For example,
await this.loadExtend('application', app);
Param | Type | Description |
---|---|---|
directory | String/Array |
directories to be loaded |
target | Object |
attach the target object from loaded files |
match | String/Array |
match the files when load, default to **/*.js (if process.env.EGG_TYPESCRIPT was true, default to `[ '**/*.(js |
ignore | String/Array |
ignore the files when load |
initializer | Function |
custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an options object that contain path |
caseStyle | String/Function |
set property's case when converting a filepath to property list. |
override | Boolean |
determine whether override the property when get the same name |
call | Boolean |
determine whether invoke when exports is function |
inject | Object |
an object that be the argument when invoke the function |
filter | Function |
a function that filter the exports which can be loaded |
EggCore record boot progress with Timing
, include:
- Process start time
- Script start time(node don't implement an interface like
process.uptime
to record the script start running time, framework can implement a prestart file used with node--require
options to setprocess.scriptTime
) - Application start time
- Load duration
require
duration
Start record a item. If the item exits, end the old one and start a new one.
- {String} name - record item name
- {Number} [start] - record item start time, default is Date.now()
End a item.
- {String} name - end item name
Generate all record items to json
- {String} name - record item name
- {Number} start - item start time
- {Number} end - item end time
- {Number} duration - item duration
- {Number} pid - pid
- {Number} index - item index
Please open an issue here.
Made with contributors-img.