@modern-js/plugin-bff 3.0.0-alpha.1 → 3.0.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/dist/cjs/cli.js +3 -11
- package/dist/cjs/runtime/hono/adapter.js +1 -0
- package/dist/cjs/runtime/hono/index.js +4 -20
- package/dist/cjs/server.js +1 -16
- package/dist/cjs/utils/createHonoRoutes.js +2 -2
- package/dist/esm/cli.mjs +6 -14
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/runtime/hono/adapter.mjs +3 -2
- package/dist/esm/runtime/hono/index.mjs +1 -3
- package/dist/esm/server.mjs +3 -18
- package/dist/esm/utils/createHonoRoutes.mjs +1 -1
- package/dist/esm/utils/pluginGenerator.mjs +1 -1
- package/dist/esm-node/cli.mjs +7 -11
- package/dist/esm-node/constants.mjs +1 -0
- package/dist/esm-node/index.mjs +1 -0
- package/dist/esm-node/loader.mjs +2 -0
- package/dist/esm-node/runtime/create-request/index.mjs +1 -0
- package/dist/esm-node/runtime/hono/adapter.mjs +3 -1
- package/dist/esm-node/runtime/hono/index.mjs +1 -2
- package/dist/esm-node/runtime/hono/operators.mjs +1 -0
- package/dist/esm-node/server.mjs +3 -17
- package/dist/esm-node/utils/clientGenerator.mjs +1 -0
- package/dist/esm-node/utils/createHonoRoutes.mjs +2 -1
- package/dist/esm-node/utils/crossProjectApiPlugin.mjs +1 -0
- package/dist/esm-node/utils/pluginGenerator.mjs +5 -1
- package/dist/esm-node/utils/runtimeGenerator.mjs +2 -0
- package/dist/types/runtime/hono/index.d.ts +0 -1
- package/package.json +21 -44
- package/rstest.config.ts +10 -0
package/dist/cjs/cli.js
CHANGED
|
@@ -49,7 +49,6 @@ const runtimeGenerator_js_namespaceObject = require("./utils/runtimeGenerator.js
|
|
|
49
49
|
var runtimeGenerator_js_default = /*#__PURE__*/ __webpack_require__.n(runtimeGenerator_js_namespaceObject);
|
|
50
50
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
51
51
|
const RUNTIME_CREATE_REQUEST = '@modern-js/plugin-bff/client';
|
|
52
|
-
const RUNTIME_HONO = '@modern-js/plugin-bff/server';
|
|
53
52
|
const bffPlugin = ()=>({
|
|
54
53
|
name: '@modern-js/plugin-bff',
|
|
55
54
|
setup: (api)=>{
|
|
@@ -66,13 +65,11 @@ const bffPlugin = ()=>({
|
|
|
66
65
|
const { server } = modernConfig;
|
|
67
66
|
const { alias } = modernConfig.source;
|
|
68
67
|
const { alias: resolveAlias } = modernConfig.resolve;
|
|
69
|
-
const { babel } = modernConfig.tools;
|
|
70
68
|
if (sourceDirs.length > 0) {
|
|
71
69
|
const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
|
|
72
70
|
await (0, server_utils_namespaceObject.compile)(appDirectory, {
|
|
73
71
|
server,
|
|
74
|
-
alias: combinedAlias
|
|
75
|
-
babelConfig: babel
|
|
72
|
+
alias: combinedAlias
|
|
76
73
|
}, {
|
|
77
74
|
sourceDirs,
|
|
78
75
|
distDir,
|
|
@@ -148,9 +145,6 @@ const bffPlugin = ()=>({
|
|
|
148
145
|
return compress;
|
|
149
146
|
};
|
|
150
147
|
api.config(async ()=>{
|
|
151
|
-
const honoRuntimePath = isHono() ? {
|
|
152
|
-
[RUNTIME_HONO]: RUNTIME_HONO
|
|
153
|
-
} : void 0;
|
|
154
148
|
const devServer = api.getConfig()?.tools?.devServer;
|
|
155
149
|
const prefix = api.getConfig()?.bff?.prefix || utils_namespaceObject.DEFAULT_API_PREFIX;
|
|
156
150
|
const compress = createCompressConfig(devServer, prefix);
|
|
@@ -177,7 +171,8 @@ const bffPlugin = ()=>({
|
|
|
177
171
|
const existLambda = apiRouter.isExistLambda();
|
|
178
172
|
const apiRegexp = new RegExp((0, utils_namespaceObject.normalizeOutputPath)(`${apiDirectory}${external_path_default().sep}.*(.[tj]s)$`));
|
|
179
173
|
const name = isServer ? 'server' : 'client';
|
|
180
|
-
const
|
|
174
|
+
const sourceExt = 'js';
|
|
175
|
+
const loaderPath = external_path_default().join(__dirname, `loader.${sourceExt}`);
|
|
181
176
|
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(apiRegexp);
|
|
182
177
|
chain.module.rule('js-bff-api').test(apiRegexp).use('custom-loader').loader(loaderPath.replace(/\\/g, '/')).options({
|
|
183
178
|
prefix,
|
|
@@ -191,9 +186,6 @@ const bffPlugin = ()=>({
|
|
|
191
186
|
httpMethodDecider
|
|
192
187
|
});
|
|
193
188
|
}
|
|
194
|
-
},
|
|
195
|
-
output: {
|
|
196
|
-
externals: honoRuntimePath
|
|
197
189
|
}
|
|
198
190
|
};
|
|
199
191
|
});
|
|
@@ -71,6 +71,7 @@ class HonoAdapter {
|
|
|
71
71
|
this.registerApiRoutes = async ()=>{
|
|
72
72
|
if (!this.isHono) return;
|
|
73
73
|
this.apiServer = new server_core_namespaceObject.Hono();
|
|
74
|
+
this.apiServer.use('*', server_core_namespaceObject.run);
|
|
74
75
|
this.apiMiddleware.forEach(({ path = '*', method = 'all', handler })=>{
|
|
75
76
|
const handlers = this.wrapInArray(handler);
|
|
76
77
|
if (0 === handlers.length) return;
|
|
@@ -5,9 +5,6 @@ var __webpack_modules__ = {
|
|
|
5
5
|
},
|
|
6
6
|
"@modern-js/bff-core" (module) {
|
|
7
7
|
module.exports = require("@modern-js/bff-core");
|
|
8
|
-
},
|
|
9
|
-
"@modern-js/server-core" (module) {
|
|
10
|
-
module.exports = require("@modern-js/server-core");
|
|
11
8
|
}
|
|
12
9
|
};
|
|
13
10
|
var __webpack_module_cache__ = {};
|
|
@@ -53,29 +50,16 @@ function __webpack_require__(moduleId) {
|
|
|
53
50
|
var __webpack_exports__ = {};
|
|
54
51
|
(()=>{
|
|
55
52
|
__webpack_require__.r(__webpack_exports__);
|
|
56
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
57
|
-
useHonoContext: ()=>_modern_js_server_core__rspack_import_1.useHonoContext
|
|
58
|
-
});
|
|
59
53
|
var _modern_js_bff_core__rspack_import_0 = __webpack_require__("@modern-js/bff-core");
|
|
60
54
|
var __rspack_reexport = {};
|
|
61
|
-
for(const __rspack_import_key in _modern_js_bff_core__rspack_import_0)if ([
|
|
62
|
-
"useHonoContext",
|
|
63
|
-
"default"
|
|
64
|
-
].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_modern_js_bff_core__rspack_import_0[__rspack_import_key];
|
|
55
|
+
for(const __rspack_import_key in _modern_js_bff_core__rspack_import_0)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_modern_js_bff_core__rspack_import_0[__rspack_import_key];
|
|
65
56
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
66
|
-
var
|
|
67
|
-
var _operators__rspack_import_2 = __webpack_require__("./operators");
|
|
57
|
+
var _operators__rspack_import_1 = __webpack_require__("./operators");
|
|
68
58
|
var __rspack_reexport = {};
|
|
69
|
-
for(const __rspack_import_key in
|
|
70
|
-
"useHonoContext",
|
|
71
|
-
"default"
|
|
72
|
-
].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_operators__rspack_import_2[__rspack_import_key];
|
|
59
|
+
for(const __rspack_import_key in _operators__rspack_import_1)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_operators__rspack_import_1[__rspack_import_key];
|
|
73
60
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
74
61
|
})();
|
|
75
|
-
exports
|
|
76
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
77
|
-
"useHonoContext"
|
|
78
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
62
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
79
63
|
Object.defineProperty(exports, '__esModule', {
|
|
80
64
|
value: true
|
|
81
65
|
});
|
package/dist/cjs/server.js
CHANGED
|
@@ -39,7 +39,6 @@ const external_path_namespaceObject = require("path");
|
|
|
39
39
|
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
40
40
|
const bff_core_namespaceObject = require("@modern-js/bff-core");
|
|
41
41
|
const utils_namespaceObject = require("@modern-js/utils");
|
|
42
|
-
const external_constants_js_namespaceObject = require("./constants.js");
|
|
43
42
|
const adapter_js_namespaceObject = require("./runtime/hono/adapter.js");
|
|
44
43
|
class Storage {
|
|
45
44
|
reset() {
|
|
@@ -49,27 +48,15 @@ class Storage {
|
|
|
49
48
|
this.middlewares = [];
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
|
-
const createTransformAPI = (storage)=>({
|
|
53
|
-
addMiddleware (fn) {
|
|
54
|
-
storage.middlewares.push(fn);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
51
|
const server = ()=>({
|
|
58
52
|
name: '@modern-js/plugin-bff',
|
|
59
53
|
setup: (api)=>{
|
|
60
54
|
const storage = new Storage();
|
|
61
|
-
const transformAPI = createTransformAPI(storage);
|
|
62
|
-
let apiAppPath = '';
|
|
63
55
|
let apiRouter;
|
|
64
56
|
const honoAdapter = new adapter_js_namespaceObject.HonoAdapter(api);
|
|
65
57
|
api.onPrepare(async ()=>{
|
|
66
58
|
const appContext = api.getServerContext();
|
|
67
|
-
const {
|
|
68
|
-
const root = (0, utils_namespaceObject.isProd)() ? distDirectory : appDirectory;
|
|
69
|
-
const apiPath = external_path_default().resolve(root || process.cwd(), utils_namespaceObject.API_DIR);
|
|
70
|
-
apiAppPath = external_path_default().resolve(apiPath, external_constants_js_namespaceObject.API_APP_NAME);
|
|
71
|
-
const apiMod = await (0, utils_namespaceObject.requireExistModule)(apiAppPath);
|
|
72
|
-
if (apiMod && 'function' == typeof apiMod) apiMod(transformAPI);
|
|
59
|
+
const { render } = appContext;
|
|
73
60
|
const { middlewares } = storage;
|
|
74
61
|
api.updateServerContext({
|
|
75
62
|
...appContext,
|
|
@@ -117,8 +104,6 @@ const server = ()=>({
|
|
|
117
104
|
api.onReset(async ({ event })=>{
|
|
118
105
|
storage.reset();
|
|
119
106
|
const appContext = api.getServerContext();
|
|
120
|
-
const newApiModule = await (0, utils_namespaceObject.requireExistModule)(apiAppPath);
|
|
121
|
-
if (newApiModule && 'function' == typeof newApiModule) newApiModule(transformAPI);
|
|
122
107
|
const { middlewares } = storage;
|
|
123
108
|
api.updateServerContext({
|
|
124
109
|
...appContext,
|
|
@@ -37,7 +37,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
37
37
|
createHonoHandler: ()=>createHonoHandler
|
|
38
38
|
});
|
|
39
39
|
const bff_core_namespaceObject = require("@modern-js/bff-core");
|
|
40
|
-
const
|
|
40
|
+
const external_qs_namespaceObject = require("qs");
|
|
41
41
|
const external_type_is_namespaceObject = require("type-is");
|
|
42
42
|
var external_type_is_default = /*#__PURE__*/ __webpack_require__.n(external_type_is_namespaceObject);
|
|
43
43
|
const createHonoRoutes = (handlerInfos = [])=>handlerInfos.map(({ routePath, handler, httpMethod })=>{
|
|
@@ -105,7 +105,7 @@ const createHonoHandler = (handler)=>async (c)=>{
|
|
|
105
105
|
const getHonoInput = async (c)=>{
|
|
106
106
|
const draft = {
|
|
107
107
|
params: c.req.param(),
|
|
108
|
-
query: (0,
|
|
108
|
+
query: (0, external_qs_namespaceObject.parse)(c.req.query()),
|
|
109
109
|
headers: c.req.header(),
|
|
110
110
|
cookies: c.req.header('cookie')
|
|
111
111
|
};
|
package/dist/esm/cli.mjs
CHANGED
|
@@ -2,12 +2,11 @@ import path from "path";
|
|
|
2
2
|
import { ApiRouter } from "@modern-js/bff-core";
|
|
3
3
|
import { compile } from "@modern-js/server-utils";
|
|
4
4
|
import { API_DIR, DEFAULT_API_PREFIX, SHARED_DIR, fs, normalizeOutputPath } from "@modern-js/utils";
|
|
5
|
-
import clientGenerator from "./utils/clientGenerator";
|
|
6
|
-
import pluginGenerator from "./utils/pluginGenerator";
|
|
7
|
-
import runtimeGenerator from "./utils/runtimeGenerator";
|
|
5
|
+
import clientGenerator from "./utils/clientGenerator.mjs";
|
|
6
|
+
import pluginGenerator from "./utils/pluginGenerator.mjs";
|
|
7
|
+
import runtimeGenerator from "./utils/runtimeGenerator.mjs";
|
|
8
8
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
9
9
|
const RUNTIME_CREATE_REQUEST = '@modern-js/plugin-bff/client';
|
|
10
|
-
const RUNTIME_HONO = '@modern-js/plugin-bff/server';
|
|
11
10
|
const bffPlugin = ()=>({
|
|
12
11
|
name: '@modern-js/plugin-bff',
|
|
13
12
|
setup: (api)=>{
|
|
@@ -24,13 +23,11 @@ const bffPlugin = ()=>({
|
|
|
24
23
|
const { server } = modernConfig;
|
|
25
24
|
const { alias } = modernConfig.source;
|
|
26
25
|
const { alias: resolveAlias } = modernConfig.resolve;
|
|
27
|
-
const { babel } = modernConfig.tools;
|
|
28
26
|
if (sourceDirs.length > 0) {
|
|
29
27
|
const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
|
|
30
28
|
await compile(appDirectory, {
|
|
31
29
|
server,
|
|
32
|
-
alias: combinedAlias
|
|
33
|
-
babelConfig: babel
|
|
30
|
+
alias: combinedAlias
|
|
34
31
|
}, {
|
|
35
32
|
sourceDirs,
|
|
36
33
|
distDir,
|
|
@@ -106,9 +103,6 @@ const bffPlugin = ()=>({
|
|
|
106
103
|
return compress;
|
|
107
104
|
};
|
|
108
105
|
api.config(async ()=>{
|
|
109
|
-
const honoRuntimePath = isHono() ? {
|
|
110
|
-
[RUNTIME_HONO]: RUNTIME_HONO
|
|
111
|
-
} : void 0;
|
|
112
106
|
const devServer = api.getConfig()?.tools?.devServer;
|
|
113
107
|
const prefix = api.getConfig()?.bff?.prefix || DEFAULT_API_PREFIX;
|
|
114
108
|
const compress = createCompressConfig(devServer, prefix);
|
|
@@ -135,7 +129,8 @@ const bffPlugin = ()=>({
|
|
|
135
129
|
const existLambda = apiRouter.isExistLambda();
|
|
136
130
|
const apiRegexp = new RegExp(normalizeOutputPath(`${apiDirectory}${path.sep}.*(.[tj]s)$`));
|
|
137
131
|
const name = isServer ? 'server' : 'client';
|
|
138
|
-
const
|
|
132
|
+
const sourceExt = 'mjs';
|
|
133
|
+
const loaderPath = path.join(__dirname, `loader.${sourceExt}`);
|
|
139
134
|
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(apiRegexp);
|
|
140
135
|
chain.module.rule('js-bff-api').test(apiRegexp).use('custom-loader').loader(loaderPath.replace(/\\/g, '/')).options({
|
|
141
136
|
prefix,
|
|
@@ -149,9 +144,6 @@ const bffPlugin = ()=>({
|
|
|
149
144
|
httpMethodDecider
|
|
150
145
|
});
|
|
151
146
|
}
|
|
152
|
-
},
|
|
153
|
-
output: {
|
|
154
|
-
externals: honoRuntimePath
|
|
155
147
|
}
|
|
156
148
|
};
|
|
157
149
|
});
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./constants";
|
|
1
|
+
export * from "./constants.mjs";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Hono } from "@modern-js/server-core";
|
|
1
|
+
import { Hono, run } from "@modern-js/server-core";
|
|
2
2
|
import { isProd, logger } from "@modern-js/utils";
|
|
3
|
-
import createHonoRoutes from "../../utils/createHonoRoutes";
|
|
3
|
+
import createHonoRoutes from "../../utils/createHonoRoutes.mjs";
|
|
4
4
|
const before = [
|
|
5
5
|
'custom-server-hook',
|
|
6
6
|
'custom-server-middleware',
|
|
@@ -33,6 +33,7 @@ class HonoAdapter {
|
|
|
33
33
|
this.registerApiRoutes = async ()=>{
|
|
34
34
|
if (!this.isHono) return;
|
|
35
35
|
this.apiServer = new Hono();
|
|
36
|
+
this.apiServer.use('*', run);
|
|
36
37
|
this.apiMiddleware.forEach(({ path = '*', method = 'all', handler })=>{
|
|
37
38
|
const handlers = this.wrapInArray(handler);
|
|
38
39
|
if (0 === handlers.length) return;
|
package/dist/esm/server.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { ApiRouter } from "@modern-js/bff-core";
|
|
3
|
-
import { API_DIR, isFunction,
|
|
4
|
-
import {
|
|
5
|
-
import { HonoAdapter } from "./runtime/hono/adapter";
|
|
3
|
+
import { API_DIR, isFunction, isWebOnly } from "@modern-js/utils";
|
|
4
|
+
import { HonoAdapter } from "./runtime/hono/adapter.mjs";
|
|
6
5
|
class Storage {
|
|
7
6
|
reset() {
|
|
8
7
|
this.middlewares = [];
|
|
@@ -11,27 +10,15 @@ class Storage {
|
|
|
11
10
|
this.middlewares = [];
|
|
12
11
|
}
|
|
13
12
|
}
|
|
14
|
-
const createTransformAPI = (storage)=>({
|
|
15
|
-
addMiddleware (fn) {
|
|
16
|
-
storage.middlewares.push(fn);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
13
|
const server = ()=>({
|
|
20
14
|
name: '@modern-js/plugin-bff',
|
|
21
15
|
setup: (api)=>{
|
|
22
16
|
const storage = new Storage();
|
|
23
|
-
const transformAPI = createTransformAPI(storage);
|
|
24
|
-
let apiAppPath = '';
|
|
25
17
|
let apiRouter;
|
|
26
18
|
const honoAdapter = new HonoAdapter(api);
|
|
27
19
|
api.onPrepare(async ()=>{
|
|
28
20
|
const appContext = api.getServerContext();
|
|
29
|
-
const {
|
|
30
|
-
const root = isProd() ? distDirectory : appDirectory;
|
|
31
|
-
const apiPath = path.resolve(root || process.cwd(), API_DIR);
|
|
32
|
-
apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
33
|
-
const apiMod = await requireExistModule(apiAppPath);
|
|
34
|
-
if (apiMod && 'function' == typeof apiMod) apiMod(transformAPI);
|
|
21
|
+
const { render } = appContext;
|
|
35
22
|
const { middlewares } = storage;
|
|
36
23
|
api.updateServerContext({
|
|
37
24
|
...appContext,
|
|
@@ -79,8 +66,6 @@ const server = ()=>({
|
|
|
79
66
|
api.onReset(async ({ event })=>{
|
|
80
67
|
storage.reset();
|
|
81
68
|
const appContext = api.getServerContext();
|
|
82
|
-
const newApiModule = await requireExistModule(apiAppPath);
|
|
83
|
-
if (newApiModule && 'function' == typeof newApiModule) newApiModule(transformAPI);
|
|
84
69
|
const { middlewares } = storage;
|
|
85
70
|
api.updateServerContext({
|
|
86
71
|
...appContext,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HttpMetadata, ResponseMetaType, ValidationError, isWithMetaHandler } from "@modern-js/bff-core";
|
|
2
|
-
import { parse } from "
|
|
2
|
+
import { parse } from "qs";
|
|
3
3
|
import type_is from "type-is";
|
|
4
4
|
const createHonoRoutes = (handlerInfos = [])=>handlerInfos.map(({ routePath, handler, httpMethod })=>{
|
|
5
5
|
const routeMiddlwares = Reflect.getMetadata('middleware', handler) || [];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { fs, logger, normalizeToPosixPath } from "@modern-js/utils";
|
|
3
|
-
import { API_DIR, DIST_DIR, LAMBDA_DIR, PACKAGE_NAME, PREFIX } from "./crossProjectApiPlugin";
|
|
3
|
+
import { API_DIR, DIST_DIR, LAMBDA_DIR, PACKAGE_NAME, PREFIX } from "./crossProjectApiPlugin.mjs";
|
|
4
4
|
function replaceContent(source, packageName, prefix, relativeDistPath, relativeApiPath, relativeLambdaPath) {
|
|
5
5
|
const updatedSource = source.replace(new RegExp(PACKAGE_NAME, 'g'), packageName).replace(new RegExp(PREFIX, 'g'), prefix).replace(new RegExp(DIST_DIR, 'g'), normalizeToPosixPath(relativeDistPath)).replace(new RegExp(API_DIR, 'g'), normalizeToPosixPath(relativeApiPath)).replace(new RegExp(LAMBDA_DIR, 'g'), normalizeToPosixPath(relativeLambdaPath));
|
|
6
6
|
return updatedSource;
|
package/dist/esm-node/cli.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
3
|
+
import { dirname as __rspack_dirname } from "node:path";
|
|
1
4
|
import path from "path";
|
|
2
5
|
import { ApiRouter } from "@modern-js/bff-core";
|
|
3
6
|
import { compile } from "@modern-js/server-utils";
|
|
@@ -5,9 +8,9 @@ import { API_DIR, DEFAULT_API_PREFIX, SHARED_DIR, fs, normalizeOutputPath } from
|
|
|
5
8
|
import clientGenerator from "./utils/clientGenerator.mjs";
|
|
6
9
|
import pluginGenerator from "./utils/pluginGenerator.mjs";
|
|
7
10
|
import runtimeGenerator from "./utils/runtimeGenerator.mjs";
|
|
11
|
+
var cli_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
|
|
8
12
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
9
13
|
const RUNTIME_CREATE_REQUEST = '@modern-js/plugin-bff/client';
|
|
10
|
-
const RUNTIME_HONO = '@modern-js/plugin-bff/server';
|
|
11
14
|
const bffPlugin = ()=>({
|
|
12
15
|
name: '@modern-js/plugin-bff',
|
|
13
16
|
setup: (api)=>{
|
|
@@ -24,13 +27,11 @@ const bffPlugin = ()=>({
|
|
|
24
27
|
const { server } = modernConfig;
|
|
25
28
|
const { alias } = modernConfig.source;
|
|
26
29
|
const { alias: resolveAlias } = modernConfig.resolve;
|
|
27
|
-
const { babel } = modernConfig.tools;
|
|
28
30
|
if (sourceDirs.length > 0) {
|
|
29
31
|
const combinedAlias = [].concat(alias ?? []).concat(resolveAlias ?? []);
|
|
30
32
|
await compile(appDirectory, {
|
|
31
33
|
server,
|
|
32
|
-
alias: combinedAlias
|
|
33
|
-
babelConfig: babel
|
|
34
|
+
alias: combinedAlias
|
|
34
35
|
}, {
|
|
35
36
|
sourceDirs,
|
|
36
37
|
distDir,
|
|
@@ -106,9 +107,6 @@ const bffPlugin = ()=>({
|
|
|
106
107
|
return compress;
|
|
107
108
|
};
|
|
108
109
|
api.config(async ()=>{
|
|
109
|
-
const honoRuntimePath = isHono() ? {
|
|
110
|
-
[RUNTIME_HONO]: RUNTIME_HONO
|
|
111
|
-
} : void 0;
|
|
112
110
|
const devServer = api.getConfig()?.tools?.devServer;
|
|
113
111
|
const prefix = api.getConfig()?.bff?.prefix || DEFAULT_API_PREFIX;
|
|
114
112
|
const compress = createCompressConfig(devServer, prefix);
|
|
@@ -135,7 +133,8 @@ const bffPlugin = ()=>({
|
|
|
135
133
|
const existLambda = apiRouter.isExistLambda();
|
|
136
134
|
const apiRegexp = new RegExp(normalizeOutputPath(`${apiDirectory}${path.sep}.*(.[tj]s)$`));
|
|
137
135
|
const name = isServer ? 'server' : 'client';
|
|
138
|
-
const
|
|
136
|
+
const sourceExt = 'mjs';
|
|
137
|
+
const loaderPath = path.join(cli_dirname, `loader.${sourceExt}`);
|
|
139
138
|
chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(apiRegexp);
|
|
140
139
|
chain.module.rule('js-bff-api').test(apiRegexp).use('custom-loader').loader(loaderPath.replace(/\\/g, '/')).options({
|
|
141
140
|
prefix,
|
|
@@ -149,9 +148,6 @@ const bffPlugin = ()=>({
|
|
|
149
148
|
httpMethodDecider
|
|
150
149
|
});
|
|
151
150
|
}
|
|
152
|
-
},
|
|
153
|
-
output: {
|
|
154
|
-
externals: honoRuntimePath
|
|
155
151
|
}
|
|
156
152
|
};
|
|
157
153
|
});
|
package/dist/esm-node/index.mjs
CHANGED
package/dist/esm-node/loader.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
1
3
|
import { generateClient } from "@modern-js/bff-core";
|
|
2
4
|
import { logger } from "@modern-js/utils";
|
|
3
5
|
async function loader(source) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { Hono, run } from "@modern-js/server-core";
|
|
2
3
|
import { isProd, logger } from "@modern-js/utils";
|
|
3
4
|
import createHonoRoutes from "../../utils/createHonoRoutes.mjs";
|
|
4
5
|
const before = [
|
|
@@ -33,6 +34,7 @@ class HonoAdapter {
|
|
|
33
34
|
this.registerApiRoutes = async ()=>{
|
|
34
35
|
if (!this.isHono) return;
|
|
35
36
|
this.apiServer = new Hono();
|
|
37
|
+
this.apiServer.use('*', run);
|
|
36
38
|
this.apiMiddleware.forEach(({ path = '*', method = 'all', handler })=>{
|
|
37
39
|
const handlers = this.wrapInArray(handler);
|
|
38
40
|
if (0 === handlers.length) return;
|
package/dist/esm-node/server.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import "node:module";
|
|
1
2
|
import path from "path";
|
|
2
3
|
import { ApiRouter } from "@modern-js/bff-core";
|
|
3
|
-
import { API_DIR, isFunction,
|
|
4
|
-
import { API_APP_NAME } from "./constants.mjs";
|
|
4
|
+
import { API_DIR, isFunction, isWebOnly } from "@modern-js/utils";
|
|
5
5
|
import { HonoAdapter } from "./runtime/hono/adapter.mjs";
|
|
6
6
|
class Storage {
|
|
7
7
|
reset() {
|
|
@@ -11,27 +11,15 @@ class Storage {
|
|
|
11
11
|
this.middlewares = [];
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
const createTransformAPI = (storage)=>({
|
|
15
|
-
addMiddleware (fn) {
|
|
16
|
-
storage.middlewares.push(fn);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
14
|
const server = ()=>({
|
|
20
15
|
name: '@modern-js/plugin-bff',
|
|
21
16
|
setup: (api)=>{
|
|
22
17
|
const storage = new Storage();
|
|
23
|
-
const transformAPI = createTransformAPI(storage);
|
|
24
|
-
let apiAppPath = '';
|
|
25
18
|
let apiRouter;
|
|
26
19
|
const honoAdapter = new HonoAdapter(api);
|
|
27
20
|
api.onPrepare(async ()=>{
|
|
28
21
|
const appContext = api.getServerContext();
|
|
29
|
-
const {
|
|
30
|
-
const root = isProd() ? distDirectory : appDirectory;
|
|
31
|
-
const apiPath = path.resolve(root || process.cwd(), API_DIR);
|
|
32
|
-
apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
33
|
-
const apiMod = await requireExistModule(apiAppPath);
|
|
34
|
-
if (apiMod && 'function' == typeof apiMod) apiMod(transformAPI);
|
|
22
|
+
const { render } = appContext;
|
|
35
23
|
const { middlewares } = storage;
|
|
36
24
|
api.updateServerContext({
|
|
37
25
|
...appContext,
|
|
@@ -79,8 +67,6 @@ const server = ()=>({
|
|
|
79
67
|
api.onReset(async ({ event })=>{
|
|
80
68
|
storage.reset();
|
|
81
69
|
const appContext = api.getServerContext();
|
|
82
|
-
const newApiModule = await requireExistModule(apiAppPath);
|
|
83
|
-
if (newApiModule && 'function' == typeof newApiModule) newApiModule(transformAPI);
|
|
84
70
|
const { middlewares } = storage;
|
|
85
71
|
api.updateServerContext({
|
|
86
72
|
...appContext,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import "node:module";
|
|
1
2
|
import { HttpMetadata, ResponseMetaType, ValidationError, isWithMetaHandler } from "@modern-js/bff-core";
|
|
2
|
-
import { parse } from "
|
|
3
|
+
import { parse } from "qs";
|
|
3
4
|
import type_is from "type-is";
|
|
4
5
|
const createHonoRoutes = (handlerInfos = [])=>handlerInfos.map(({ routePath, handler, httpMethod })=>{
|
|
5
6
|
const routeMiddlwares = Reflect.getMetadata('middleware', handler) || [];
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
3
|
+
import { dirname as __rspack_dirname } from "node:path";
|
|
1
4
|
import path from "path";
|
|
2
5
|
import { fs, logger, normalizeToPosixPath } from "@modern-js/utils";
|
|
3
6
|
import { API_DIR, DIST_DIR, LAMBDA_DIR, PACKAGE_NAME, PREFIX } from "./crossProjectApiPlugin.mjs";
|
|
7
|
+
var pluginGenerator_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
|
|
4
8
|
function replaceContent(source, packageName, prefix, relativeDistPath, relativeApiPath, relativeLambdaPath) {
|
|
5
9
|
const updatedSource = source.replace(new RegExp(PACKAGE_NAME, 'g'), packageName).replace(new RegExp(PREFIX, 'g'), prefix).replace(new RegExp(DIST_DIR, 'g'), normalizeToPosixPath(relativeDistPath)).replace(new RegExp(API_DIR, 'g'), normalizeToPosixPath(relativeApiPath)).replace(new RegExp(LAMBDA_DIR, 'g'), normalizeToPosixPath(relativeLambdaPath));
|
|
6
10
|
return updatedSource;
|
|
@@ -11,7 +15,7 @@ async function pluginGenerator({ prefix, appDirectory, relativeDistPath, relativ
|
|
|
11
15
|
const packageJson = JSON.parse(packageContent);
|
|
12
16
|
const pluginDir = path.resolve(appDirectory, `./${relativeDistPath}`, 'plugin');
|
|
13
17
|
const pluginPath = path.join(pluginDir, 'index.js');
|
|
14
|
-
const pluginTemplate = await fs.readFile(path.resolve(
|
|
18
|
+
const pluginTemplate = await fs.readFile(path.resolve(pluginGenerator_dirname, 'crossProjectApiPlugin.js'), 'utf8');
|
|
15
19
|
const updatedPlugin = replaceContent(pluginTemplate, packageJson.name, prefix, relativeDistPath, relativeApiPath, relativeLambdaPath);
|
|
16
20
|
await fs.ensureFile(pluginPath);
|
|
17
21
|
await fs.writeFile(pluginPath, updatedPlugin);
|
package/package.json
CHANGED
|
@@ -15,15 +15,12 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "3.0.0
|
|
19
|
-
"jsnext:source": "./src/cli.ts",
|
|
18
|
+
"version": "3.0.0",
|
|
20
19
|
"types": "./dist/types/cli.d.ts",
|
|
21
20
|
"main": "./dist/cjs/cli.js",
|
|
22
|
-
"module": "./dist/esm-node/cli.mjs",
|
|
23
21
|
"exports": {
|
|
24
22
|
".": {
|
|
25
23
|
"types": "./dist/types/cli.d.ts",
|
|
26
|
-
"jsnext:source": "./src/cli.ts",
|
|
27
24
|
"node": {
|
|
28
25
|
"import": "./dist/esm-node/cli.mjs",
|
|
29
26
|
"require": "./dist/cjs/cli.js"
|
|
@@ -32,7 +29,6 @@
|
|
|
32
29
|
},
|
|
33
30
|
"./cli": {
|
|
34
31
|
"types": "./dist/types/cli.d.ts",
|
|
35
|
-
"jsnext:source": "./src/cli.ts",
|
|
36
32
|
"node": {
|
|
37
33
|
"import": "./dist/esm-node/cli.mjs",
|
|
38
34
|
"require": "./dist/cjs/cli.js"
|
|
@@ -41,25 +37,14 @@
|
|
|
41
37
|
},
|
|
42
38
|
"./server-plugin": {
|
|
43
39
|
"types": "./dist/types/server.d.ts",
|
|
44
|
-
"jsnext:source": "./src/server.ts",
|
|
45
40
|
"node": {
|
|
46
41
|
"import": "./dist/esm-node/server.mjs",
|
|
47
42
|
"require": "./dist/cjs/server.js"
|
|
48
43
|
},
|
|
49
44
|
"default": "./dist/cjs/server.js"
|
|
50
45
|
},
|
|
51
|
-
"./loader": {
|
|
52
|
-
"types": "./dist/types/loader.d.ts",
|
|
53
|
-
"jsnext:source": "./src/loader.ts",
|
|
54
|
-
"node": {
|
|
55
|
-
"import": "./dist/esm-node/loader.mjs",
|
|
56
|
-
"require": "./dist/cjs/loader.js"
|
|
57
|
-
},
|
|
58
|
-
"default": "./dist/cjs/loader.js"
|
|
59
|
-
},
|
|
60
46
|
"./server": {
|
|
61
47
|
"types": "./dist/types/runtime/hono/index.d.ts",
|
|
62
|
-
"jsnext:source": "./src/runtime/hono/index.ts",
|
|
63
48
|
"node": {
|
|
64
49
|
"import": "./dist/esm-node/runtime/hono/index.mjs",
|
|
65
50
|
"require": "./dist/cjs/runtime/hono/index.js"
|
|
@@ -67,13 +52,8 @@
|
|
|
67
52
|
"default": "./dist/cjs/runtime/hono/index.js"
|
|
68
53
|
},
|
|
69
54
|
"./client": {
|
|
70
|
-
"types": "./dist/types/create-request/index.d.ts",
|
|
71
|
-
"
|
|
72
|
-
"node": {
|
|
73
|
-
"import": "./dist/esm-node/runtime/create-request/index.mjs",
|
|
74
|
-
"require": "./dist/cjs/runtime/create-request/index.js"
|
|
75
|
-
},
|
|
76
|
-
"default": "./dist/cjs/runtime/create-request/index.js"
|
|
55
|
+
"types": "./dist/types/runtime/create-request/index.d.ts",
|
|
56
|
+
"default": "./dist/esm/runtime/create-request/index.mjs"
|
|
77
57
|
}
|
|
78
58
|
},
|
|
79
59
|
"typesVersions": {
|
|
@@ -96,35 +76,32 @@
|
|
|
96
76
|
}
|
|
97
77
|
},
|
|
98
78
|
"dependencies": {
|
|
99
|
-
"@babel/core": "^7.28.5",
|
|
100
79
|
"@swc/helpers": "^0.5.17",
|
|
80
|
+
"qs": "^6.14.1",
|
|
101
81
|
"type-is": "^1.6.18",
|
|
102
|
-
"@modern-js/bff-core": "3.0.0
|
|
103
|
-
"@modern-js/
|
|
104
|
-
"@modern-js/server-
|
|
105
|
-
"@modern-js/
|
|
106
|
-
"@modern-js/server-
|
|
107
|
-
"@modern-js/utils": "3.0.0
|
|
82
|
+
"@modern-js/bff-core": "3.0.0",
|
|
83
|
+
"@modern-js/builder": "3.0.0",
|
|
84
|
+
"@modern-js/server-core": "3.0.0",
|
|
85
|
+
"@modern-js/create-request": "3.0.0",
|
|
86
|
+
"@modern-js/server-utils": "3.0.0",
|
|
87
|
+
"@modern-js/utils": "3.0.0"
|
|
108
88
|
},
|
|
109
89
|
"devDependencies": {
|
|
110
|
-
"@rsbuild/core": "
|
|
111
|
-
"@rslib/core": "0.19.
|
|
112
|
-
"@types/babel__core": "^7.20.5",
|
|
113
|
-
"@types/jest": "^29.5.14",
|
|
90
|
+
"@rsbuild/core": "2.0.0-beta.1",
|
|
91
|
+
"@rslib/core": "0.19.4",
|
|
114
92
|
"@types/node": "^20",
|
|
93
|
+
"@types/qs": "^6.14.0",
|
|
115
94
|
"@types/type-is": "^1.6.7",
|
|
116
|
-
"jest": "^29.7.0",
|
|
117
95
|
"memfs": "^3.5.3",
|
|
118
|
-
"ts-jest": "^29.4.6",
|
|
119
96
|
"typescript": "^5",
|
|
120
97
|
"zod": "^3.25.76",
|
|
121
|
-
"@modern-js/
|
|
122
|
-
"@modern-js/
|
|
123
|
-
"@modern-js/plugin": "3.0.0
|
|
124
|
-
"@modern-js/
|
|
125
|
-
"@modern-js/
|
|
126
|
-
"@
|
|
127
|
-
"@
|
|
98
|
+
"@modern-js/bff-runtime": "3.0.0",
|
|
99
|
+
"@modern-js/app-tools": "3.0.0",
|
|
100
|
+
"@modern-js/plugin": "3.0.0",
|
|
101
|
+
"@modern-js/rslib": "2.68.10",
|
|
102
|
+
"@modern-js/runtime": "3.0.0",
|
|
103
|
+
"@modern-js/types": "3.0.0",
|
|
104
|
+
"@scripts/rstest-config": "2.66.0"
|
|
128
105
|
},
|
|
129
106
|
"sideEffects": false,
|
|
130
107
|
"publishConfig": {
|
|
@@ -134,6 +111,6 @@
|
|
|
134
111
|
"scripts": {
|
|
135
112
|
"build": "rslib build",
|
|
136
113
|
"dev": "rslib build --watch",
|
|
137
|
-
"test": "
|
|
114
|
+
"test": "rstest --passWithNoTests"
|
|
138
115
|
}
|
|
139
116
|
}
|