diff --git a/README.md b/README.md index 4b3ee698..c0a227d9 100644 --- a/README.md +++ b/README.md @@ -120,11 +120,11 @@ Load app/extend/helper.js #### loadCustomApp -Load app.js +Load app.js, if app.js export boot class, then trigger configDidLoad #### loadCustomAgent -Load agent.js +Load agent.js, if agent.js export boot class, then trigger configDidLoad #### loadService diff --git a/lib/lifecycle.js b/lib/lifecycle.js index e917d310..1543c57b 100644 --- a/lib/lifecycle.js +++ b/lib/lifecycle.js @@ -80,6 +80,19 @@ class Lifecycle extends EventEmitter { this[BOOT_HOOKS].push(hook); } + addFunctionAsBootHook(hook) { + // app.js is export as a funciton + // call this function in configDidLoad + this[BOOT_HOOKS].push(class Hook { + constructor(app) { + this.app = app; + } + configDidLoad() { + hook(this.app); + } + }); + } + /** * init boots and trigger config did config */ diff --git a/lib/loader/egg_loader.js b/lib/loader/egg_loader.js index 190aaa29..dea654dc 100644 --- a/lib/loader/egg_loader.js +++ b/lib/loader/egg_loader.js @@ -95,10 +95,6 @@ class EggLoader { : this.getServerScope(); } - get bootFileName() { - return this.app.type === 'application' ? 'app' : 'agent'; - } - /** * Get {@link AppInfo#env} * @return {String} env @@ -468,7 +464,6 @@ const loaders = [ require('./mixin/middleware'), require('./mixin/controller'), require('./mixin/router'), - require('./mixin/boot'), ]; for (const loader of loaders) { diff --git a/lib/loader/mixin/boot.js b/lib/loader/mixin/boot.js deleted file mode 100644 index 0fdba0d6..00000000 --- a/lib/loader/mixin/boot.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -const path = require('path'); -const is = require('is-type-of'); - -module.exports = { - /** - * load app.js/agent.js if it's a boot class - */ - loadBootHook() { - const fileName = this.bootFileName; - this.timing.start(`Load boot/${fileName}.js`); - for (const unit of this.getLoadUnits()) { - const bootFilePath = this.resolveModule(path.join(unit.path, fileName)); - if (!bootFilePath) { - continue; - } - const bootHook = this.requireFile(bootFilePath); - // if is boot class, add to lifecycle - if (is.class(bootHook)) { - this.lifecycle.addBootHook(bootHook); - } - } - // init boots - this.lifecycle.init(); - this.timing.end(`Load boot/${fileName}.js`); - }, -}; diff --git a/lib/loader/mixin/custom.js b/lib/loader/mixin/custom.js index df77674d..4999c964 100644 --- a/lib/loader/mixin/custom.js +++ b/lib/loader/mixin/custom.js @@ -1,41 +1,73 @@ 'use strict'; +const is = require('is-type-of'); const path = require('path'); +const LOAD_BOOT_HOOK = Symbol('Loader#loadBootHook'); + module.exports = { /** * load app.js * * @example + * - old: + * * ```js * module.exports = function(app) { - * // can do everything - * do(); - * - * // if you will invoke asynchronous, you can use readyCallback - * const done = app.readyCallback(); - * doAsync(done); + * doSomething(); * } * ``` + * + * - new: + * + * ```js + * module.exports = class Boot { + * constructor(app) { + * this.app = app; + * } + * configDidLoad() { + * doSomething(); + * } + * } * @since 1.0.0 */ loadCustomApp() { - this.timing.start('Load app.js'); + this[LOAD_BOOT_HOOK]('app'); this.lifecycle.triggerConfigDidLoad(); - this.getLoadUnits() - .forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'app')))); - this.timing.end('Load app.js'); }, /** * Load agent.js, same as {@link EggLoader#loadCustomApp} */ loadCustomAgent() { - this.timing.start('Load agent.js'); + this[LOAD_BOOT_HOOK]('agent'); this.lifecycle.triggerConfigDidLoad(); - this.getLoadUnits() - .forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'agent')))); - this.timing.end('Load agent.js'); + }, + + // FIXME: no logger used after egg removed + loadBootHook() { + // do nothing + }, + + [LOAD_BOOT_HOOK](fileName) { + this.timing.start(`Load ${fileName}.js`); + for (const unit of this.getLoadUnits()) { + const bootFilePath = this.resolveModule(path.join(unit.path, fileName)); + if (!bootFilePath) { + continue; + } + const bootHook = this.requireFile(bootFilePath); + if (is.class(bootHook)) { + // if is boot class, add to lifecycle + this.lifecycle.addBootHook(bootHook); + } else { + // if is boot function, wrap to class + this.lifecycle.addFunctionAsBootHook(bootHook); + } + } + // init boots + this.lifecycle.init(); + this.timing.end(`Load ${fileName}.js`); }, }; diff --git a/test/egg.test.js b/test/egg.test.js index 4783f014..547e8a52 100644 --- a/test/egg.test.js +++ b/test/egg.test.js @@ -569,15 +569,17 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', ]); await app.ready(); assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -587,8 +589,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -600,8 +603,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -613,8 +617,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -632,20 +637,21 @@ describe('test/egg.test.js', () => { app.loader.loadPlugin(); app.loader.loadConfig(); app.loader.loadAgentExtend(); - app.loader.loadBootHook(); app.loader.loadCustomAgent(); assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'agent.js in plugin', + 'configDidLoad in app', ]); await app.ready(); assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'agent.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -655,8 +661,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'agent.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -668,8 +675,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'agent.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -681,8 +689,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'agent.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -818,8 +827,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', @@ -832,8 +842,9 @@ describe('test/egg.test.js', () => { assert.deepStrictEqual( app.bootLog, [ - 'configDidLoad', - 'app.js', + 'configDidLoad in plugin', + 'app.js in plugin', + 'configDidLoad in app', 'didLoad', 'beforeStart', 'willReady', diff --git a/test/fixtures/boot/agent.js b/test/fixtures/boot/agent.js index 833c1cf8..28a33d48 100644 --- a/test/fixtures/boot/agent.js +++ b/test/fixtures/boot/agent.js @@ -9,7 +9,7 @@ module.exports = class { } configDidLoad() { - this.app.bootLog.push('configDidLoad'); + this.app.bootLog.push('configDidLoad in app'); } async didLoad() { diff --git a/test/fixtures/boot/app.js b/test/fixtures/boot/app.js index 04ede162..34bdd425 100644 --- a/test/fixtures/boot/app.js +++ b/test/fixtures/boot/app.js @@ -9,7 +9,7 @@ module.exports = class { } configDidLoad() { - this.app.bootLog.push('configDidLoad'); + this.app.bootLog.push('configDidLoad in app'); } async didLoad() { diff --git a/test/fixtures/boot/app/plugin/boot-plugin-dep/agent.js b/test/fixtures/boot/app/plugin/boot-plugin-dep/agent.js new file mode 100644 index 00000000..17b698cc --- /dev/null +++ b/test/fixtures/boot/app/plugin/boot-plugin-dep/agent.js @@ -0,0 +1,10 @@ +'use strict'; + +module.exports = class Boot { + constructor(agent) { + this.agent = agent; + } + configDidLoad() { + this.agent.bootLog.push('configDidLoad in plugin'); + } +}; diff --git a/test/fixtures/boot/app/plugin/boot-plugin-dep/app.js b/test/fixtures/boot/app/plugin/boot-plugin-dep/app.js new file mode 100644 index 00000000..1ce58721 --- /dev/null +++ b/test/fixtures/boot/app/plugin/boot-plugin-dep/app.js @@ -0,0 +1,10 @@ +'use strict'; + +module.exports = class Boot { + constructor(app) { + this.app = app; + } + configDidLoad() { + this.app.bootLog.push('configDidLoad in plugin'); + } +}; diff --git a/test/fixtures/boot/app/plugin/boot-plugin-dep/package.json b/test/fixtures/boot/app/plugin/boot-plugin-dep/package.json new file mode 100644 index 00000000..2632e4b6 --- /dev/null +++ b/test/fixtures/boot/app/plugin/boot-plugin-dep/package.json @@ -0,0 +1,9 @@ +{ + "name": "bootPluginDep", + "eggPlugin": { + "name": "bootPluginDep", + "deps": [ + "bootPuginDep" + ] + } +} diff --git a/test/fixtures/boot/app/plugin/boot-plugin/agent.js b/test/fixtures/boot/app/plugin/boot-plugin/agent.js index 5a25f77b..7f8e0e69 100644 --- a/test/fixtures/boot/app/plugin/boot-plugin/agent.js +++ b/test/fixtures/boot/app/plugin/boot-plugin/agent.js @@ -1,14 +1,14 @@ 'use strict'; const sleep = require('mz-modules/sleep'); -module.exports = app => { - app.bootLog.push('app.js'); - app.beforeStart(async () => { +module.exports = agent => { + agent.bootLog.push('agent.js in plugin'); + agent.beforeStart(async () => { await sleep(5); - app.bootLog.push('beforeStart'); + agent.bootLog.push('beforeStart'); }); - app.ready(()=> { - app.bootLog.push('ready'); + agent.ready(()=> { + agent.bootLog.push('ready'); }); }; diff --git a/test/fixtures/boot/app/plugin/boot-plugin/app.js b/test/fixtures/boot/app/plugin/boot-plugin/app.js index 5a25f77b..dc3e751c 100644 --- a/test/fixtures/boot/app/plugin/boot-plugin/app.js +++ b/test/fixtures/boot/app/plugin/boot-plugin/app.js @@ -2,7 +2,7 @@ const sleep = require('mz-modules/sleep'); module.exports = app => { - app.bootLog.push('app.js'); + app.bootLog.push('app.js in plugin'); app.beforeStart(async () => { await sleep(5); app.bootLog.push('beforeStart'); diff --git a/test/fixtures/boot/app/plugin/boot-plugin/package.json b/test/fixtures/boot/app/plugin/boot-plugin/package.json index 4c5f4b61..f0b53535 100644 --- a/test/fixtures/boot/app/plugin/boot-plugin/package.json +++ b/test/fixtures/boot/app/plugin/boot-plugin/package.json @@ -1,6 +1,9 @@ { "name": "bootPlugin", "eggPlugin": { - "name": "bootPlugin" + "name": "bootPlugin", + "dependencies": [ + "bootPluginDep" + ] } } diff --git a/test/fixtures/boot/config/plugin.js b/test/fixtures/boot/config/plugin.js index 0f6cb614..5bfbd3b9 100644 --- a/test/fixtures/boot/config/plugin.js +++ b/test/fixtures/boot/config/plugin.js @@ -6,3 +6,7 @@ exports.bootPlugin = { enable: true, path: path.join(__dirname, '../app/plugin/boot-plugin'), }; +exports.bootPluginDep = { + enable: true, + path: path.join(__dirname, '../app/plugin/boot-plugin-dep'), +}; diff --git a/test/fixtures/egg-ts/plugins/a/package.json b/test/fixtures/egg-ts/plugins/a/package.json index 1aeaf2c8..1e45a636 100644 --- a/test/fixtures/egg-ts/plugins/a/package.json +++ b/test/fixtures/egg-ts/plugins/a/package.json @@ -1,3 +1,6 @@ { - "name": "a" -} \ No newline at end of file + "name": "a", + "eggPlugin": { + "name": "a" + } +} diff --git a/test/fixtures/egg/index.js b/test/fixtures/egg/index.js index bae255d5..81f6f435 100644 --- a/test/fixtures/egg/index.js +++ b/test/fixtures/egg/index.js @@ -23,7 +23,6 @@ class AppLoader extends EggLoader { this.loadContextExtend(); this.loadRequestExtend(); this.loadResponseExtend(); - this.loadBootHook(); this.loadCustomApp(); this.loadMiddleware(); this.loadService();