@modern-js/plugin-bff 1.1.1 → 1.1.3-rc.0
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/.runtime-exports/index.d.ts +0 -0
- package/CHANGELOG.md +46 -0
- package/dist/js/modern/cli.js +3 -3
- package/dist/js/modern/constants.js +1 -1
- package/dist/js/modern/loader.js +1 -1
- package/dist/js/modern/server.js +66 -11
- package/dist/js/node/cli.js +7 -8
- package/dist/js/node/constants.js +3 -3
- package/dist/js/node/loader.js +1 -1
- package/dist/js/node/server.js +69 -10
- package/dist/types/cli.d.ts +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/package.json +19 -18
- package/src/cli.ts +3 -3
- package/src/constants.ts +1 -1
- package/src/loader.ts +3 -1
- package/src/server.ts +55 -9
- package/tests/__snapshots__/cli.test.ts.snap +8 -2
|
File without changes
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# @modern-js/plugin-bff
|
|
2
2
|
|
|
3
|
+
## 1.1.3-rc.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [d927bc83]
|
|
8
|
+
- Updated dependencies [d73ff455]
|
|
9
|
+
- Updated dependencies [9c1ab865]
|
|
10
|
+
- Updated dependencies [d73ff455]
|
|
11
|
+
- Updated dependencies [d927bc83]
|
|
12
|
+
- Updated dependencies [d73ff455]
|
|
13
|
+
- Updated dependencies [d73ff455]
|
|
14
|
+
- Updated dependencies [d73ff455]
|
|
15
|
+
- @modern-js/utils@1.1.4-rc.0
|
|
16
|
+
- @modern-js/core@1.1.4-rc.0
|
|
17
|
+
- @modern-js/babel-preset-lib@1.1.3-rc.0
|
|
18
|
+
- @modern-js/bff-utils@1.1.2-rc.0
|
|
19
|
+
- @modern-js/create-request@1.1.2-rc.0
|
|
20
|
+
- @modern-js/server-utils@1.1.3-rc.0
|
|
21
|
+
- @modern-js/babel-compiler@1.1.3-rc.0
|
|
22
|
+
- @modern-js/server-plugin@1.1.2
|
|
23
|
+
- @modern-js/babel-chain@1.1.1
|
|
24
|
+
|
|
25
|
+
## 1.1.2
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- 085a6a58: refactor server plugin
|
|
30
|
+
- 39e8137d: fix: support prefix array & webpack loader should read requestCreator & adjust runtime path to relative
|
|
31
|
+
- 085a6a58: refactor server plugin
|
|
32
|
+
- 085a6a58: refactor server conifg
|
|
33
|
+
- 085a6a58: support server runtime
|
|
34
|
+
- 085a6a58: feat: refactor server plugin
|
|
35
|
+
- Updated dependencies [085a6a58]
|
|
36
|
+
- Updated dependencies [085a6a58]
|
|
37
|
+
- Updated dependencies [085a6a58]
|
|
38
|
+
- Updated dependencies [d280ea33]
|
|
39
|
+
- Updated dependencies [d4fcc73a]
|
|
40
|
+
- Updated dependencies [085a6a58]
|
|
41
|
+
- Updated dependencies [ed1f6b12]
|
|
42
|
+
- Updated dependencies [a5ebbb00]
|
|
43
|
+
- Updated dependencies [085a6a58]
|
|
44
|
+
- @modern-js/core@1.1.3
|
|
45
|
+
- @modern-js/server-plugin@1.1.2
|
|
46
|
+
- @modern-js/server-utils@1.1.2
|
|
47
|
+
- @modern-js/utils@1.1.3
|
|
48
|
+
|
|
3
49
|
## 1.1.1
|
|
4
50
|
|
|
5
51
|
### Patch Changes
|
package/dist/js/modern/cli.js
CHANGED
|
@@ -2,9 +2,8 @@ import path from 'path';
|
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
3
|
import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
|
|
4
4
|
import { compiler } from '@modern-js/babel-compiler';
|
|
5
|
-
import { PLUGIN_SCHEMAS, normalizeOutputPath } from '@modern-js/utils';
|
|
5
|
+
import { PLUGIN_SCHEMAS, normalizeOutputPath, API_DIR } from '@modern-js/utils';
|
|
6
6
|
import { resolveBabelConfig } from '@modern-js/server-utils';
|
|
7
|
-
import { API_DIR } from "./constants";
|
|
8
7
|
const DEFAULT_API_PREFIX = '/api';
|
|
9
8
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
10
9
|
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
@@ -41,7 +40,8 @@ export default createPlugin(() => ({
|
|
|
41
40
|
apiDir: rootDir,
|
|
42
41
|
port,
|
|
43
42
|
fetcher,
|
|
44
|
-
target: _config.name
|
|
43
|
+
target: _config.name,
|
|
44
|
+
requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
},
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const
|
|
1
|
+
export const API_APP_NAME = '_app';
|
|
2
2
|
export const BUILD_FILES = ['**/*.[tj]sx?', '!**/*.test.jsx?', '!**/*.test.tsx?', '!**/*.spec.jsx?', '!**/*.spec.tsx?', '!__tests__/*.tsx?', '!__tests__/*.jsx?'];
|
package/dist/js/modern/loader.js
CHANGED
|
@@ -9,7 +9,7 @@ async function loader(source) {
|
|
|
9
9
|
|
|
10
10
|
const draftOptions = getOptions(this);
|
|
11
11
|
const options = {
|
|
12
|
-
prefix: draftOptions.prefix,
|
|
12
|
+
prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
|
|
13
13
|
apiDir: draftOptions.apiDir,
|
|
14
14
|
target: draftOptions.target,
|
|
15
15
|
port: Number(draftOptions.port),
|
package/dist/js/modern/server.js
CHANGED
|
@@ -1,18 +1,73 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { createPlugin } from '@modern-js/server-plugin';
|
|
3
3
|
import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
import { useAppContext } from '@modern-js/core';
|
|
5
|
+
import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
|
|
6
|
+
import { API_APP_NAME } from "./constants";
|
|
7
|
+
|
|
8
|
+
class Storage {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.middlewares = [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
reset() {
|
|
14
|
+
this.middlewares = [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const createTransformAPI = storage => ({
|
|
20
|
+
addMiddleware(fn) {
|
|
21
|
+
storage.middlewares.push(fn);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export default createPlugin(() => {
|
|
27
|
+
const {
|
|
28
|
+
appDirectory,
|
|
29
|
+
distDirectory
|
|
30
|
+
} = useAppContext();
|
|
31
|
+
const root = isProd() ? distDirectory : appDirectory;
|
|
32
|
+
const apiPath = path.resolve(root || process.cwd(), API_DIR);
|
|
33
|
+
const apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
34
|
+
const storage = new Storage();
|
|
35
|
+
const transformAPI = createTransformAPI(storage);
|
|
36
|
+
const apiMod = requireExistModule(apiAppPath);
|
|
37
|
+
|
|
38
|
+
if (apiMod) {
|
|
39
|
+
apiMod(transformAPI);
|
|
14
40
|
}
|
|
15
41
|
|
|
16
|
-
|
|
42
|
+
return {
|
|
43
|
+
reset() {
|
|
44
|
+
storage.reset();
|
|
45
|
+
const newApiModule = requireExistModule(apiAppPath);
|
|
46
|
+
|
|
47
|
+
if (newApiModule) {
|
|
48
|
+
newApiModule(transformAPI);
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
gather({
|
|
53
|
+
addAPIMiddleware
|
|
54
|
+
}) {
|
|
55
|
+
storage.middlewares.forEach(mid => {
|
|
56
|
+
addAPIMiddleware(mid);
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
prepareApiServer(props, next) {
|
|
61
|
+
const {
|
|
62
|
+
pwd,
|
|
63
|
+
prefix
|
|
64
|
+
} = props;
|
|
65
|
+
const apiDir = path.resolve(pwd, API_DIR);
|
|
66
|
+
injectAPIHandlerInfos(apiDir, prefix);
|
|
67
|
+
return next(props);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
};
|
|
71
|
+
}, {
|
|
17
72
|
name: '@modern-js/plugin-bff'
|
|
18
73
|
});
|
package/dist/js/node/cli.js
CHANGED
|
@@ -17,8 +17,6 @@ var _utils = require("@modern-js/utils");
|
|
|
17
17
|
|
|
18
18
|
var _serverUtils = require("@modern-js/server-utils");
|
|
19
19
|
|
|
20
|
-
var _constants = require("./constants");
|
|
21
|
-
|
|
22
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
21
|
|
|
24
22
|
const DEFAULT_API_PREFIX = '/api';
|
|
@@ -51,7 +49,7 @@ var _default = (0, _core.createPlugin)(() => ({
|
|
|
51
49
|
} = bff || {};
|
|
52
50
|
const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
|
|
53
51
|
|
|
54
|
-
const rootDir = _path.default.resolve(appDirectory,
|
|
52
|
+
const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
|
|
55
53
|
|
|
56
54
|
chain.resolve.alias.set('@api', rootDir);
|
|
57
55
|
const apiRegexp = new RegExp((0, _utils.normalizeOutputPath)(`${appDirectory}${_path.default.sep}api${_path.default.sep}.*(.[tj]s)$`));
|
|
@@ -60,12 +58,13 @@ var _default = (0, _core.createPlugin)(() => ({
|
|
|
60
58
|
apiDir: rootDir,
|
|
61
59
|
port,
|
|
62
60
|
fetcher,
|
|
63
|
-
target: _config.name
|
|
61
|
+
target: _config.name,
|
|
62
|
+
requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
|
|
64
63
|
});
|
|
65
64
|
}
|
|
66
65
|
},
|
|
67
66
|
source: {
|
|
68
|
-
moduleScopes: [`./${
|
|
67
|
+
moduleScopes: [`./${_utils.API_DIR}`]
|
|
69
68
|
}
|
|
70
69
|
};
|
|
71
70
|
},
|
|
@@ -109,11 +108,11 @@ var _default = (0, _core.createPlugin)(() => ({
|
|
|
109
108
|
|
|
110
109
|
const modernConfig = (0, _core.useResolvedConfigContext)();
|
|
111
110
|
|
|
112
|
-
const rootDir = _path.default.resolve(appDirectory,
|
|
111
|
+
const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
|
|
113
112
|
|
|
114
|
-
const distDir = _path.default.resolve(distDirectory,
|
|
113
|
+
const distDir = _path.default.resolve(distDirectory, _utils.API_DIR);
|
|
115
114
|
|
|
116
|
-
const sourceAbsDir = _path.default.resolve(appDirectory,
|
|
115
|
+
const sourceAbsDir = _path.default.resolve(appDirectory, _utils.API_DIR);
|
|
117
116
|
|
|
118
117
|
const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
|
|
119
118
|
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.BUILD_FILES = exports.
|
|
7
|
-
const
|
|
8
|
-
exports.
|
|
6
|
+
exports.BUILD_FILES = exports.API_APP_NAME = void 0;
|
|
7
|
+
const API_APP_NAME = '_app';
|
|
8
|
+
exports.API_APP_NAME = API_APP_NAME;
|
|
9
9
|
const BUILD_FILES = ['**/*.[tj]sx?', '!**/*.test.jsx?', '!**/*.test.tsx?', '!**/*.spec.jsx?', '!**/*.spec.tsx?', '!__tests__/*.tsx?', '!__tests__/*.jsx?'];
|
|
10
10
|
exports.BUILD_FILES = BUILD_FILES;
|
package/dist/js/node/loader.js
CHANGED
|
@@ -17,7 +17,7 @@ async function loader(source) {
|
|
|
17
17
|
|
|
18
18
|
const draftOptions = (0, _loaderUtils.getOptions)(this);
|
|
19
19
|
const options = {
|
|
20
|
-
prefix: draftOptions.prefix,
|
|
20
|
+
prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
|
|
21
21
|
apiDir: draftOptions.apiDir,
|
|
22
22
|
target: draftOptions.target,
|
|
23
23
|
port: Number(draftOptions.port),
|
package/dist/js/node/server.js
CHANGED
|
@@ -11,24 +11,83 @@ var _serverPlugin = require("@modern-js/server-plugin");
|
|
|
11
11
|
|
|
12
12
|
var _bffUtils = require("@modern-js/bff-utils");
|
|
13
13
|
|
|
14
|
+
var _core = require("@modern-js/core");
|
|
15
|
+
|
|
16
|
+
var _utils = require("@modern-js/utils");
|
|
17
|
+
|
|
14
18
|
var _constants = require("./constants");
|
|
15
19
|
|
|
16
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
class Storage {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.middlewares = [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
reset() {
|
|
28
|
+
this.middlewares = [];
|
|
29
|
+
}
|
|
24
30
|
|
|
25
|
-
|
|
31
|
+
}
|
|
26
32
|
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
const createTransformAPI = storage => ({
|
|
34
|
+
addMiddleware(fn) {
|
|
35
|
+
storage.middlewares.push(fn);
|
|
29
36
|
}
|
|
30
37
|
|
|
31
|
-
})
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
var _default = (0, _serverPlugin.createPlugin)(() => {
|
|
41
|
+
const {
|
|
42
|
+
appDirectory,
|
|
43
|
+
distDirectory
|
|
44
|
+
} = (0, _core.useAppContext)();
|
|
45
|
+
const root = (0, _utils.isProd)() ? distDirectory : appDirectory;
|
|
46
|
+
|
|
47
|
+
const apiPath = _path.default.resolve(root || process.cwd(), _utils.API_DIR);
|
|
48
|
+
|
|
49
|
+
const apiAppPath = _path.default.resolve(apiPath, _constants.API_APP_NAME);
|
|
50
|
+
|
|
51
|
+
const storage = new Storage();
|
|
52
|
+
const transformAPI = createTransformAPI(storage);
|
|
53
|
+
const apiMod = (0, _utils.requireExistModule)(apiAppPath);
|
|
54
|
+
|
|
55
|
+
if (apiMod) {
|
|
56
|
+
apiMod(transformAPI);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
reset() {
|
|
61
|
+
storage.reset();
|
|
62
|
+
const newApiModule = (0, _utils.requireExistModule)(apiAppPath);
|
|
63
|
+
|
|
64
|
+
if (newApiModule) {
|
|
65
|
+
newApiModule(transformAPI);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
gather({
|
|
70
|
+
addAPIMiddleware
|
|
71
|
+
}) {
|
|
72
|
+
storage.middlewares.forEach(mid => {
|
|
73
|
+
addAPIMiddleware(mid);
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
prepareApiServer(props, next) {
|
|
78
|
+
const {
|
|
79
|
+
pwd,
|
|
80
|
+
prefix
|
|
81
|
+
} = props;
|
|
82
|
+
|
|
83
|
+
const apiDir = _path.default.resolve(pwd, _utils.API_DIR);
|
|
84
|
+
|
|
85
|
+
(0, _bffUtils.injectAPIHandlerInfos)(apiDir, prefix);
|
|
86
|
+
return next(props);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
};
|
|
90
|
+
}, {
|
|
32
91
|
name: '@modern-js/plugin-bff'
|
|
33
92
|
});
|
|
34
93
|
|
package/dist/types/cli.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const API_APP_NAME = "_app";
|
|
2
2
|
export declare const BUILD_FILES: string[];
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.1.
|
|
14
|
+
"version": "1.1.3-rc.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -45,23 +45,23 @@
|
|
|
45
45
|
"@babel/core": "^7.15.5",
|
|
46
46
|
"@babel/runtime": "^7",
|
|
47
47
|
"@modern-js/babel-chain": "^1.1.1",
|
|
48
|
-
"@modern-js/babel-compiler": "^1.1.
|
|
49
|
-
"@modern-js/babel-preset-lib": "^1.1.
|
|
50
|
-
"@modern-js/create-request": "^1.1.
|
|
51
|
-
"@modern-js/server-utils": "^1.1.
|
|
52
|
-
"@modern-js/utils": "^1.1.
|
|
48
|
+
"@modern-js/babel-compiler": "^1.1.3-rc.0",
|
|
49
|
+
"@modern-js/babel-preset-lib": "^1.1.3-rc.0",
|
|
50
|
+
"@modern-js/create-request": "^1.1.2-rc.0",
|
|
51
|
+
"@modern-js/server-utils": "^1.1.3-rc.0",
|
|
52
|
+
"@modern-js/utils": "^1.1.4-rc.0",
|
|
53
53
|
"fs-extra": "^10.0.0",
|
|
54
54
|
"loader-utils": "^2.0.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@modern-js/bff-utils": "^1.1.
|
|
58
|
-
"@modern-js/core": "^1.1.
|
|
59
|
-
"@modern-js/module-tools": "^1.1.
|
|
60
|
-
"@modern-js/plugin-analyze": "^1.1.
|
|
61
|
-
"@modern-js/plugin-testing": "^1.1.
|
|
62
|
-
"@modern-js/runtime": "^1.1.
|
|
63
|
-
"@modern-js/server-plugin": "^1.1.
|
|
64
|
-
"@modern-js/types": "^1.1.
|
|
57
|
+
"@modern-js/bff-utils": "^1.1.2-rc.0",
|
|
58
|
+
"@modern-js/core": "^1.1.4-rc.0",
|
|
59
|
+
"@modern-js/module-tools": "^1.1.1",
|
|
60
|
+
"@modern-js/plugin-analyze": "^1.1.2-rc.0",
|
|
61
|
+
"@modern-js/plugin-testing": "^1.1.1",
|
|
62
|
+
"@modern-js/runtime": "^1.1.2-rc.0",
|
|
63
|
+
"@modern-js/server-plugin": "^1.1.2",
|
|
64
|
+
"@modern-js/types": "^1.1.3-rc.0",
|
|
65
65
|
"@types/babel__core": "^7.1.15",
|
|
66
66
|
"@types/fs-extra": "^9.0.13",
|
|
67
67
|
"@types/jest": "^26",
|
|
@@ -74,9 +74,9 @@
|
|
|
74
74
|
"webpack-chain": "^6.5.1"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
|
-
"@modern-js/bff-utils": "^1.1.
|
|
78
|
-
"@modern-js/core": "^1.1.
|
|
79
|
-
"@modern-js/server-plugin": "^1.1.
|
|
77
|
+
"@modern-js/bff-utils": "^1.1.2-rc.0",
|
|
78
|
+
"@modern-js/core": "^1.1.4-rc.0",
|
|
79
|
+
"@modern-js/server-plugin": "^1.1.2"
|
|
80
80
|
},
|
|
81
81
|
"modernConfig": {
|
|
82
82
|
"output": {
|
|
@@ -93,5 +93,6 @@
|
|
|
93
93
|
"build": "modern build",
|
|
94
94
|
"dev": "modern build --watch",
|
|
95
95
|
"test": "modern test"
|
|
96
|
-
}
|
|
96
|
+
},
|
|
97
|
+
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
|
97
98
|
}
|
package/src/cli.ts
CHANGED
|
@@ -6,19 +6,18 @@ import {
|
|
|
6
6
|
useResolvedConfigContext,
|
|
7
7
|
} from '@modern-js/core';
|
|
8
8
|
import { compiler } from '@modern-js/babel-compiler';
|
|
9
|
-
import { PLUGIN_SCHEMAS, normalizeOutputPath } from '@modern-js/utils';
|
|
9
|
+
import { PLUGIN_SCHEMAS, normalizeOutputPath, API_DIR } from '@modern-js/utils';
|
|
10
10
|
import { resolveBabelConfig } from '@modern-js/server-utils';
|
|
11
11
|
|
|
12
12
|
import type { Configuration } from 'webpack';
|
|
13
13
|
import type Chain from 'webpack-chain';
|
|
14
14
|
import type { ServerRoute } from '@modern-js/types';
|
|
15
|
-
import { API_DIR } from './constants';
|
|
16
15
|
|
|
17
16
|
declare module '@modern-js/core' {
|
|
18
17
|
interface UserConfig {
|
|
19
18
|
bff: {
|
|
20
19
|
prefix?: string;
|
|
21
|
-
|
|
20
|
+
requestCreator?: string;
|
|
22
21
|
fetcher?: string;
|
|
23
22
|
proxy: Record<string, any>;
|
|
24
23
|
};
|
|
@@ -69,6 +68,7 @@ export default createPlugin(
|
|
|
69
68
|
port,
|
|
70
69
|
fetcher,
|
|
71
70
|
target: _config.name,
|
|
71
|
+
requestCreator: bff?.requestCreator,
|
|
72
72
|
});
|
|
73
73
|
},
|
|
74
74
|
},
|
package/src/constants.ts
CHANGED
package/src/loader.ts
CHANGED
|
@@ -20,7 +20,9 @@ async function loader(this: LoaderContext<APILoaderOptions>, source: string) {
|
|
|
20
20
|
const draftOptions = getOptions(this as any);
|
|
21
21
|
|
|
22
22
|
const options: GenClientOptions = {
|
|
23
|
-
prefix: draftOptions.prefix
|
|
23
|
+
prefix: (Array.isArray(draftOptions.prefix)
|
|
24
|
+
? draftOptions.prefix[0]
|
|
25
|
+
: draftOptions.prefix) as string,
|
|
24
26
|
apiDir: draftOptions.apiDir as string,
|
|
25
27
|
target: draftOptions.target as string,
|
|
26
28
|
port: Number(draftOptions.port),
|
package/src/server.ts
CHANGED
|
@@ -1,18 +1,64 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { createPlugin } from '@modern-js/server-plugin';
|
|
3
3
|
import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
4
|
-
import {
|
|
4
|
+
import { useAppContext } from '@modern-js/core';
|
|
5
|
+
import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
|
|
6
|
+
import { API_APP_NAME } from './constants';
|
|
7
|
+
|
|
8
|
+
type SF = (args: any) => void;
|
|
9
|
+
class Storage {
|
|
10
|
+
public middlewares: SF[] = [];
|
|
11
|
+
|
|
12
|
+
reset() {
|
|
13
|
+
this.middlewares = [];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const createTransformAPI = (storage: Storage) => ({
|
|
18
|
+
addMiddleware(fn: SF) {
|
|
19
|
+
storage.middlewares.push(fn);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
5
22
|
|
|
6
23
|
export default createPlugin(
|
|
7
|
-
() =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
24
|
+
() => {
|
|
25
|
+
const { appDirectory, distDirectory } = useAppContext();
|
|
26
|
+
|
|
27
|
+
const root = isProd() ? distDirectory : appDirectory;
|
|
28
|
+
|
|
29
|
+
const apiPath = path.resolve(root || process.cwd(), API_DIR);
|
|
30
|
+
const apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
31
|
+
|
|
32
|
+
const storage = new Storage();
|
|
33
|
+
const transformAPI = createTransformAPI(storage);
|
|
34
|
+
|
|
35
|
+
const apiMod = requireExistModule(apiAppPath);
|
|
36
|
+
if (apiMod) {
|
|
37
|
+
apiMod(transformAPI);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
reset() {
|
|
42
|
+
storage.reset();
|
|
43
|
+
const newApiModule = requireExistModule(apiAppPath);
|
|
44
|
+
if (newApiModule) {
|
|
45
|
+
newApiModule(transformAPI);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
gather({ addAPIMiddleware }) {
|
|
49
|
+
storage.middlewares.forEach(mid => {
|
|
50
|
+
addAPIMiddleware(mid);
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
prepareApiServer(props, next) {
|
|
54
|
+
const { pwd, prefix } = props;
|
|
55
|
+
const apiDir = path.resolve(pwd, API_DIR);
|
|
11
56
|
|
|
12
|
-
|
|
57
|
+
injectAPIHandlerInfos(apiDir, prefix);
|
|
13
58
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
59
|
+
return next(props);
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
},
|
|
17
63
|
{ name: '@modern-js/plugin-bff' },
|
|
18
64
|
) as any;
|
|
@@ -25,12 +25,18 @@ Array [
|
|
|
25
25
|
"type": "string",
|
|
26
26
|
},
|
|
27
27
|
"prefix": Object {
|
|
28
|
-
"
|
|
28
|
+
"items": Object {
|
|
29
|
+
"type": "string",
|
|
30
|
+
},
|
|
31
|
+
"type": Array [
|
|
32
|
+
"string",
|
|
33
|
+
"array",
|
|
34
|
+
],
|
|
29
35
|
},
|
|
30
36
|
"proxy": Object {
|
|
31
37
|
"type": "object",
|
|
32
38
|
},
|
|
33
|
-
"
|
|
39
|
+
"requestCreator": Object {
|
|
34
40
|
"type": "string",
|
|
35
41
|
},
|
|
36
42
|
},
|