@midwayjs/core 3.11.12-beta.3 → 3.12.0-beta.1
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.
|
@@ -17,7 +17,9 @@ export declare class CommonJSFileDetector extends AbstractFileDetector<{
|
|
|
17
17
|
conflictCheck?: boolean;
|
|
18
18
|
}> {
|
|
19
19
|
private duplicateModuleCheckSet;
|
|
20
|
-
run(container: any): Promise<void>;
|
|
20
|
+
run(container: any): void | Promise<void>;
|
|
21
|
+
loadSync(container: any): void;
|
|
22
|
+
loadAsync(container: any): Promise<void>;
|
|
21
23
|
getType(): 'commonjs' | 'module';
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
@@ -6,14 +6,6 @@ const glob_1 = require("@midwayjs/glob");
|
|
|
6
6
|
const error_1 = require("../error");
|
|
7
7
|
const constants_1 = require("../constants");
|
|
8
8
|
const decorator_1 = require("../decorator");
|
|
9
|
-
async function requireModule(modulePath, type) {
|
|
10
|
-
if (type === 'commonjs') {
|
|
11
|
-
return require(modulePath);
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
return await import(modulePath);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
9
|
class AbstractFileDetector {
|
|
18
10
|
constructor(options) {
|
|
19
11
|
this.options = options;
|
|
@@ -29,6 +21,9 @@ const DEFAULT_IGNORE_PATTERN = [
|
|
|
29
21
|
'**/logs/**',
|
|
30
22
|
'**/run/**',
|
|
31
23
|
'**/public/**',
|
|
24
|
+
'**/app/view/**',
|
|
25
|
+
'**/app/views/**',
|
|
26
|
+
'**/app/extend/**',
|
|
32
27
|
'**/node_modules/**',
|
|
33
28
|
'**/**.test.ts',
|
|
34
29
|
'**/**.test.js',
|
|
@@ -42,7 +37,54 @@ class CommonJSFileDetector extends AbstractFileDetector {
|
|
|
42
37
|
super(...arguments);
|
|
43
38
|
this.duplicateModuleCheckSet = new Map();
|
|
44
39
|
}
|
|
45
|
-
|
|
40
|
+
run(container) {
|
|
41
|
+
if (this.getType() === 'commonjs') {
|
|
42
|
+
return this.loadSync(container);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
return this.loadAsync(container);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
loadSync(container) {
|
|
49
|
+
var _a;
|
|
50
|
+
this.options = this.options || {};
|
|
51
|
+
const loadDirs = [].concat((_a = this.options.loadDir) !== null && _a !== void 0 ? _a : container.get('baseDir'));
|
|
52
|
+
for (const dir of loadDirs) {
|
|
53
|
+
const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
|
|
54
|
+
cwd: dir,
|
|
55
|
+
ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []).concat(this.extraDetectorOptions.ignore || []),
|
|
56
|
+
});
|
|
57
|
+
// 检查重复模块
|
|
58
|
+
const checkDuplicatedHandler = (module, options) => {
|
|
59
|
+
if ((this.options.conflictCheck ||
|
|
60
|
+
this.extraDetectorOptions.conflictCheck) &&
|
|
61
|
+
types_1.Types.isClass(module)) {
|
|
62
|
+
const name = (0, decorator_1.getProviderName)(module);
|
|
63
|
+
if (name) {
|
|
64
|
+
if (this.duplicateModuleCheckSet.has(name)) {
|
|
65
|
+
throw new error_1.MidwayDuplicateClassNameError(name, options.srcPath, this.duplicateModuleCheckSet.get(name));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.duplicateModuleCheckSet.set(name, options.srcPath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
for (const file of fileResults) {
|
|
74
|
+
const exports = require(file);
|
|
75
|
+
// add module to set
|
|
76
|
+
container.bindClass(exports, {
|
|
77
|
+
namespace: this.options.namespace,
|
|
78
|
+
srcPath: file,
|
|
79
|
+
createFrom: 'file',
|
|
80
|
+
bindHook: checkDuplicatedHandler,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// check end
|
|
85
|
+
this.duplicateModuleCheckSet.clear();
|
|
86
|
+
}
|
|
87
|
+
async loadAsync(container) {
|
|
46
88
|
var _a;
|
|
47
89
|
this.options = this.options || {};
|
|
48
90
|
const loadDirs = [].concat((_a = this.options.loadDir) !== null && _a !== void 0 ? _a : container.get('baseDir'));
|
|
@@ -68,7 +110,7 @@ class CommonJSFileDetector extends AbstractFileDetector {
|
|
|
68
110
|
}
|
|
69
111
|
};
|
|
70
112
|
for (const file of fileResults) {
|
|
71
|
-
const exports = await
|
|
113
|
+
const exports = await import(file);
|
|
72
114
|
// add module to set
|
|
73
115
|
container.bindClass(exports, {
|
|
74
116
|
namespace: this.options.namespace,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.12.0-beta.1",
|
|
4
4
|
"description": "midway core",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"koa": "2.14.1",
|
|
26
26
|
"mm": "3.3.0",
|
|
27
27
|
"raw-body": "2.5.2",
|
|
28
|
-
"sinon": "15.
|
|
28
|
+
"sinon": "15.2.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@midwayjs/glob": "^1.0.2",
|