@eggjs/tegg-loader 4.0.0-beta.7 → 4.0.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,35 @@
1
- import { LoaderCreator, LoaderFactory } from "./LoaderFactory.js";
2
- import { LoaderUtil } from "./LoaderUtil.js";
3
- import { ModuleLoader } from "./impl/ModuleLoader.js";
1
+ import { EggLoadUnitTypeLike, EggProtoImplClass, Loader, ModuleReference } from "@eggjs/tegg-types";
2
+ import { ModuleDescriptor } from "@eggjs/tegg-metadata";
3
+
4
+ //#region src/LoaderFactory.d.ts
5
+ type LoaderCreator = (unitPath: string) => Loader;
6
+ declare class LoaderFactory {
7
+ private static loaderCreatorMap;
8
+ static createLoader(unitPath: string, type: EggLoadUnitTypeLike): Loader;
9
+ static registerLoader(type: EggLoadUnitTypeLike, creator: LoaderCreator): void;
10
+ static loadApp(moduleReferences: readonly ModuleReference[]): Promise<ModuleDescriptor[]>;
11
+ }
12
+ //#endregion
13
+ //#region src/LoaderUtil.d.ts
14
+ interface LoaderUtilConfig {
15
+ extraFilePattern?: string[];
16
+ }
17
+ declare class LoaderUtil {
18
+ static config: LoaderUtilConfig;
19
+ static setConfig(config: LoaderUtilConfig): void;
20
+ static supportExtensions(): string[];
21
+ static get extension(): ".ts" | ".js";
22
+ static filePattern(): string[];
23
+ static loadFile(filePath: string): Promise<EggProtoImplClass[]>;
24
+ }
25
+ //#endregion
26
+ //#region src/impl/ModuleLoader.d.ts
27
+ declare class ModuleLoader implements Loader {
28
+ private readonly moduleDir;
29
+ private protoClazzList;
30
+ constructor(moduleDir: string);
31
+ load(): Promise<EggProtoImplClass[]>;
32
+ static createModuleLoader(path: string): ModuleLoader;
33
+ }
34
+ //#endregion
4
35
  export { LoaderCreator, LoaderFactory, LoaderUtil, ModuleLoader };
package/dist/index.js CHANGED
@@ -1,6 +1,120 @@
1
- import { LoaderFactory } from "./LoaderFactory.js";
2
- import { LoaderUtil } from "./LoaderUtil.js";
3
- import { ModuleLoader } from "./impl/ModuleLoader.js";
4
- import "./impl/index.js";
1
+ import BuiltinModule from "node:module";
2
+ import { EggLoadUnitType } from "@eggjs/tegg-types";
3
+ import "@eggjs/tegg-metadata";
4
+ import { PrototypeUtil } from "@eggjs/core-decorator";
5
+ import { isClass } from "is-type-of";
6
+ import path from "node:path";
7
+ import { debuglog } from "node:util";
8
+ import { globby } from "globby";
5
9
 
