Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support customLoader #3484

Merged
merged 12 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/source/en/advanced/loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions docs/source/zh-cn/advanced/loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file added docs/theme/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions lib/loader/app_worker_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class AppWorkerLoader extends EggLoader {
this.loadController();
// app
this.loadRouter(); // Dependent on controllers

this.loadCustomLoader();
}

}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/apps/custom-loader/app/adapter/docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

class DockerAdapter {
constructor(app) {
this.app = app;
}

async inspectDocker() {
return 'docker';
}

}

module.exports = DockerAdapter;
17 changes: 17 additions & 0 deletions test/fixtures/apps/custom-loader/app/controller/user.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions test/fixtures/apps/custom-loader/app/repository/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

class UserRepository {
constructor(ctx) {
this.ctx = ctx;
}

async get() {
return this.ctx.params.name;
}

}

module.exports = UserRepository;
5 changes: 5 additions & 0 deletions test/fixtures/apps/custom-loader/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
app.router.get('/users/:name', app.controller.user.get);
};
15 changes: 15 additions & 0 deletions test/fixtures/apps/custom-loader/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
keys: '123',
customLoader: {
adapter: {
directory: 'app/adapter',
inject: 'app',
},
repository: {
directory: 'app/repository',
inject: 'ctx',
},
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/custom-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "custom-loader"
}
26 changes: 26 additions & 0 deletions test/lib/core/custom_loader.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});
2 changes: 1 addition & 1 deletion test/lib/core/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"'));
Expand Down