@@ -10,7 +10,7 @@ import { extend } from 'extend2';
10
10
import { Request , Response , Application , Context as KoaContext } from '@eggjs/koa' ;
11
11
import { pathMatching , type PathMatchingOptions } from 'egg-path-matching' ;
12
12
import { now , diff } from 'performance-ms' ;
13
- import { FULLPATH , FileLoader , FileLoaderOptions } from './file_loader.js' ;
13
+ import { CaseStyle , FULLPATH , FileLoader , FileLoaderOptions } from './file_loader.js' ;
14
14
import { ContextLoader , ContextLoaderOptions } from './context_loader.js' ;
15
15
import utils , { Fun } from '../utils/index.js' ;
16
16
import sequencify from '../utils/sequencify.js' ;
@@ -19,6 +19,7 @@ import type {
19
19
Context , EggCore , MiddlewareFunc ,
20
20
} from '../egg.js' ;
21
21
import type { BaseContextClass } from '../base_context_class.js' ;
22
+ import type { EggAppConfig , EggAppInfo , EggPluginInfo } from '../types.js' ;
22
23
23
24
const debug = debuglog ( '@eggjs/core/loader/egg_loader' ) ;
24
25
@@ -29,44 +30,6 @@ const originalPrototypes: Record<string, any> = {
29
30
application : Application . prototype ,
30
31
} ;
31
32
32
- export interface EggAppInfo {
33
- /** package.json */
34
- pkg : Record < string , any > ;
35
- /** the application name from package.json */
36
- name : string ;
37
- /** current directory of application */
38
- baseDir : string ;
39
- /** equals to serverEnv */
40
- env : string ;
41
- /** equals to serverScope */
42
- scope : string ;
43
- /** home directory of the OS */
44
- HOME : string ;
45
- /** baseDir when local and unittest, HOME when other environment */
46
- root : string ;
47
- }
48
-
49
- export interface EggPluginInfo {
50
- /** the plugin name, it can be used in `dep` */
51
- name : string ;
52
- /** the package name of plugin */
53
- package ?: string ;
54
- version ?: string ;
55
- /** whether enabled */
56
- enable : boolean ;
57
- implicitEnable ?: boolean ;
58
- /** the directory of the plugin package */
59
- path ?: string ;
60
- /** the dependent plugins, you can use the plugin name */
61
- dependencies : string [ ] ;
62
- /** the optional dependent plugins. */
63
- optionalDependencies : string [ ] ;
64
- dependents ?: string [ ] ;
65
- /** specify the serverEnv that only enable the plugin in it */
66
- env : string [ ] ;
67
- /** the file plugin config in. */
68
- from : string ;
69
- }
70
33
71
34
export interface EggLoaderOptions {
72
35
/** server env */
@@ -845,7 +808,7 @@ export class EggLoader {
845
808
846
809
/** start Config loader */
847
810
configMeta : Record < string , any > ;
848
- config : Record < string , any > ;
811
+ config : EggAppConfig ;
849
812
850
813
/**
851
814
* Load config/config.js
@@ -859,7 +822,10 @@ export class EggLoader {
859
822
this . timing . start ( 'Load Config' ) ;
860
823
this . configMeta = { } ;
861
824
862
- const target : Record < string , any > = { } ;
825
+ const target : EggAppConfig = {
826
+ appMiddleware : [ ] ,
827
+ coreMiddleware : [ ] ,
828
+ } ;
863
829
864
830
// Load Application config first
865
831
const appConfig = await this . #preloadAppConfig( ) ;
@@ -889,8 +855,8 @@ export class EggLoader {
889
855
extend ( true , target , envConfig ) ;
890
856
891
857
// You can manipulate the order of app.config.coreMiddleware and app.config.appMiddleware in app.js
892
- target . coreMiddleware = target . coreMiddlewares = target . coreMiddleware || [ ] ;
893
- target . appMiddleware = target . appMiddlewares = target . middleware || [ ] ;
858
+ target . coreMiddlewares = target . coreMiddleware ;
859
+ target . appMiddlewares = target . middleware ;
894
860
895
861
this . config = target ;
896
862
debug ( '[loadConfig] all config: %o' , this . config ) ;
@@ -1208,7 +1174,7 @@ export class EggLoader {
1208
1174
const servicePaths = this . getLoadUnits ( ) . map ( unit => path . join ( unit . path , 'app/service' ) ) ;
1209
1175
options = {
1210
1176
call : true ,
1211
- caseStyle : ' lower' ,
1177
+ caseStyle : CaseStyle . lower ,
1212
1178
fieldClass : 'serviceClasses' ,
1213
1179
directory : servicePaths ,
1214
1180
...options ,
@@ -1248,7 +1214,7 @@ export class EggLoader {
1248
1214
opt = {
1249
1215
call : false ,
1250
1216
override : true ,
1251
- caseStyle : ' lower' ,
1217
+ caseStyle : CaseStyle . lower ,
1252
1218
directory : middlewarePaths ,
1253
1219
...opt ,
1254
1220
} ;
@@ -1323,7 +1289,7 @@ export class EggLoader {
1323
1289
this . timing . start ( 'Load Controller' ) ;
1324
1290
const controllerBase = path . join ( this . options . baseDir , 'app/controller' ) ;
1325
1291
opt = {
1326
- caseStyle : ' lower' ,
1292
+ caseStyle : CaseStyle . lower ,
1327
1293
directory : controllerBase ,
1328
1294
initializer : ( obj , opt ) => {
1329
1295
// return class if it exports a function
@@ -1403,7 +1369,7 @@ export class EggLoader {
1403
1369
case 'ctx' : {
1404
1370
assert ( ! ( property in this . app . context ) , `customLoader should not override ctx.${ property } ` ) ;
1405
1371
const options = {
1406
- caseStyle : ' lower' ,
1372
+ caseStyle : CaseStyle . lower ,
1407
1373
fieldClass : `${ property } Classes` ,
1408
1374
...loaderConfig ,
1409
1375
directory,
@@ -1414,7 +1380,7 @@ export class EggLoader {
1414
1380
case 'app' : {
1415
1381
assert ( ! ( property in this . app ) , `customLoader should not override app.${ property } ` ) ;
1416
1382
const options = {
1417
- caseStyle : ' lower' ,
1383
+ caseStyle : CaseStyle . lower ,
1418
1384
initializer : ( Clazz : unknown ) => {
1419
1385
return isClass ( Clazz ) ? new Clazz ( this . app ) : Clazz ;
1420
1386
} ,
@@ -1533,10 +1499,11 @@ export class EggLoader {
1533
1499
* @param {Object } options - see {@link FileLoader}
1534
1500
* @since 1.0.0
1535
1501
*/
1536
- async loadToApp ( directory : string | string [ ] , property : string | symbol , options ?: FileLoaderOptions ) {
1502
+ async loadToApp ( directory : string | string [ ] , property : string | symbol ,
1503
+ options ?: Omit < FileLoaderOptions , 'inject' | 'target' > ) {
1537
1504
const target = { } ;
1538
1505
Reflect . set ( this . app , property , target ) ;
1539
- options = {
1506
+ const loadOptions : FileLoaderOptions = {
1540
1507
...options ,
1541
1508
directory : options ?. directory ?? directory ,
1542
1509
target,
@@ -1545,7 +1512,7 @@ export class EggLoader {
1545
1512
1546
1513
const timingKey = `Load "${ String ( property ) } " to Application` ;
1547
1514
this . timing . start ( timingKey ) ;
1548
- await new FileLoader ( options ) . load ( ) ;
1515
+ await new FileLoader ( loadOptions ) . load ( ) ;
1549
1516
this . timing . end ( timingKey ) ;
1550
1517
}
1551
1518
@@ -1556,8 +1523,9 @@ export class EggLoader {
1556
1523
* @param {Object } options - see {@link ContextLoader}
1557
1524
* @since 1.0.0
1558
1525
*/
1559
- async loadToContext ( directory : string | string [ ] , property : string | symbol , options ?: ContextLoaderOptions ) {
1560
- options = {
1526
+ async loadToContext ( directory : string | string [ ] , property : string | symbol ,
1527
+ options ?: Omit < ContextLoaderOptions , 'inject' | 'property' > ) {
1528
+ const loadOptions : ContextLoaderOptions = {
1561
1529
...options ,
1562
1530
directory : options ?. directory || directory ,
1563
1531
property,
@@ -1566,7 +1534,7 @@ export class EggLoader {
1566
1534
1567
1535
const timingKey = `Load "${ String ( property ) } " to Context` ;
1568
1536
this . timing . start ( timingKey ) ;
1569
- await new ContextLoader ( options ) . load ( ) ;
1537
+ await new ContextLoader ( loadOptions ) . load ( ) ;
1570
1538
this . timing . end ( timingKey ) ;
1571
1539
}
1572
1540
0 commit comments