@@ -95,19 +95,30 @@ export class Loader {
9595 // 现在只考虑加载.js文件,后续考虑.ts文件
9696 if ( extention === ".js" ) {
9797 const mod = require ( file ) ;
98+ // 如果mod 为 koa-router实例
9899 // const exports = get(mod, "default");
99100 // 如果disableLoading为true,则不加载这个文件路由
100101 // tslint:disable-next-line:no-empty
101- if ( ! mod [ disableLoading ] ) {
102+ if ( mod instanceof Router ) {
103+ if ( config . getItem ( "debug" ) ) {
104+ consola . info ( `loading a router instance from file: ${ file } ` ) ;
105+ get ( mod , "stack" , [ ] ) . forEach ( ly => {
106+ consola . info ( `loading a route: ${ get ( ly , "path" ) } ` ) ;
107+ } ) ;
108+ }
109+ mainRouter . use ( mod . routes ( ) ) . use ( mod . allowedMethods ( ) ) ;
110+ } else if ( ! mod [ disableLoading ] ) {
102111 Object . keys ( mod ) . forEach ( key => {
103112 if ( mod [ key ] instanceof Router ) {
104- consola . info ( `loading a router instance :${ key } from file: ${ file } ` ) ;
105- get ( mod [ key ] , "stack" , [ ] ) . forEach ( ly => {
106- consola . info ( `loading a route: ${ get ( ly , "path" ) } ` ) ;
107- } ) ;
108- mainRouter
109- . use ( mod [ key ] . routes ( ) )
110- . use ( mod [ key ] . allowedMethods ( ) ) ;
113+ if ( config . getItem ( "debug" ) ) {
114+ consola . info (
115+ `loading a router instance :${ key } from file: ${ file } `
116+ ) ;
117+ get ( mod [ key ] , "stack" , [ ] ) . forEach ( ly => {
118+ consola . info ( `loading a route: ${ get ( ly , "path" ) } ` ) ;
119+ } ) ;
120+ }
121+ mainRouter . use ( mod [ key ] . routes ( ) ) . use ( mod [ key ] . allowedMethods ( ) ) ;
111122 }
112123 } ) ;
113124 }
0 commit comments