custom.js
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'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) {
* doSomething();
* }
* ```
*
* - new:
*
* ```js
* module.exports = class Boot {
* constructor(app) {
* this.app = app;
* }
* configDidLoad() {
* doSomething();
* }
* }
* @since 1.0.0
*/
loadCustomApp() {
this[LOAD_BOOT_HOOK]('app');
this.lifecycle.triggerConfigWillLoad();
},
/**
* Load agent.js, same as {@link EggLoader#loadCustomApp}
*/
loadCustomAgent() {
this[LOAD_BOOT_HOOK]('agent');
this.lifecycle.triggerConfigWillLoad();
},
// 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)) {
bootHook.prototype.fullPath = bootFilePath;
// if is boot class, add to lifecycle
this.lifecycle.addBootHook(bootHook);
} else if (is.function(bootHook)) {
// if is boot function, wrap to class
// for compatibility
this.lifecycle.addFunctionAsBootHook(bootHook);
} else {
this.options.logger.warn('[egg-loader] %s must exports a boot class', bootFilePath);
}
}
// init boots
this.lifecycle.init();
this.timing.end(`Load ${fileName}.js`);
},
};