@modern-js/plugin-bff 2.53.0 → 2.54.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.
package/dist/cjs/cli.js CHANGED
@@ -121,9 +121,9 @@ const bffPlugin = () => ({
121
121
  routes: routes.concat(apiServerRoutes)
122
122
  };
123
123
  },
124
- collectServerPlugins({ plugins }) {
124
+ _internalServerPlugins({ plugins }) {
125
125
  plugins.push({
126
- "@modern-js/plugin-bff": "@modern-js/plugin-bff/server"
126
+ name: "@modern-js/plugin-bff/server"
127
127
  });
128
128
  return {
129
129
  plugins
@@ -34,6 +34,7 @@ module.exports = __toCommonJS(server_exports);
34
34
  var import_path = __toESM(require("path"));
35
35
  var import_bff_core = require("@modern-js/bff-core");
36
36
  var import_utils = require("@modern-js/utils");
37
+ var import_server_core = require("@modern-js/server-core");
37
38
  var import_constants = require("./constants");
38
39
  class Storage {
39
40
  reset() {
@@ -56,7 +57,8 @@ var server_default = () => ({
56
57
  let apiAppPath = "";
57
58
  let apiRouter;
58
59
  return {
59
- prepare() {
60
+ async prepare() {
61
+ var _config_bff, _config_bff1, _config_bff2;
60
62
  const appContext = api.useAppContext();
61
63
  const { appDirectory, distDirectory } = appContext;
62
64
  const root = (0, import_utils.isProd)() ? distDirectory : appDirectory;
@@ -71,8 +73,54 @@ var server_default = () => ({
71
73
  ...appContext,
72
74
  apiMiddlewares: middlewares
73
75
  });
76
+ const config = api.useConfigContext();
77
+ const prefix = (config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix) || "/api";
78
+ const enableHandleWeb = config === null || config === void 0 ? void 0 : (_config_bff1 = config.bff) === null || _config_bff1 === void 0 ? void 0 : _config_bff1.enableHandleWeb;
79
+ const httpMethodDecider = config === null || config === void 0 ? void 0 : (_config_bff2 = config.bff) === null || _config_bff2 === void 0 ? void 0 : _config_bff2.httpMethodDecider;
80
+ const { distDirectory: pwd, routes, middlewares: globalMiddlewares } = api.useAppContext();
81
+ const webOnly = await (0, import_utils.isWebOnly)();
82
+ let handler;
83
+ if (webOnly) {
84
+ handler = async (c, next) => {
85
+ c.body("");
86
+ await next();
87
+ };
88
+ } else {
89
+ const runner = api.useHookRunners();
90
+ const renderHandler = enableHandleWeb ? await (0, import_server_core.getRenderHandler)({
91
+ pwd,
92
+ routes: routes || [],
93
+ config
94
+ }) : null;
95
+ handler = await runner.prepareApiServer({
96
+ pwd,
97
+ prefix,
98
+ render: renderHandler,
99
+ httpMethodDecider
100
+ }, {
101
+ onLast: () => null
102
+ });
103
+ }
104
+ if (handler) {
105
+ globalMiddlewares.push({
106
+ name: "bind-bff",
107
+ handler: (c, next) => {
108
+ if (!c.req.path.startsWith(prefix) && !enableHandleWeb) {
109
+ return next();
110
+ } else {
111
+ return handler(c, next);
112
+ }
113
+ },
114
+ order: "post",
115
+ before: [
116
+ "custom-server-hook",
117
+ "custom-server-middleware",
118
+ "render"
119
+ ]
120
+ });
121
+ }
74
122
  },
75
- reset() {
123
+ reset({ event }) {
76
124
  storage.reset();
77
125
  const appContext = api.useAppContext();
78
126
  const newApiModule = (0, import_utils.requireExistModule)(apiAppPath);
@@ -84,15 +132,14 @@ var server_default = () => ({
84
132
  ...appContext,
85
133
  apiMiddlewares: middlewares
86
134
  });
87
- },
88
- onApiChange(changes) {
89
- const apiHandlerInfos = apiRouter.getApiHandlers();
90
- const appContext = api.useAppContext();
91
- api.setAppContext({
92
- ...appContext,
93
- apiHandlerInfos
94
- });
95
- return changes;
135
+ if (event.type === "file-change") {
136
+ const apiHandlerInfos = apiRouter.getApiHandlers();
137
+ const appContext2 = api.useAppContext();
138
+ api.setAppContext({
139
+ ...appContext2,
140
+ apiHandlerInfos
141
+ });
142
+ }
96
143
  },
97
144
  prepareApiServer(props, next) {
98
145
  const { pwd, prefix, httpMethodDecider } = props;
package/dist/esm/cli.js CHANGED
@@ -97,10 +97,10 @@ var bffPlugin = function() {
97
97
  routes: routes.concat(apiServerRoutes)
98
98
  };
99
99
  },
100
- collectServerPlugins: function collectServerPlugins(param) {
100
+ _internalServerPlugins: function _internalServerPlugins(param) {
101
101
  var plugins = param.plugins;
102
102
  plugins.push({
103
- "@modern-js/plugin-bff": "@modern-js/plugin-bff/server"
103
+ name: "@modern-js/plugin-bff/server"
104
104
  });
105
105
  return {
106
106
  plugins
@@ -1,9 +1,12 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
1
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
3
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
4
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
5
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
6
  import path from "path";
5
7
  import { ApiRouter } from "@modern-js/bff-core";
6
- import { API_DIR, isProd, requireExistModule } from "@modern-js/utils";
8
+ import { API_DIR, isProd, isWebOnly, requireExistModule } from "@modern-js/utils";
9
+ import { getRenderHandler } from "@modern-js/server-core";
7
10
  import { API_APP_NAME } from "./constants";
8
11
  var Storage = /* @__PURE__ */ function() {
9
12
  "use strict";
@@ -34,21 +37,136 @@ function server_default() {
34
37
  var apiRouter;
35
38
  return {
36
39
  prepare: function prepare() {
37
- var appContext = api.useAppContext();
38
- var appDirectory = appContext.appDirectory, distDirectory = appContext.distDirectory;
39
- var root = isProd() ? distDirectory : appDirectory;
40
- var apiPath = path.resolve(root || process.cwd(), API_DIR);
41
- apiAppPath = path.resolve(apiPath, API_APP_NAME);
42
- var apiMod = requireExistModule(apiAppPath);
43
- if (apiMod && typeof apiMod === "function") {
44
- apiMod(transformAPI);
45
- }
46
- var middlewares = storage.middlewares;
47
- api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
48
- apiMiddlewares: middlewares
49
- }));
40
+ return _async_to_generator(function() {
41
+ var _config_bff, _config_bff1, _config_bff2, appContext, appDirectory, distDirectory, root, apiPath, apiMod, middlewares, config, prefix, enableHandleWeb, httpMethodDecider, _api_useAppContext, pwd, routes, globalMiddlewares, webOnly, handler, runner, renderHandler, _tmp;
42
+ return _ts_generator(this, function(_state) {
43
+ switch (_state.label) {
44
+ case 0:
45
+ appContext = api.useAppContext();
46
+ appDirectory = appContext.appDirectory, distDirectory = appContext.distDirectory;
47
+ root = isProd() ? distDirectory : appDirectory;
48
+ apiPath = path.resolve(root || process.cwd(), API_DIR);
49
+ apiAppPath = path.resolve(apiPath, API_APP_NAME);
50
+ apiMod = requireExistModule(apiAppPath);
51
+ if (apiMod && typeof apiMod === "function") {
52
+ apiMod(transformAPI);
53
+ }
54
+ middlewares = storage.middlewares;
55
+ api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
56
+ apiMiddlewares: middlewares
57
+ }));
58
+ config = api.useConfigContext();
59
+ prefix = (config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix) || "/api";
60
+ enableHandleWeb = config === null || config === void 0 ? void 0 : (_config_bff1 = config.bff) === null || _config_bff1 === void 0 ? void 0 : _config_bff1.enableHandleWeb;
61
+ httpMethodDecider = config === null || config === void 0 ? void 0 : (_config_bff2 = config.bff) === null || _config_bff2 === void 0 ? void 0 : _config_bff2.httpMethodDecider;
62
+ _api_useAppContext = api.useAppContext(), pwd = _api_useAppContext.distDirectory, routes = _api_useAppContext.routes, globalMiddlewares = _api_useAppContext.middlewares;
63
+ return [
64
+ 4,
65
+ isWebOnly()
66
+ ];
67
+ case 1:
68
+ webOnly = _state.sent();
69
+ if (!webOnly)
70
+ return [
71
+ 3,
72
+ 2
73
+ ];
74
+ handler = function() {
75
+ var _ref = _async_to_generator(function(c, next) {
76
+ return _ts_generator(this, function(_state2) {
77
+ switch (_state2.label) {
78
+ case 0:
79
+ c.body("");
80
+ return [
81
+ 4,
82
+ next()
83
+ ];
84
+ case 1:
85
+ _state2.sent();
86
+ return [
87
+ 2
88
+ ];
89
+ }
90
+ });
91
+ });
92
+ return function(c, next) {
93
+ return _ref.apply(this, arguments);
94
+ };
95
+ }();
96
+ return [
97
+ 3,
98
+ 7
99
+ ];
100
+ case 2:
101
+ runner = api.useHookRunners();
102
+ if (!enableHandleWeb)
103
+ return [
104
+ 3,
105
+ 4
106
+ ];
107
+ return [
108
+ 4,
109
+ getRenderHandler({
110
+ pwd,
111
+ routes: routes || [],
112
+ config
113
+ })
114
+ ];
115
+ case 3:
116
+ _tmp = _state.sent();
117
+ return [
118
+ 3,
119
+ 5
120
+ ];
121
+ case 4:
122
+ _tmp = null;
123
+ _state.label = 5;
124
+ case 5:
125
+ renderHandler = _tmp;
126
+ return [
127
+ 4,
128
+ runner.prepareApiServer({
129
+ pwd,
130
+ prefix,
131
+ render: renderHandler,
132
+ httpMethodDecider
133
+ }, {
134
+ onLast: function() {
135
+ return null;
136
+ }
137
+ })
138
+ ];
139
+ case 6:
140
+ handler = _state.sent();
141
+ _state.label = 7;
142
+ case 7:
143
+ if (handler) {
144
+ globalMiddlewares.push({
145
+ name: "bind-bff",
146
+ handler: function(c, next) {
147
+ if (!c.req.path.startsWith(prefix) && !enableHandleWeb) {
148
+ return next();
149
+ } else {
150
+ return handler(c, next);
151
+ }
152
+ },
153
+ order: "post",
154
+ before: [
155
+ "custom-server-hook",
156
+ "custom-server-middleware",
157
+ "render"
158
+ ]
159
+ });
160
+ }
161
+ return [
162
+ 2
163
+ ];
164
+ }
165
+ });
166
+ })();
50
167
  },
51
- reset: function reset() {
168
+ reset: function reset(param) {
169
+ var event = param.event;
52
170
  storage.reset();
53
171
  var appContext = api.useAppContext();
54
172
  var newApiModule = requireExistModule(apiAppPath);
@@ -59,14 +177,13 @@ function server_default() {
59
177
  api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
60
178
  apiMiddlewares: middlewares
61
179
  }));
62
- },
63
- onApiChange: function onApiChange(changes) {
64
- var apiHandlerInfos = apiRouter.getApiHandlers();
65
- var appContext = api.useAppContext();
66
- api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
67
- apiHandlerInfos
68
- }));
69
- return changes;
180
+ if (event.type === "file-change") {
181
+ var apiHandlerInfos = apiRouter.getApiHandlers();
182
+ var appContext1 = api.useAppContext();
183
+ api.setAppContext(_object_spread_props(_object_spread({}, appContext1), {
184
+ apiHandlerInfos
185
+ }));
186
+ }
70
187
  },
71
188
  prepareApiServer: function prepareApiServer(props, next) {
72
189
  var pwd = props.pwd, prefix = props.prefix, httpMethodDecider = props.httpMethodDecider;
@@ -87,9 +87,9 @@ const bffPlugin = () => ({
87
87
  routes: routes.concat(apiServerRoutes)
88
88
  };
89
89
  },
90
- collectServerPlugins({ plugins }) {
90
+ _internalServerPlugins({ plugins }) {
91
91
  plugins.push({
92
- "@modern-js/plugin-bff": "@modern-js/plugin-bff/server"
92
+ name: "@modern-js/plugin-bff/server"
93
93
  });
94
94
  return {
95
95
  plugins
@@ -1,6 +1,7 @@
1
1
  import path from "path";
2
2
  import { ApiRouter } from "@modern-js/bff-core";
3
- import { API_DIR, isProd, requireExistModule } from "@modern-js/utils";
3
+ import { API_DIR, isProd, isWebOnly, requireExistModule } from "@modern-js/utils";
4
+ import { getRenderHandler } from "@modern-js/server-core";
4
5
  import { API_APP_NAME } from "./constants";
5
6
  class Storage {
6
7
  reset() {
@@ -23,7 +24,8 @@ var server_default = () => ({
23
24
  let apiAppPath = "";
24
25
  let apiRouter;
25
26
  return {
26
- prepare() {
27
+ async prepare() {
28
+ var _config_bff, _config_bff1, _config_bff2;
27
29
  const appContext = api.useAppContext();
28
30
  const { appDirectory, distDirectory } = appContext;
29
31
  const root = isProd() ? distDirectory : appDirectory;
@@ -38,8 +40,54 @@ var server_default = () => ({
38
40
  ...appContext,
39
41
  apiMiddlewares: middlewares
40
42
  });
43
+ const config = api.useConfigContext();
44
+ const prefix = (config === null || config === void 0 ? void 0 : (_config_bff = config.bff) === null || _config_bff === void 0 ? void 0 : _config_bff.prefix) || "/api";
45
+ const enableHandleWeb = config === null || config === void 0 ? void 0 : (_config_bff1 = config.bff) === null || _config_bff1 === void 0 ? void 0 : _config_bff1.enableHandleWeb;
46
+ const httpMethodDecider = config === null || config === void 0 ? void 0 : (_config_bff2 = config.bff) === null || _config_bff2 === void 0 ? void 0 : _config_bff2.httpMethodDecider;
47
+ const { distDirectory: pwd, routes, middlewares: globalMiddlewares } = api.useAppContext();
48
+ const webOnly = await isWebOnly();
49
+ let handler;
50
+ if (webOnly) {
51
+ handler = async (c, next) => {
52
+ c.body("");
53
+ await next();
54
+ };
55
+ } else {
56
+ const runner = api.useHookRunners();
57
+ const renderHandler = enableHandleWeb ? await getRenderHandler({
58
+ pwd,
59
+ routes: routes || [],
60
+ config
61
+ }) : null;
62
+ handler = await runner.prepareApiServer({
63
+ pwd,
64
+ prefix,
65
+ render: renderHandler,
66
+ httpMethodDecider
67
+ }, {
68
+ onLast: () => null
69
+ });
70
+ }
71
+ if (handler) {
72
+ globalMiddlewares.push({
73
+ name: "bind-bff",
74
+ handler: (c, next) => {
75
+ if (!c.req.path.startsWith(prefix) && !enableHandleWeb) {
76
+ return next();
77
+ } else {
78
+ return handler(c, next);
79
+ }
80
+ },
81
+ order: "post",
82
+ before: [
83
+ "custom-server-hook",
84
+ "custom-server-middleware",
85
+ "render"
86
+ ]
87
+ });
88
+ }
41
89
  },
42
- reset() {
90
+ reset({ event }) {
43
91
  storage.reset();
44
92
  const appContext = api.useAppContext();
45
93
  const newApiModule = requireExistModule(apiAppPath);
@@ -51,15 +99,14 @@ var server_default = () => ({
51
99
  ...appContext,
52
100
  apiMiddlewares: middlewares
53
101
  });
54
- },
55
- onApiChange(changes) {
56
- const apiHandlerInfos = apiRouter.getApiHandlers();
57
- const appContext = api.useAppContext();
58
- api.setAppContext({
59
- ...appContext,
60
- apiHandlerInfos
61
- });
62
- return changes;
102
+ if (event.type === "file-change") {
103
+ const apiHandlerInfos = apiRouter.getApiHandlers();
104
+ const appContext2 = api.useAppContext();
105
+ api.setAppContext({
106
+ ...appContext2,
107
+ apiHandlerInfos
108
+ });
109
+ }
63
110
  },
64
111
  prepareApiServer(props, next) {
65
112
  const { pwd, prefix, httpMethodDecider } = props;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.53.0",
18
+ "version": "2.54.1",
19
19
  "jsnext:source": "./src/cli.ts",
20
20
  "types": "./dist/types/cli.d.ts",
21
21
  "main": "./dist/cjs/cli.js",
@@ -57,13 +57,13 @@
57
57
  "dependencies": {
58
58
  "@babel/core": "^7.23.2",
59
59
  "@swc/helpers": "0.5.3",
60
- "@modern-js/bff-core": "2.53.0",
61
- "@modern-js/create-request": "2.53.0",
62
- "@modern-js/server-utils": "2.53.0",
63
- "@modern-js/utils": "2.53.0"
60
+ "@modern-js/bff-core": "2.54.1",
61
+ "@modern-js/create-request": "2.54.1",
62
+ "@modern-js/utils": "2.54.1",
63
+ "@modern-js/server-utils": "2.54.1"
64
64
  },
65
65
  "devDependencies": {
66
- "@rsbuild/shared": "0.7.3",
66
+ "@rsbuild/shared": "0.7.9",
67
67
  "@types/babel__core": "^7.20.1",
68
68
  "@types/jest": "^29",
69
69
  "@types/node": "^14",
@@ -71,15 +71,15 @@
71
71
  "memfs": "^3.5.1",
72
72
  "ts-jest": "^29.1.0",
73
73
  "typescript": "^5",
74
- "webpack": "^5.91.0",
75
- "@modern-js/core": "2.53.0",
76
- "@modern-js/bff-runtime": "2.53.0",
77
- "@modern-js/runtime": "2.53.0",
78
- "@modern-js/server-core": "2.53.0",
79
- "@modern-js/types": "2.53.0",
80
- "@scripts/jest-config": "2.53.0",
81
- "@scripts/build": "2.53.0",
82
- "@modern-js/app-tools": "2.53.0"
74
+ "webpack": "^5.92.0",
75
+ "@modern-js/core": "2.54.1",
76
+ "@modern-js/runtime": "2.54.1",
77
+ "@modern-js/bff-runtime": "2.54.1",
78
+ "@modern-js/server-core": "2.54.1",
79
+ "@modern-js/types": "2.54.1",
80
+ "@modern-js/app-tools": "2.54.1",
81
+ "@scripts/build": "2.54.1",
82
+ "@scripts/jest-config": "2.54.1"
83
83
  },
84
84
  "sideEffects": false,
85
85
  "publishConfig": {