@modern-js/plugin-bff 2.4.1-beta.0 → 2.5.1-alpha.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/CHANGELOG.md +16 -5
- package/cli.js +1 -1
- package/dist/cjs/cli.js +167 -0
- package/dist/cjs/constants.js +38 -0
- package/dist/cjs/helper.js +43 -0
- package/dist/cjs/index.js +17 -0
- package/dist/cjs/loader.js +67 -0
- package/dist/cjs/server.js +98 -0
- package/dist/esm/cli.js +375 -0
- package/dist/esm/constants.js +11 -0
- package/dist/esm/helper.js +23 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/loader.js +192 -0
- package/dist/esm/server.js +152 -0
- package/dist/esm-node/cli.js +147 -0
- package/dist/esm-node/constants.js +14 -0
- package/dist/esm-node/helper.js +14 -0
- package/dist/esm-node/index.js +1 -0
- package/dist/esm-node/loader.js +46 -0
- package/dist/esm-node/server.js +71 -0
- package/dist/js/modern/cli.js +28 -0
- package/dist/js/node/cli.js +26 -0
- package/dist/js/treeshaking/cli.js +61 -0
- package/dist/types/loader.d.ts +2 -0
- package/package.json +20 -21
- package/server.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
# @modern-js/plugin-bff
|
|
2
2
|
|
|
3
|
-
## 2.
|
|
3
|
+
## 2.5.0
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
+
- 89ca6cc: refactor: merge build-config into scripts/build
|
|
8
|
+
|
|
9
|
+
refactor: 把 build-config 合并进 scripts/build
|
|
10
|
+
|
|
11
|
+
- 6fca567: feat: support bff handle complete server, include page render
|
|
12
|
+
feat: 支持 bff 处理整个服务,包括页面渲染
|
|
13
|
+
- 30614fa: chore: modify package.json entry fields and build config
|
|
14
|
+
chore: 更改 package.json entry 字段以及构建配置
|
|
15
|
+
- Updated dependencies [89ca6cc]
|
|
16
|
+
- Updated dependencies [30614fa]
|
|
17
|
+
- Updated dependencies [1b0ce87]
|
|
7
18
|
- Updated dependencies [11c053b]
|
|
8
|
-
- @modern-js/
|
|
9
|
-
- @modern-js/
|
|
10
|
-
- @modern-js/
|
|
11
|
-
- @modern-js/
|
|
19
|
+
- @modern-js/bff-core@2.5.0
|
|
20
|
+
- @modern-js/create-request@2.5.0
|
|
21
|
+
- @modern-js/server-utils@2.5.0
|
|
22
|
+
- @modern-js/utils@2.5.0
|
|
12
23
|
|
|
13
24
|
## 2.4.0
|
|
14
25
|
|
package/cli.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('./dist/
|
|
1
|
+
module.exports = require('./dist/cjs/cli');
|
package/dist/cjs/cli.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var cli_exports = {};
|
|
25
|
+
__export(cli_exports, {
|
|
26
|
+
default: () => cli_default
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(cli_exports);
|
|
29
|
+
var import_path = __toESM(require("path"));
|
|
30
|
+
var import_utils = require("@modern-js/utils");
|
|
31
|
+
var import_server_utils = require("@modern-js/server-utils");
|
|
32
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
33
|
+
var import_helper = require("./helper");
|
|
34
|
+
const DEFAULT_API_PREFIX = "/api";
|
|
35
|
+
const TS_CONFIG_FILENAME = "tsconfig.json";
|
|
36
|
+
var cli_default = () => ({
|
|
37
|
+
name: "@modern-js/plugin-bff",
|
|
38
|
+
setup: (api) => {
|
|
39
|
+
let unRegisterResolveRuntimePath = null;
|
|
40
|
+
return {
|
|
41
|
+
validateSchema() {
|
|
42
|
+
return import_utils.PLUGIN_SCHEMAS["@modern-js/plugin-bff"];
|
|
43
|
+
},
|
|
44
|
+
config() {
|
|
45
|
+
const configContext = api.useConfigContext();
|
|
46
|
+
console.log("22222222222222222", configContext.bff);
|
|
47
|
+
return {
|
|
48
|
+
tools: {
|
|
49
|
+
webpackChain: (chain, { name, CHAIN_ID }) => {
|
|
50
|
+
const { appDirectory, port } = api.useAppContext();
|
|
51
|
+
const modernConfig = api.useResolvedConfigContext();
|
|
52
|
+
const { bff } = modernConfig || {};
|
|
53
|
+
const prefix = (bff == null ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
|
|
54
|
+
const { httpMethodDecider } = bff;
|
|
55
|
+
const rootDir = import_path.default.resolve(appDirectory, import_utils.API_DIR);
|
|
56
|
+
chain.resolve.alias.set("@api", rootDir);
|
|
57
|
+
const apiRouter = new import_bff_core.ApiRouter({
|
|
58
|
+
apiDir: rootDir,
|
|
59
|
+
prefix,
|
|
60
|
+
httpMethodDecider
|
|
61
|
+
});
|
|
62
|
+
const lambdaDir = apiRouter.getLambdaDir();
|
|
63
|
+
const existLambda = apiRouter.isExistLambda();
|
|
64
|
+
const apiRegexp = new RegExp(
|
|
65
|
+
(0, import_utils.normalizeOutputPath)(`${rootDir}${import_path.default.sep}.*(.[tj]s)$`)
|
|
66
|
+
);
|
|
67
|
+
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(apiRegexp);
|
|
68
|
+
chain.module.rule(CHAIN_ID.RULE.JS_BFF_API).test(apiRegexp).use("custom-loader").loader(require.resolve("./loader").replace(/\\/g, "/")).options({
|
|
69
|
+
prefix,
|
|
70
|
+
apiDir: rootDir,
|
|
71
|
+
lambdaDir,
|
|
72
|
+
existLambda,
|
|
73
|
+
port,
|
|
74
|
+
target: name,
|
|
75
|
+
httpMethodDecider
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
source: {
|
|
80
|
+
moduleScopes: [`./${import_utils.API_DIR}`, /create-request/]
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
modifyServerRoutes({ routes }) {
|
|
85
|
+
const modernConfig = api.useResolvedConfigContext();
|
|
86
|
+
const { bff } = modernConfig || {};
|
|
87
|
+
const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
|
|
88
|
+
const prefixList = [];
|
|
89
|
+
if (Array.isArray(prefix)) {
|
|
90
|
+
prefixList.push(...prefix);
|
|
91
|
+
} else {
|
|
92
|
+
prefixList.push(prefix);
|
|
93
|
+
}
|
|
94
|
+
const apiServerRoutes = prefixList.map((pre) => ({
|
|
95
|
+
urlPath: pre,
|
|
96
|
+
isApi: true,
|
|
97
|
+
entryPath: "",
|
|
98
|
+
isSPA: false,
|
|
99
|
+
isSSR: false
|
|
100
|
+
}));
|
|
101
|
+
if (bff == null ? void 0 : bff.enableHandleWeb) {
|
|
102
|
+
return {
|
|
103
|
+
routes: routes.map((route) => {
|
|
104
|
+
return {
|
|
105
|
+
...route,
|
|
106
|
+
isApi: true
|
|
107
|
+
};
|
|
108
|
+
}).concat(apiServerRoutes)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return { routes: routes.concat(apiServerRoutes) };
|
|
112
|
+
},
|
|
113
|
+
collectServerPlugins({ plugins }) {
|
|
114
|
+
plugins.push({
|
|
115
|
+
"@modern-js/plugin-bff": "@modern-js/plugin-bff/server"
|
|
116
|
+
});
|
|
117
|
+
return { plugins };
|
|
118
|
+
},
|
|
119
|
+
async beforeBuild() {
|
|
120
|
+
if ((0, import_utils.isProd)()) {
|
|
121
|
+
const { internalDirectory } = api.useAppContext();
|
|
122
|
+
unRegisterResolveRuntimePath = (0, import_helper.registerModernRuntimePath)(internalDirectory);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async afterBuild() {
|
|
126
|
+
if (unRegisterResolveRuntimePath) {
|
|
127
|
+
unRegisterResolveRuntimePath();
|
|
128
|
+
}
|
|
129
|
+
const { appDirectory, distDirectory } = api.useAppContext();
|
|
130
|
+
const modernConfig = api.useResolvedConfigContext();
|
|
131
|
+
const distDir = import_path.default.resolve(distDirectory);
|
|
132
|
+
const apiDir = import_path.default.resolve(appDirectory, import_utils.API_DIR);
|
|
133
|
+
const sharedDir = import_path.default.resolve(appDirectory, import_utils.SHARED_DIR);
|
|
134
|
+
const tsconfigPath = import_path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
|
|
135
|
+
const sourceDirs = [];
|
|
136
|
+
if (import_utils.fs.existsSync(apiDir)) {
|
|
137
|
+
sourceDirs.push(apiDir);
|
|
138
|
+
}
|
|
139
|
+
if (import_utils.fs.existsSync(sharedDir)) {
|
|
140
|
+
sourceDirs.push(sharedDir);
|
|
141
|
+
}
|
|
142
|
+
const { server } = modernConfig;
|
|
143
|
+
const { alias, define, globalVars } = modernConfig.source;
|
|
144
|
+
const { babel } = modernConfig.tools;
|
|
145
|
+
if (sourceDirs.length > 0) {
|
|
146
|
+
await (0, import_server_utils.compile)(
|
|
147
|
+
appDirectory,
|
|
148
|
+
{
|
|
149
|
+
server,
|
|
150
|
+
alias,
|
|
151
|
+
define,
|
|
152
|
+
globalVars,
|
|
153
|
+
babelConfig: babel
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
sourceDirs,
|
|
157
|
+
distDir,
|
|
158
|
+
tsconfigPath
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
167
|
+
0 && (module.exports = {});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var constants_exports = {};
|
|
19
|
+
__export(constants_exports, {
|
|
20
|
+
API_APP_NAME: () => API_APP_NAME,
|
|
21
|
+
BUILD_FILES: () => BUILD_FILES
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(constants_exports);
|
|
24
|
+
const API_APP_NAME = "_app";
|
|
25
|
+
const BUILD_FILES = [
|
|
26
|
+
"**/*.[tj]sx?",
|
|
27
|
+
"!**/*.test.jsx?",
|
|
28
|
+
"!**/*.test.tsx?",
|
|
29
|
+
"!**/*.spec.jsx?",
|
|
30
|
+
"!**/*.spec.tsx?",
|
|
31
|
+
"!__tests__/*.tsx?",
|
|
32
|
+
"!__tests__/*.jsx?"
|
|
33
|
+
];
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
API_APP_NAME,
|
|
37
|
+
BUILD_FILES
|
|
38
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var helper_exports = {};
|
|
25
|
+
__export(helper_exports, {
|
|
26
|
+
registerModernRuntimePath: () => registerModernRuntimePath
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(helper_exports);
|
|
29
|
+
var path = __toESM(require("path"));
|
|
30
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
31
|
+
const serverRuntimeAlias = "@modern-js/runtime/server";
|
|
32
|
+
const serverRuntimePath = ".runtime-exports/server";
|
|
33
|
+
const registerModernRuntimePath = (internalDirectory) => {
|
|
34
|
+
const paths = {
|
|
35
|
+
[serverRuntimeAlias]: path.join(internalDirectory, serverRuntimePath)
|
|
36
|
+
};
|
|
37
|
+
const unRegister = (0, import_bff_core.registerPaths)(paths);
|
|
38
|
+
return unRegister;
|
|
39
|
+
};
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
registerModernRuntimePath
|
|
43
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var src_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(src_exports);
|
|
17
|
+
__reExport(src_exports, require("./constants"), module.exports);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var loader_exports = {};
|
|
19
|
+
__export(loader_exports, {
|
|
20
|
+
default: () => loader_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(loader_exports);
|
|
23
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
24
|
+
var import_utils = require("@modern-js/utils");
|
|
25
|
+
async function loader(source) {
|
|
26
|
+
this.cacheable();
|
|
27
|
+
const callback = this.async();
|
|
28
|
+
const draftOptions = this.getOptions();
|
|
29
|
+
const { resourcePath } = this;
|
|
30
|
+
const warning = `The file ${resourcePath} is not allowd to be imported in src directory, only API definition files are allowed.`;
|
|
31
|
+
if (!draftOptions.existLambda) {
|
|
32
|
+
import_utils.logger.warn(warning);
|
|
33
|
+
callback(null, `throw new Error('${warning}')`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const options = {
|
|
37
|
+
prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
|
|
38
|
+
apiDir: draftOptions.apiDir,
|
|
39
|
+
target: draftOptions.target,
|
|
40
|
+
port: Number(draftOptions.port),
|
|
41
|
+
source,
|
|
42
|
+
resourcePath,
|
|
43
|
+
httpMethodDecider: draftOptions.httpMethodDecider
|
|
44
|
+
};
|
|
45
|
+
const { lambdaDir } = draftOptions;
|
|
46
|
+
if (!resourcePath.startsWith(lambdaDir)) {
|
|
47
|
+
import_utils.logger.warn(warning);
|
|
48
|
+
callback(null, `throw new Error('${warning}')`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (draftOptions.fetcher) {
|
|
52
|
+
options.fetcher = draftOptions.fetcher;
|
|
53
|
+
}
|
|
54
|
+
if (draftOptions.requestCreator) {
|
|
55
|
+
options.requestCreator = draftOptions.requestCreator;
|
|
56
|
+
}
|
|
57
|
+
options.requireResolve = require.resolve;
|
|
58
|
+
const result = await (0, import_bff_core.generateClient)(options);
|
|
59
|
+
if (result.isOk) {
|
|
60
|
+
callback(void 0, result.value);
|
|
61
|
+
} else {
|
|
62
|
+
callback(void 0, `throw new Error('${result.value}')`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
var loader_default = loader;
|
|
66
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
67
|
+
0 && (module.exports = {});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var server_exports = {};
|
|
25
|
+
__export(server_exports, {
|
|
26
|
+
default: () => server_default
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(server_exports);
|
|
29
|
+
var import_path = __toESM(require("path"));
|
|
30
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
31
|
+
var import_utils = require("@modern-js/utils");
|
|
32
|
+
var import_constants = require("./constants");
|
|
33
|
+
class Storage {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.middlewares = [];
|
|
36
|
+
}
|
|
37
|
+
reset() {
|
|
38
|
+
this.middlewares = [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const createTransformAPI = (storage) => ({
|
|
42
|
+
addMiddleware(fn) {
|
|
43
|
+
storage.middlewares.push(fn);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
var server_default = () => ({
|
|
47
|
+
name: "@modern-js/plugin-bff",
|
|
48
|
+
setup: (api) => {
|
|
49
|
+
const storage = new Storage();
|
|
50
|
+
const transformAPI = createTransformAPI(storage);
|
|
51
|
+
let apiAppPath = "";
|
|
52
|
+
return {
|
|
53
|
+
prepare() {
|
|
54
|
+
const { appDirectory, distDirectory } = api.useAppContext();
|
|
55
|
+
const root = (0, import_utils.isProd)() ? distDirectory : appDirectory;
|
|
56
|
+
const apiPath = import_path.default.resolve(root || process.cwd(), import_utils.API_DIR);
|
|
57
|
+
apiAppPath = import_path.default.resolve(apiPath, import_constants.API_APP_NAME);
|
|
58
|
+
const apiMod = (0, import_utils.requireExistModule)(apiAppPath);
|
|
59
|
+
if (apiMod && typeof apiMod === "function") {
|
|
60
|
+
apiMod(transformAPI);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
reset() {
|
|
64
|
+
storage.reset();
|
|
65
|
+
const newApiModule = (0, import_utils.requireExistModule)(apiAppPath);
|
|
66
|
+
if (newApiModule && typeof newApiModule === "function") {
|
|
67
|
+
newApiModule(transformAPI);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
gather({ addAPIMiddleware }) {
|
|
71
|
+
storage.middlewares.forEach((mid) => {
|
|
72
|
+
addAPIMiddleware(mid);
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
prepareApiServer(props, next) {
|
|
76
|
+
const { pwd, prefix, httpMethodDecider } = props;
|
|
77
|
+
const apiDir = import_path.default.resolve(pwd, import_utils.API_DIR);
|
|
78
|
+
const appContext = api.useAppContext();
|
|
79
|
+
const apiRouter = new import_bff_core.ApiRouter({
|
|
80
|
+
apiDir,
|
|
81
|
+
prefix,
|
|
82
|
+
httpMethodDecider
|
|
83
|
+
});
|
|
84
|
+
const apiMode = apiRouter.getApiMode();
|
|
85
|
+
const apiHandlerInfos = apiRouter.getApiHandlers();
|
|
86
|
+
api.setAppContext({
|
|
87
|
+
...appContext,
|
|
88
|
+
apiRouter,
|
|
89
|
+
apiHandlerInfos,
|
|
90
|
+
apiMode
|
|
91
|
+
});
|
|
92
|
+
return next(props);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
98
|
+
0 && (module.exports = {});
|