Skip to content

Commit 4007ec7

Browse files
committed
modify: 改进自定义 loader 加载 & 编写配置说明
1 parent 37bdeca commit 4007ec7

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

src/Application/Application.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { builtLoaders } from "./builtInLoaders";
55
import { CONF_RESERVEDS, ENV_NAME } from "./constants";
66
import { CONFIG_ENTITY_KEY, ConfigLoader } from "../ConfigLoader";
77
import { Container } from "../IoCLoader";
8-
import { dirname, normalize } from "path";
8+
import { dirname, normalize, resolve } from "path";
99
import { EventEmitter } from "events";
1010
import { existsSync } from "fs";
1111
import { IApplication } from "./IApplication";
@@ -103,14 +103,26 @@ export class Application extends EventEmitter implements IApplication {
103103
return builtLoaders;
104104
}
105105

106+
/**
107+
* 字符串是不是一个路戏
108+
* @param str 字符串
109+
*/
110+
private isPath(str: string) {
111+
if (!str) return false;
112+
return str.startsWith("/") || str.startsWith(".") || /^[a-z]+\:/i.test(str);
113+
}
114+
106115
/**
107116
* 加载一个 loader
108117
* @param name loader 名称
109118
*/
110119
protected importLoader(name: string): ILoaderConstructor {
120+
name = String(name);
111121
const { root } = this.options;
112-
const path = normalize(`${root}/node_modules/${String(name)}`);
113-
const loader = require(path);
122+
const loaderPath = this.isPath(name)
123+
? resolve(root, name)
124+
: normalize(`${root}/node_modules/${name}`);
125+
const loader = require(loaderPath);
114126
return loader.default || loader;
115127
}
116128

src/_example/configs/config.dev.yml

Whitespace-only changes.

src/_example/configs/config.prod.yml

Whitespace-only changes.

src/_example/configs/config.test.yml

Whitespace-only changes.

src/_example/configs/config.yml

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1-
port: 8080
1+
# -----------------------------------------------------------------------------
2+
# 基础配置
3+
# 应用默认将自动选取一个可用端口,可在此指定监听端口
4+
# 此外,也可在 Application 实例化时,通过 options 指定端口
5+
# -----------------------------------------------------------------------------
26

3-
loaders:
7+
# prot: 8080
8+
9+
10+
# -----------------------------------------------------------------------------
11+
# 配置 Loader 第一步:
12+
# 在此声明要在当前应用启用的 loader,格式为 `<CONF_NAME>: <包括名路径>`
13+
# 如果有需要,也可在此禁用内建 loader,格式为 `<CONF_NAME>: false`
14+
# -----------------------------------------------------------------------------
15+
16+
loaders:
17+
# loader_conf_name: loader_package
18+
19+
20+
# -----------------------------------------------------------------------------
21+
# 配置 Loader 第二步:
22+
# 在此配置已经启用的 loader
23+
# 如有需要,内建 loaders 也可直接在此配置或禁用,格式为 `<CONF_NAME>: false`
24+
# -----------------------------------------------------------------------------
25+
26+
# loader_conf_name:
27+
# option_a: ...
28+
# option_b: ...
29+
30+
31+
# -----------------------------------------------------------------------------
32+
# 自定义配置
33+
# 除了「基础配置」和「Loader 配置」,可在此添加自定义配置
34+
# 可在 controller/service/model 中,通过 `@config(<path>)` 注入配置内容
35+
# 另外,配置的整体内容,可通过全局应用实例获取(示例代码:`app.config`)
36+
# -----------------------------------------------------------------------------
37+
38+
# custom_conf_name:
39+
# option_a: ...
40+
# option_b: ...

src/_example/src/app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Application } from "../..";
22

3-
const application = new Application();
4-
application
3+
new Application()
54
.launch()
65
.then(({ port }) => console.info("Running:", `http://localhost:${port}`))
76
.catch(err => console.error(err));

0 commit comments

Comments
 (0)