@@ -10,7 +10,7 @@ import { extend } from 'extend2';
1010import { Request , Response , Application , Context as KoaContext } from '@eggjs/koa' ;
1111import { pathMatching , type PathMatchingOptions } from 'egg-path-matching' ;
1212import { now , diff } from 'performance-ms' ;
13- import { FULLPATH , FileLoader , FileLoaderOptions } from './file_loader.js' ;
13+ import { CaseStyle , FULLPATH , FileLoader , FileLoaderOptions } from './file_loader.js' ;
1414import { ContextLoader , ContextLoaderOptions } from './context_loader.js' ;
1515import utils , { Fun } from '../utils/index.js' ;
1616import sequencify from '../utils/sequencify.js' ;
@@ -19,6 +19,7 @@ import type {
1919 Context , EggCore , MiddlewareFunc ,
2020} from '../egg.js' ;
2121import type { BaseContextClass } from '../base_context_class.js' ;
22+ import type { EggAppConfig , EggAppInfo , EggPluginInfo } from '../types.js' ;
2223
2324const debug = debuglog ( '@eggjs/core/loader/egg_loader' ) ;
2425
@@ -29,44 +30,6 @@ const originalPrototypes: Record<string, any> = {
2930 application : Application . prototype ,
3031} ;
3132
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- }
7033
7134export interface EggLoaderOptions {
7235 /** server env */
@@ -845,7 +808,7 @@ export class EggLoader {
845808
846809 /** start Config loader */
847810 configMeta : Record < string , any > ;
848- config : Record < string , any > ;
811+ config : EggAppConfig ;
849812
850813 /**
851814 * Load config/config.js
@@ -859,7 +822,10 @@ export class EggLoader {
859822 this . timing . start ( 'Load Config' ) ;
860823 this . configMeta = { } ;
861824
862- const target : Record < string , any > = { } ;
825+ const target : EggAppConfig = {
826+ middleware : [ ] ,
827+ coreMiddleware : [ ] ,
828+ } ;
863829
864830 // Load Application config first
865831 const appConfig = await this . #preloadAppConfig( ) ;
@@ -1208,7 +1174,7 @@ export class EggLoader {
12081174 const servicePaths = this . getLoadUnits ( ) . map ( unit => path . join ( unit . path , 'app/service' ) ) ;
12091175 options = {
12101176 call : true ,
1211- caseStyle : ' lower' ,
1177+ caseStyle : CaseStyle . lower ,
12121178 fieldClass : 'serviceClasses' ,
12131179 directory : servicePaths ,
12141180 ...options ,
@@ -1248,7 +1214,7 @@ export class EggLoader {
12481214 opt = {
12491215 call : false ,
12501216 override : true ,
1251- caseStyle : ' lower' ,
1217+ caseStyle : CaseStyle . lower ,
12521218 directory : middlewarePaths ,
12531219 ...opt ,
12541220 } ;
@@ -1323,7 +1289,7 @@ export class EggLoader {
13231289 this . timing . start ( 'Load Controller' ) ;
13241290 const controllerBase = path . join ( this . options . baseDir , 'app/controller' ) ;
13251291 opt = {
1326- caseStyle : ' lower' ,
1292+ caseStyle : CaseStyle . lower ,
13271293 directory : controllerBase ,
13281294 initializer : ( obj , opt ) => {
13291295 // return class if it exports a function
@@ -1403,7 +1369,7 @@ export class EggLoader {
14031369 case 'ctx' : {
14041370 assert ( ! ( property in this . app . context ) , `customLoader should not override ctx.${ property } ` ) ;
14051371 const options = {
1406- caseStyle : ' lower' ,
1372+ caseStyle : CaseStyle . lower ,
14071373 fieldClass : `${ property } Classes` ,
14081374 ...loaderConfig ,
14091375 directory,
@@ -1414,7 +1380,7 @@ export class EggLoader {
14141380 case 'app' : {
14151381 assert ( ! ( property in this . app ) , `customLoader should not override app.${ property } ` ) ;
14161382 const options = {
1417- caseStyle : ' lower' ,
1383+ caseStyle : CaseStyle . lower ,
14181384 initializer : ( Clazz : unknown ) => {
14191385 return isClass ( Clazz ) ? new Clazz ( this . app ) : Clazz ;
14201386 } ,
@@ -1533,10 +1499,11 @@ export class EggLoader {
15331499 * @param {Object } options - see {@link FileLoader}
15341500 * @since 1.0.0
15351501 */
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' > ) {
15371504 const target = { } ;
15381505 Reflect . set ( this . app , property , target ) ;
1539- options = {
1506+ const loadOptions : FileLoaderOptions = {
15401507 ...options ,
15411508 directory : options ?. directory ?? directory ,
15421509 target,
@@ -1545,7 +1512,7 @@ export class EggLoader {
15451512
15461513 const timingKey = `Load "${ String ( property ) } " to Application` ;
15471514 this . timing . start ( timingKey ) ;
1548- await new FileLoader ( options ) . load ( ) ;
1515+ await new FileLoader ( loadOptions ) . load ( ) ;
15491516 this . timing . end ( timingKey ) ;
15501517 }
15511518
@@ -1556,8 +1523,9 @@ export class EggLoader {
15561523 * @param {Object } options - see {@link ContextLoader}
15571524 * @since 1.0.0
15581525 */
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 = {
15611529 ...options ,
15621530 directory : options ?. directory || directory ,
15631531 property,
@@ -1566,7 +1534,7 @@ export class EggLoader {
15661534
15671535 const timingKey = `Load "${ String ( property ) } " to Context` ;
15681536 this . timing . start ( timingKey ) ;
1569- await new ContextLoader ( options ) . load ( ) ;
1537+ await new ContextLoader ( loadOptions ) . load ( ) ;
15701538 this . timing . end ( timingKey ) ;
15711539 }
15721540
0 commit comments