|
20 | 20 | [download-image]: https://img.shields.io/npm/dm/egg-loader.svg?style=flat-square
|
21 | 21 | [download-url]: https://npmjs.org/package/egg-loader
|
22 | 22 |
|
23 |
| -egg 文件加载器 |
| 23 | +A core Plugable framework based on koa |
24 | 24 |
|
25 |
| -## 使用说明 |
| 25 | +**Don't use it directly, see [egg]** |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +Directory structure |
| 30 | + |
| 31 | +``` |
| 32 | +├── package.json |
| 33 | +├── app.js (optional) |
| 34 | +├── agent.js (optional) |
| 35 | +├── app |
| 36 | +| ├── router.js |
| 37 | +│ ├── controller |
| 38 | +│ │ └── home.js |
| 39 | +| ├── extend (optional) |
| 40 | +│ | ├── helper.js (optional) |
| 41 | +│ | ├── filter.js (optional) |
| 42 | +│ | ├── request.js (optional) |
| 43 | +│ | ├── response.js (optional) |
| 44 | +│ | ├── context.js (optional) |
| 45 | +│ | ├── application.js (optional) |
| 46 | +│ | └── agent.js (optional) |
| 47 | +│ ├── service (optional) |
| 48 | +│ ├── middleware (optional) |
| 49 | +│ │ └── response_time.js |
| 50 | +│ └── view (optional) |
| 51 | +| ├── layout.html |
| 52 | +│ └── home.html |
| 53 | +├── config |
| 54 | +| ├── config.default.js |
| 55 | +│ ├── config.prod.js |
| 56 | +| ├── config.test.js (optional) |
| 57 | +| ├── config.local.js (optional) |
| 58 | +| ├── config.unittest.js (optional) |
| 59 | +│ └── plugin.js |
| 60 | +``` |
| 61 | + |
| 62 | +Than you can start with code below |
26 | 63 |
|
27 | 64 | ```js
|
28 |
| -const app = koa(); |
29 |
| -const Loader = require('egg-loader'); |
30 |
| -const loader = new Loader({ |
31 |
| - baseDir: '/path/to/app', |
32 |
| - eggPath: '/path/to/framework', |
33 |
| - app: app, |
| 65 | +const Application = require('egg-core').Application; |
| 66 | +const app = new Application({ |
| 67 | + baseDir: '/path/to/app' |
34 | 68 | });
|
35 |
| -loader.loadPlugin(); |
36 |
| -loader.loadConfig(); |
| 69 | +app.ready(() => { |
| 70 | + app.listen(3000); |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +## EggLoader |
| 75 | + |
| 76 | +EggLoader will load file or directory easily, you can also custom your loader with low level API. |
| 77 | + |
| 78 | +### constructor |
| 79 | + |
| 80 | +- {String} baseDir - current directory of application |
| 81 | +- {Object} app - instance of egg application |
| 82 | +- {Object} plugins - merge plugins for test |
| 83 | +- {Logger} logger - logger instance,default is console |
| 84 | + |
| 85 | +### High Level API |
| 86 | + |
| 87 | +#### loadPlugin |
| 88 | + |
| 89 | +Load config/plugin.js |
| 90 | + |
| 91 | +#### loadConfig |
| 92 | + |
| 93 | +Load config/config.js and config/{serverEnv}.js |
| 94 | + |
| 95 | +#### loadController |
| 96 | + |
| 97 | +Load app/controller |
| 98 | + |
| 99 | +#### loadMiddleware |
| 100 | + |
| 101 | +Load app/middleware |
| 102 | + |
| 103 | +#### loadApplicationExtend |
| 104 | + |
| 105 | +Load app/extend/application.js |
| 106 | + |
| 107 | +#### loadContextExtend |
| 108 | + |
| 109 | +Load app/extend/context.js |
| 110 | + |
| 111 | +#### loadRequestExtend |
| 112 | + |
| 113 | +Load app/extend/request.js |
| 114 | + |
| 115 | +#### loadResponseExtend |
| 116 | + |
| 117 | +Load app/extend/response.js |
| 118 | + |
| 119 | +#### loadHelperExtend |
| 120 | + |
| 121 | +Load app/extend/helper.js |
| 122 | + |
| 123 | +#### loadCustomApp |
| 124 | + |
| 125 | +Load app.js |
| 126 | + |
| 127 | +#### loadCustomAgent |
| 128 | + |
| 129 | +Load agent.js |
| 130 | + |
| 131 | +#### loadService |
| 132 | + |
| 133 | +Load app/service |
| 134 | + |
| 135 | +### Low Level API |
| 136 | + |
| 137 | +#### getServerEnv() |
| 138 | + |
| 139 | +Get serverEnv for application, available serverEnv |
| 140 | + |
| 141 | +serverEnv | description |
| 142 | +--- | --- |
| 143 | +default | default environment |
| 144 | +test | system integration testing environment |
| 145 | +prod | production environment |
| 146 | +local | local environment on your own computer |
| 147 | +unittest | unit test environment |
| 148 | + |
| 149 | +You can use this.serverEnv directly after instantiation. |
| 150 | + |
| 151 | +#### getEggPaths() |
| 152 | + |
| 153 | +Get the directory of the frameworks, a new framework born by extending egg, then you can use this function to get all frameworks. |
| 154 | + |
| 155 | +#### getLoadUnits() |
| 156 | + |
| 157 | +A loadUnit is a directory that can be loaded by EggLoader, it has the same structure. |
| 158 | + |
| 159 | +This function will get add loadUnits follow the order: |
| 160 | + |
| 161 | +1. plugin |
| 162 | +2. framework |
| 163 | +3. app |
| 164 | + |
| 165 | +loadUnit has a path and a type(app, framework, plugin). |
| 166 | + |
| 167 | +```js |
| 168 | +{ |
| 169 | + path: 'path/to/application', |
| 170 | + type: 'app', |
| 171 | +} |
37 | 172 | ```
|
38 | 173 |
|
39 |
| -## API |
| 174 | +#### getAppname() |
| 175 | + |
| 176 | +Get appname from package.json |
| 177 | + |
| 178 | +#### loadFile(filepath) |
| 179 | + |
| 180 | +Load single file, will invork when export is function. |
| 181 | + |
| 182 | +#### loadToApp(directory, property, LoaderOptions) |
| 183 | + |
| 184 | +Load the files in directory to app. |
40 | 185 |
|
41 |
| -### options |
| 186 | +Invoke `this.loadToApp('$baseDir/app/controller', 'controller')`, then you can use it by `app.controller`. |
42 | 187 |
|
43 |
| -- baseDir: 应用根目录 |
44 |
| -- eggPath: egg 本身的路径 |
45 |
| -- plugins: 自定义插件配置 |
46 |
| -- app: 任何基于 koa 实例化 |
| 188 | +#### loadToContext(directory, property, LoaderOptions) |
47 | 189 |
|
48 |
| -### methods |
| 190 | +Load the files in directory to context, it will bind the context. |
49 | 191 |
|
50 |
| -基础方式 |
| 192 | +``` |
| 193 | +// define service in app/service/query.js |
| 194 | +module.exports = class Query { |
| 195 | + constructor(ctx) { |
| 196 | + // get the ctx |
| 197 | + } |
| 198 | +
|
| 199 | + get() {} |
| 200 | +}; |
| 201 | +
|
| 202 | +// use the service in app/controller/home.js |
| 203 | +module.exports = function*() { |
| 204 | + this.body = this.service.query.get(); |
| 205 | +}; |
| 206 | +``` |
51 | 207 |
|
52 |
| -- loadFile: 加载单文件, |
53 |
| -- loadDirs: 获取需要加载的所有目录,按照 egg > 插件 > 框架 > 应用的顺序加载。 |
| 208 | +#### loadExtend(name, target) |
| 209 | + |
| 210 | +Loader app/extend/xx.js to target, example |
| 211 | + |
| 212 | +```js |
| 213 | +this.loadExtend('application', app); |
| 214 | +``` |
54 | 215 |
|
55 |
| -业务方法 |
| 216 | +### LoaderOptions |
56 | 217 |
|
57 |
| -- getAppname: 获取应用名 |
58 |
| -- loadServerEnv: 加载环境变量 |
59 |
| -- loadConfig: 加载: config |
60 |
| -- loadPlugin: 加载插件 |
61 |
| -- loadApplication: 加载 extend/application.js 到 app |
62 |
| -- loadRequest: 加载 extend/request.js 到 app.request |
63 |
| -- loadResponse: 加载 extend/response.js 到 app.response |
64 |
| -- loadContext: 加载 extend/context.js 到 app.context |
65 |
| -- loadHelper: 加载 extend/helper.js,到 app.Helper.prototype,需要定义 app.Helper 才会加载 |
66 |
| -- loadService: 加载 app/service 到 app.service |
67 |
| -- loadProxy: 加载 app/proxy 到 app.proxy |
68 |
| -- loadMiddleware: 加载中间件 |
69 |
| -- loadController: 加载 app/controller 到 app.controller |
70 |
| -- loadAgent: 加载 agent.js 进行自定义 |
71 |
| -- loadApp: 加载 app.js 进行自定义 |
| 218 | +- {String|Array} directory - directories to load |
| 219 | +- {Object} target: attach object from loaded files, |
| 220 | +- {String} ignore - ignore the files when load |
| 221 | +- {Function} initializer - custom file exports |
| 222 | +- {Boolean} lowercaseFirst - determine whether the fist letter is lowercase |
| 223 | +- {Boolean} override: determine whether override the property when get the same name |
| 224 | +- {Boolean} call - determine whether invoke when exports is function |
| 225 | +- {Object} inject - an object that be the argument when invoke the function |
72 | 226 |
|
| 227 | +[egg]: https://github.com/eggjs/egg |
0 commit comments