10
+ //#region src/LoaderFactory.ts
11
+ var LoaderFactory = class LoaderFactory {
12
+ static loaderCreatorMap = /* @__PURE__ */ new Map();
13
+ static createLoader(unitPath, type) {
14
+ const creator = this.loaderCreatorMap.get(type);
15
+ if (!creator) throw new Error(`not find creator for loader type ${type}`);
16
+ return creator(unitPath);
17
+ }
18
+ static registerLoader(type, creator) {
19
+ this.loaderCreatorMap.set(type, creator);
20
+ }
21
+ static async loadApp(moduleReferences) {
22
+ const result = [];
23
+ const multiInstanceClazzList = [];
24
+ for (const moduleReference of moduleReferences) {
25
+ const loader = LoaderFactory.createLoader(moduleReference.path, moduleReference.loaderType || EggLoadUnitType.MODULE);
26
+ const res = {
27
+ name: moduleReference.name,
28
+ unitPath: moduleReference.path,
29
+ clazzList: [],
30
+ protos: [],
31
+ multiInstanceClazzList,
32
+ optional: moduleReference.optional
33
+ };
34
+ result.push(res);
35
+ const clazzList = await loader.load();
36
+ for (const clazz of clazzList) if (PrototypeUtil.isEggPrototype(clazz)) res.clazzList.push(clazz);
37
+ else if (PrototypeUtil.isEggMultiInstancePrototype(clazz)) res.multiInstanceClazzList.push(clazz);
38
+ }
39
+ return result;
40
+ }
41
+ };
42
+
43
+ //#endregion
44
+ //#region src/LoaderUtil.ts
45
+ const Module = globalThis.module?.constructor?.length > 1 ? globalThis.module.constructor : BuiltinModule;
46
+ var LoaderUtil = class LoaderUtil {
47
+ static config = {};
48
+ static setConfig(config) {
49
+ this.config = config;
50
+ }
51
+ static supportExtensions() {
52
+ const extensions = Object.keys(Module._extensions);
53
+ if (process.env.VITEST === "true" && !extensions.includes(".ts")) extensions.push(".ts");
54
+ return extensions;
55
+ }
56
+ static get extension() {
57
+ return LoaderUtil.supportExtensions().includes(".ts") ? ".ts" : ".js";
58
+ }
59
+ static filePattern() {
60
+ return [
61
+ `**/*.(${LoaderUtil.supportExtensions().map((t) => t.substring(1)).filter((t) => t !== "json").join("|")})`,
62
+ "!**/+(.*)/**",
63
+ "!**/node_modules",
64
+ "!**/*.d.ts",
65
+ "!**/test",
66
+ "!**/coverage",
67
+ ...this.config.extraFilePattern || []
68
+ ];
69
+ }
70
+ static async loadFile(filePath) {
71
+ let exports;
72
+ try {
73
+ exports = await import(filePath);
74
+ } catch (e) {
75
+ console.error("[tegg/loader] loadFile %s error:", filePath);
76
+ console.error(e);
77
+ throw new Error(`[tegg/loader] load ${filePath} failed: ${e.message}`);
78
+ }
79
+ const clazzList = [];
80
+ const exportNames = Object.keys(exports);
81
+ for (const exportName of exportNames) {
82
+ const clazz = exports[exportName];
83
+ if (!(isClass(clazz) && (PrototypeUtil.isEggPrototype(clazz) || PrototypeUtil.isEggMultiInstancePrototype(clazz)))) continue;
84
+ clazzList.push(clazz);
85
+ }
86
+ return clazzList;
87
+ }
88
+ };
89
+
90
+ //#endregion
91
+ //#region src/impl/ModuleLoader.ts
92
+ const debug = debuglog("@eggjs/tegg-loader/impl/ModuleLoader");
93
+ var ModuleLoader = class ModuleLoader {
94
+ moduleDir;
95
+ protoClazzList;
96
+ constructor(moduleDir) {
97
+ this.moduleDir = moduleDir;
98
+ }
99
+ async load() {
100
+ if (this.protoClazzList) return this.protoClazzList;
101
+ const protoClassList = [];
102
+ const filePattern = LoaderUtil.filePattern();
103
+ const files = await globby(filePattern, { cwd: this.moduleDir });
104
+ debug("load files: %o, filePattern: %o, moduleDir: %o", files, filePattern, this.moduleDir);
105
+ for (const file of files) {
106
+ const realPath = path.join(this.moduleDir, file);
107
+ const fileClazzList = await LoaderUtil.loadFile(realPath);
108
+ for (const clazz of fileClazzList) protoClassList.push(clazz);
109
+ }
110
+ this.protoClazzList = Array.from(new Set(protoClassList));
111
+ return this.protoClazzList;
112
+ }
113
+ static createModuleLoader(path$1) {
114
+ return new ModuleLoader(path$1);
115
+ }
116
+ };
117
+ LoaderFactory.registerLoader("MODULE", ModuleLoader.createModuleLoader);
118
+
119
+ //#endregion
6
120
  export { LoaderFactory, LoaderUtil, ModuleLoader };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/tegg-loader",
3
- "version": "4.0.0-beta.7",
3
+ "version": "4.0.0-beta.8",
4
4
  "description": "tegg default loader implement",
