Skip to content

Commit 9defd2d

Browse files
committed
fix: fix loadDir default path
1 parent 41a6fa2 commit 9defd2d

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

packages/midway-core/src/loader.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import * as path from 'path';
22
import { MidwayContainer } from './container';
33
import { MidwayRequestContainer } from './requestContainer';
44

5+
function buildLoadDir(baseDir, dir) {
6+
if (!path.isAbsolute(dir)) {
7+
return path.join(baseDir, dir);
8+
}
9+
return dir;
10+
}
11+
512
export class ContainerLoader {
613

714
baseDir;
@@ -51,6 +58,7 @@ export class ContainerLoader {
5158
}
5259

5360
loadDirectory(loadOpts: {
61+
baseDir?: string;
5462
loadDir?: string[];
5563
disableAutoLoad?: boolean;
5664
pattern?: string;
@@ -62,12 +70,14 @@ export class ContainerLoader {
6270
loadOpts.disableAutoLoad = true;
6371
}
6472

65-
// 如果没有关闭autoLoad 则进行load
73+
// if not disable auto load
6674
if (!loadOpts.disableAutoLoad) {
67-
const defaultLoadDir = this.isTsMode ? [this.baseDir] : ['app', 'lib'];
75+
// use baseDir in parameter first
76+
const baseDir = loadOpts.baseDir || this.baseDir;
77+
const defaultLoadDir = this.isTsMode ? [baseDir] : ['app', 'lib'];
6878
this.applicationContext.load({
6979
loadDir: (loadOpts.loadDir || defaultLoadDir).map(dir => {
70-
return this.buildLoadDir(dir);
80+
return buildLoadDir(baseDir, dir);
7181
}),
7282
pattern: loadOpts.pattern,
7383
ignore: loadOpts.ignore
@@ -81,11 +91,4 @@ export class ContainerLoader {
8191
await this.requestContext.ready();
8292
}
8393

84-
private buildLoadDir(dir) {
85-
if (!path.isAbsolute(dir)) {
86-
return path.join(this.baseDir, dir);
87-
}
88-
return dir;
89-
}
90-
9194
}

packages/midway-web/src/loader/webLoader.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { EggRouter as Router } from '@eggjs/router';
22
import {
33
CONTROLLER_KEY,
44
ControllerOption,
5+
KoaMiddleware,
56
PRIORITY_KEY,
67
RouterOption,
7-
WEB_ROUTER_KEY,
8-
KoaMiddleware
8+
WEB_ROUTER_KEY
99
} from '@midwayjs/decorator';
1010
import * as extend from 'extend2';
1111
import * as fs from 'fs';
@@ -53,6 +53,7 @@ export class MidwayWebLoader extends EggLoader {
5353
this.loadPlugin();
5454
super.loadConfig();
5555
}
56+
5657
// Get the real plugin path
5758
protected getPluginPath(plugin) {
5859
if (plugin.path) {
@@ -149,10 +150,14 @@ export class MidwayWebLoader extends EggLoader {
149150
protected loadApplicationContext() {
150151
// this.app.options.container 测试用例编写方便点
151152
const containerConfig = this.config.container || this.app.options.container || {};
153+
if (!containerConfig.loadDir) {
154+
// 如果没有配置,默认就把扫描目录改到 /src or /dist
155+
containerConfig.baseDir = this.baseDir;
156+
}
152157
// 在 super constructor 中会调用到getAppInfo,之后会被赋值
153158
// 如果是typescript会加上 dist 或者 src 目录
154159
this.containerLoader = new ContainerLoader({
155-
baseDir: this.baseDir,
160+
baseDir: this.appDir,
156161
isTsMode: this.isTsMode
157162
});
158163
this.containerLoader.initialize();
@@ -246,6 +251,7 @@ export class MidwayWebLoader extends EggLoader {
246251
protected async refreshContext(): Promise<void> {
247252
await this.containerLoader.refresh();
248253
}
254+
249255
/**
250256
* wrap controller string to middleware function
251257
* @param controllerMapping like xxxController.index

packages/midway-web/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ export function getMethodNames(obj) {
4343
}
4444

4545
export function isTypeScriptEnvironment() {
46-
return !!require.extensions['.ts'];
46+
return process.env['TS_NODE_FILES'] === 'true';
4747
}

0 commit comments

Comments
 (0)