diff --git a/docs/source/en/advanced/loader.md b/docs/source/en/advanced/loader.md index 21d2c6c977..504ce7a3a2 100644 --- a/docs/source/en/advanced/loader.md +++ b/docs/source/en/advanced/loader.md @@ -496,6 +496,40 @@ app/controller | true app/middleware | false app/service | true +## CustomLoader + +You can use `customLoader` instead of `loadToContext` and `loadToApp`. + +When you define a loader with `loadToApp` + +```js +// app.js +module.exports = app => { + const directory = path.join(app.config.baseDir, 'app/adapter'); + app.loader.loadToApp(directory, 'adapter'); +};; +``` + +Instead, you can define `customLoader` + +```js +// config/config.default.js +module.exports = { + customLoader: { + // the property name when load to application, E.X. app.adapter + adapter: { + // relative to app.config.baseDir + directory: 'app/adapter', + // if inject is ctx, it will use loadToContext + inject: 'app', + // whether load the directory of the framework and plugin + loadunit: false, + // you can also use other LoaderOptions + } + }, +}; +`` + [Loader]: https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js [AppWorkerLoader]: https://github.com/eggjs/egg/blob/master/lib/loader/app_worker_loader.js [AgentWorkerLoader]: https://github.com/eggjs/egg/blob/master/lib/loader/agent_worker_loader.js diff --git a/docs/source/zh-cn/advanced/loader.md b/docs/source/zh-cn/advanced/loader.md index 80ab7a80f6..997ddc66a9 100644 --- a/docs/source/zh-cn/advanced/loader.md +++ b/docs/source/zh-cn/advanced/loader.md @@ -501,6 +501,40 @@ app/controller | true app/middleware | false app/service | true +## CustomLoader + +`loadToContext` 和 `loadToApp` 可被 `customLoader` 配置替代。 + +如使用 `loadToApp` 加载的代码如下 + +```js +// app.js +module.exports = app => { + const directory = path.join(app.config.baseDir, 'app/adapter'); + app.loader.loadToApp(directory, 'adapter'); +};; +``` + +换成 `customLoader` 后变为 + +```js +// config/config.default.js +module.exports = { + customLoader: { + // 定义在 app 上的属性名 app.adapter + adapter: { + // 相对于 app.config.baseDir + directory: 'app/adapter', + // 如果是 ctx 则使用 loadToContext + inject: 'app', + // 是否加载框架和插件的目录 + loadunit: false, + // 还可以定义其他 LoaderOptions + } + }, +}; +``` + [Loader]: https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js [AppWorkerLoader]: https://github.com/eggjs/egg/blob/master/lib/loader/app_worker_loader.js [AgentWorkerLoader]: https://github.com/eggjs/egg/blob/master/lib/loader/agent_worker_loader.js diff --git a/docs/theme/.gitkeep b/docs/theme/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/loader/app_worker_loader.js b/lib/loader/app_worker_loader.js index 46bdff4ca8..73ca5c2548 100644 --- a/lib/loader/app_worker_loader.js +++ b/lib/loader/app_worker_loader.js @@ -39,6 +39,8 @@ class AppWorkerLoader extends EggLoader { this.loadController(); // app this.loadRouter(); // Dependent on controllers + + this.loadCustomLoader(); } } diff --git a/package.json b/package.json index 2d0fb5a6e2..1691ce39ce 100644 --- a/package.json +++ b/package.json @@ -21,16 +21,16 @@ "agentkeepalive": "^4.0.2", "cache-content-type": "^1.0.1", "circular-json-for-egg": "^1.0.0", - "cluster-client": "^3.0.0", + "cluster-client": "^3.0.1", "debug": "^4.1.1", "delegates": "^1.0.0", - "egg-cluster": "^1.22.2", + "egg-cluster": "^1.23.0", "egg-cookies": "^2.2.6", - "egg-core": "^4.14.1", + "egg-core": "^4.15.0", "egg-development": "^2.4.2", "egg-i18n": "^2.0.0", "egg-jsonp": "^2.0.0", - "egg-logger": "^2.3.1", + "egg-logger": "^2.3.2", "egg-logrotator": "^3.0.5", "egg-multipart": "^2.4.0", "egg-onerror": "^2.1.0", @@ -69,9 +69,9 @@ "egg-plugin-puml": "^2.4.0", "egg-tracer": "^1.1.0", "egg-view-nunjucks": "^2.2.0", - "eslint": "^5.14.1", + "eslint": "^5.15.1", "eslint-config-egg": "^7.1.0", - "findlinks": "^2.0.0", + "findlinks": "^2.1.0", "formstream": "^1.1.0", "glob": "^7.1.3", "koa-static": "^3.0.0", diff --git a/test/fixtures/apps/custom-loader/app/adapter/docker.js b/test/fixtures/apps/custom-loader/app/adapter/docker.js new file mode 100644 index 0000000000..ec94c4d72f --- /dev/null +++ b/test/fixtures/apps/custom-loader/app/adapter/docker.js @@ -0,0 +1,14 @@ +'use strict'; + +class DockerAdapter { + constructor(app) { + this.app = app; + } + + async inspectDocker() { + return 'docker'; + } + +} + +module.exports = DockerAdapter; diff --git a/test/fixtures/apps/custom-loader/app/controller/user.js b/test/fixtures/apps/custom-loader/app/controller/user.js new file mode 100644 index 0000000000..0f2f62d5be --- /dev/null +++ b/test/fixtures/apps/custom-loader/app/controller/user.js @@ -0,0 +1,17 @@ +'use strict'; + +class UserController { + constructor(ctx) { + this.ctx = ctx; + this.app = ctx.app; + } + + async get() { + this.ctx.body = { + adapter: await this.app.adapter.docker.inspectDocker(), + repository: await this.ctx.repository.user.get(), + }; + } +} + +module.exports = UserController; diff --git a/test/fixtures/apps/custom-loader/app/repository/user.js b/test/fixtures/apps/custom-loader/app/repository/user.js new file mode 100644 index 0000000000..3b432f37ee --- /dev/null +++ b/test/fixtures/apps/custom-loader/app/repository/user.js @@ -0,0 +1,14 @@ +'use strict'; + +class UserRepository { + constructor(ctx) { + this.ctx = ctx; + } + + async get() { + return this.ctx.params.name; + } + +} + +module.exports = UserRepository; diff --git a/test/fixtures/apps/custom-loader/app/router.js b/test/fixtures/apps/custom-loader/app/router.js new file mode 100644 index 0000000000..ca39db339c --- /dev/null +++ b/test/fixtures/apps/custom-loader/app/router.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = app => { + app.router.get('/users/:name', app.controller.user.get); +}; diff --git a/test/fixtures/apps/custom-loader/config/config.default.js b/test/fixtures/apps/custom-loader/config/config.default.js new file mode 100644 index 0000000000..8f46b4be12 --- /dev/null +++ b/test/fixtures/apps/custom-loader/config/config.default.js @@ -0,0 +1,15 @@ +'use strict'; + +module.exports = { + keys: '123', + customLoader: { + adapter: { + directory: 'app/adapter', + inject: 'app', + }, + repository: { + directory: 'app/repository', + inject: 'ctx', + }, + }, +}; diff --git a/test/fixtures/apps/custom-loader/package.json b/test/fixtures/apps/custom-loader/package.json new file mode 100644 index 0000000000..3e35520860 --- /dev/null +++ b/test/fixtures/apps/custom-loader/package.json @@ -0,0 +1,3 @@ +{ + "name": "custom-loader" +} diff --git a/test/lib/core/custom_loader.test.js b/test/lib/core/custom_loader.test.js new file mode 100644 index 0000000000..6177a57bcc --- /dev/null +++ b/test/lib/core/custom_loader.test.js @@ -0,0 +1,26 @@ +'use strict'; + +const mock = require('egg-mock'); +const utils = require('../../utils'); + +describe('test/lib/core/custom_loader.test.js', () => { + afterEach(mock.restore); + + let app; + before(() => { + app = utils.app('apps/custom-loader'); + return app.ready(); + }); + after(() => app.close()); + + it('should', async () => { + await app.httpRequest() + .get('/users/popomore') + .expect({ + adapter: 'docker', + repository: 'popomore', + }) + .expect(200); + }); + +}); diff --git a/test/lib/core/logger.test.js b/test/lib/core/logger.test.js index 8588cab878..d2eccc32a7 100644 --- a/test/lib/core/logger.test.js +++ b/test/lib/core/logger.test.js @@ -139,7 +139,7 @@ describe('test/lib/core/logger.test.js', () => { const logfile = path.join(app.config.logger.dir, 'logger-output-json-web.json.log'); ctx.logger.info('json format'); - await sleep(1000); + await sleep(2000); assert(fs.existsSync(logfile)); assert(fs.readFileSync(logfile, 'utf8').includes('"message":"json format"'));