5
5
  "keywords": [
6
6
  "egg",
@@ -33,9 +33,9 @@
33
33
  "dependencies": {
34
34
  "globby": "^14.1.0",
35
35
  "is-type-of": "^2.2.0",
36
- "@eggjs/tegg-metadata": "4.0.0-beta.7",
37
- "@eggjs/core-decorator": "4.0.0-beta.7",
38
- "@eggjs/tegg-types": "4.0.0-beta.7"
36
+ "@eggjs/core-decorator": "4.0.0-beta.8",
37
+ "@eggjs/tegg-types": "4.0.0-beta.8",
38
+ "@eggjs/tegg-metadata": "4.0.0-beta.8"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"
@@ -1,13 +0,0 @@
1
- import { EggLoadUnitTypeLike, Loader, ModuleReference } from "@eggjs/tegg-types";
2
- import { ModuleDescriptor } from "@eggjs/tegg-metadata";
3
-
4
- //#region src/LoaderFactory.d.ts
5
- type LoaderCreator = (unitPath: string) => Loader;
6
- declare class LoaderFactory {
7
- private static loaderCreatorMap;
8
- static createLoader(unitPath: string, type: EggLoadUnitTypeLike): Loader;
9
- static registerLoader(type: EggLoadUnitTypeLike, creator: LoaderCreator): void;
10
- static loadApp(moduleReferences: readonly ModuleReference[]): Promise<ModuleDescriptor[]>;
11
- }
12
- //#endregion
13
- export { LoaderCreator, LoaderFactory };
@@ -1,39 +0,0 @@
1
- import { EggLoadUnitType } from "@eggjs/tegg-types";
2
- import "@eggjs/tegg-metadata";
3
- import { PrototypeUtil } from "@eggjs/core-decorator";
4
-
5
- //#region src/LoaderFactory.ts
6
- var LoaderFactory = class LoaderFactory {
7
- static loaderCreatorMap = /* @__PURE__ */ new Map();
8
- static createLoader(unitPath, type) {
9
- const creator = this.loaderCreatorMap.get(type);
10
- if (!creator) throw new Error(`not find creator for loader type ${type}`);
11
- return creator(unitPath);
12
- }
13
- static registerLoader(type, creator) {
14
- this.loaderCreatorMap.set(type, creator);
15
- }
16
- static async loadApp(moduleReferences) {
17
- const result = [];
18
- const multiInstanceClazzList = [];
19
- for (const moduleReference of moduleReferences) {
20
- const loader = LoaderFactory.createLoader(moduleReference.path, moduleReference.loaderType || EggLoadUnitType.MODULE);
21
- const res = {
22
- name: moduleReference.name,
23
- unitPath: moduleReference.path,
24
- clazzList: [],
25
- protos: [],
26
- multiInstanceClazzList,
27
- optional: moduleReference.optional
28
- };
29
- result.push(res);
30
- const clazzList = await loader.load();
31
- for (const clazz of clazzList) if (PrototypeUtil.isEggPrototype(clazz)) res.clazzList.push(clazz);
32
- else if (PrototypeUtil.isEggMultiInstancePrototype(clazz)) res.multiInstanceClazzList.push(clazz);
33
- }
34
- return result;
35
- }
36
- };
37
-
38
- //#endregion
39
- export { LoaderFactory };
@@ -1,16 +0,0 @@
1
- import { EggProtoImplClass } from "@eggjs/tegg-types";
2
-
3
- //#region src/LoaderUtil.d.ts
4
- interface LoaderUtilConfig {
5
- extraFilePattern?: string[];
6
- }
7
- declare class LoaderUtil {
8
- static config: LoaderUtilConfig;
9
- static setConfig(config: LoaderUtilConfig): void;
10
- static supportExtensions(): string[];
11
- static get extension(): ".ts" | ".js";
12
- static filePattern(): string[];
13
- static loadFile(filePath: string): Promise<EggProtoImplClass[]>;
14
- }
15
- //#endregion
16
- export { LoaderUtil };
@@ -1,52 +0,0 @@
1
- import BuiltinModule from "node:module";
2
- import { PrototypeUtil } from "@eggjs/core-decorator";
3
- import { isClass } from "is-type-of";
4
-
5
- //#region src/LoaderUtil.ts
6
- const Module = globalThis.module?.constructor?.length > 1 ? globalThis.module.constructor : BuiltinModule;
7
- var LoaderUtil = class LoaderUtil {
8
- static config = {};
9
- static setConfig(config) {
10
- this.config = config;
11
- }
12
- static supportExtensions() {
13
- const extensions = Object.keys(Module._extensions);
14
- if (process.env.VITEST === "true" && !extensions.includes(".ts")) extensions.push(".ts");
15
- return extensions;
16
- }
17
- static get extension() {
18
- return LoaderUtil.supportExtensions().includes(".ts") ? ".ts" : ".js";
19
- }
20
- static filePattern() {
21
- return [
22
- `**/*.(${LoaderUtil.supportExtensions().map((t) => t.substring(1)).filter((t) => t !== "json").join("|")})`,
23
- "!**/+(.*)/**",
24
- "!**/node_modules",
25
- "!**/*.d.ts",
26
- "!**/test",
27
- "!**/coverage",
28
- ...this.config.extraFilePattern || []
29
- ];
30
- }
31
- static async loadFile(filePath) {
32
- let exports;
33
- try {
34
- exports = await import(filePath);
35
- } catch (e) {
36
- console.error("[tegg/loader] loadFile %s error:", filePath);
37
- console.error(e);
38
- throw new Error(`[tegg/loader] load ${filePath} failed: ${e.message}`);
39
- }
40
- const clazzList = [];
41
- const exportNames = Object.keys(exports);
42
- for (const exportName of exportNames) {
43
- const clazz = exports[exportName];
44
- if (!(isClass(clazz) && (PrototypeUtil.isEggPrototype(clazz) || PrototypeUtil.isEggMultiInstancePrototype(clazz)))) continue;
45
- clazzList.push(clazz);
46
- }
47
- return clazzList;
48
- }
49
- };
50
-
51
- //#endregion
52
- export { LoaderUtil };
@@ -1,12 +0,0 @@
1
- import { EggProtoImplClass, Loader } from "@eggjs/tegg-types";
2
-
3
- //#region src/impl/ModuleLoader.d.ts
4
- declare class ModuleLoader implements Loader {
5
- private readonly moduleDir;
6
- private protoClazzList;
7
- constructor(moduleDir: string);
8
- load(): Promise<EggProtoImplClass[]>;
9
- static createModuleLoader(path: string): ModuleLoader;
10
- }
11
- //#endregion
12
- export { ModuleLoader };
@@ -1,36 +0,0 @@
1
- import { LoaderFactory } from "../LoaderFactory.js";
2
- import { LoaderUtil } from "../LoaderUtil.js";
3
- import path from "node:path";
4
- import { debuglog } from "node:util";
5
- import { globby } from "globby";
6
-
7
- //#region src/impl/ModuleLoader.ts
8
- const debug = debuglog("@eggjs/tegg-loader/impl/ModuleLoader");
9
- var ModuleLoader = class ModuleLoader {
10
- moduleDir;
11
- protoClazzList;
12
- constructor(moduleDir) {
13
- this.moduleDir = moduleDir;
14
- }
15
- async load() {
16
- if (this.protoClazzList) return this.protoClazzList;
17
- const protoClassList = [];
18
- const filePattern = LoaderUtil.filePattern();
19
- const files = await globby(filePattern, { cwd: this.moduleDir });
20
- debug("load files: %o, filePattern: %o, moduleDir: %o", files, filePattern, this.moduleDir);
21
- for (const file of files) {
22
- const realPath = path.join(this.moduleDir, file);
23
- const fileClazzList = await LoaderUtil.loadFile(realPath);
24
- for (const clazz of fileClazzList) protoClassList.push(clazz);
25
- }
26
- this.protoClazzList = Array.from(new Set(protoClassList));
27
- return this.protoClazzList;
28
- }
29
- static createModuleLoader(path$1) {
30
- return new ModuleLoader(path$1);
31
- }
32
- };
33
- LoaderFactory.registerLoader("MODULE", ModuleLoader.createModuleLoader);
34
-
35
- //#endregion
36
- export { ModuleLoader };
@@ -1,3 +0,0 @@
1
- import { ModuleLoader } from "./ModuleLoader.js";
2
-
3
- export { };