@modern-js/plugin-bff 2.57.1-alpha.0 → 2.58.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 CHANGED
@@ -36,11 +36,13 @@ var import_path = __toESM(require("path"));
36
36
  var import_utils = require("@modern-js/utils");
37
37
  var import_server_utils = require("@modern-js/server-utils");
38
38
  var import_bff_core = require("@modern-js/bff-core");
39
+ var import_helper = require("./helper");
39
40
  const DEFAULT_API_PREFIX = "/api";
40
41
  const TS_CONFIG_FILENAME = "tsconfig.json";
41
42
  const bffPlugin = () => ({
42
43
  name: "@modern-js/plugin-bff",
43
44
  setup: (api) => {
45
+ let unRegisterResolveRuntimePath = null;
44
46
  return {
45
47
  config() {
46
48
  return {
@@ -127,18 +129,27 @@ const bffPlugin = () => ({
127
129
  plugins
128
130
  };
129
131
  },
132
+ async beforeBuild() {
133
+ if ((0, import_utils.isProd)()) {
134
+ const { internalDirectory } = api.useAppContext();
135
+ unRegisterResolveRuntimePath = (0, import_helper.registerModernRuntimePath)(internalDirectory);
136
+ }
137
+ },
130
138
  async afterBuild() {
131
- const { appDirectory, distDirectory, apiDirectory, sharedDirectory, moduleType } = api.useAppContext();
139
+ if (unRegisterResolveRuntimePath) {
140
+ unRegisterResolveRuntimePath();
141
+ }
142
+ const { appDirectory, distDirectory, apiDirectory, sharedDirectory } = api.useAppContext();
132
143
  const modernConfig = api.useResolvedConfigContext();
133
144
  const distDir = import_path.default.resolve(distDirectory);
134
145
  const apiDir = apiDirectory || import_path.default.resolve(appDirectory, import_utils.API_DIR);
135
146
  const sharedDir = sharedDirectory || import_path.default.resolve(appDirectory, import_utils.SHARED_DIR);
136
147
  const tsconfigPath = import_path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
137
148
  const sourceDirs = [];
138
- if (await import_utils.fs.pathExists(apiDir)) {
149
+ if (import_utils.fs.existsSync(apiDir)) {
139
150
  sourceDirs.push(apiDir);
140
151
  }
141
- if (await import_utils.fs.pathExists(sharedDir)) {
152
+ if (import_utils.fs.existsSync(sharedDir)) {
142
153
  sourceDirs.push(sharedDir);
143
154
  }
144
155
  const { server } = modernConfig;
@@ -152,8 +163,7 @@ const bffPlugin = () => ({
152
163
  }, {
153
164
  sourceDirs,
154
165
  distDir,
155
- tsconfigPath,
156
- moduleType
166
+ tsconfigPath
157
167
  });
158
168
  }
159
169
  }
@@ -63,7 +63,7 @@ var server_default = () => ({
63
63
  const root = (0, import_utils.isProd)() ? distDirectory : appDirectory;
64
64
  const apiPath = import_path.default.resolve(root || process.cwd(), import_utils.API_DIR);
65
65
  apiAppPath = import_path.default.resolve(apiPath, import_constants.API_APP_NAME);
66
- const apiMod = await (0, import_utils.requireExistModule)(apiAppPath);
66
+ const apiMod = (0, import_utils.requireExistModule)(apiAppPath);
67
67
  if (apiMod && typeof apiMod === "function") {
68
68
  apiMod(transformAPI);
69
69
  }
@@ -115,10 +115,10 @@ var server_default = () => ({
115
115
  });
116
116
  }
117
117
  },
118
- async reset({ event }) {
118
+ reset({ event }) {
119
119
  storage.reset();
120
120
  const appContext = api.useAppContext();
121
- const newApiModule = await (0, import_utils.requireExistModule)(apiAppPath);
121
+ const newApiModule = (0, import_utils.requireExistModule)(apiAppPath);
122
122
  if (newApiModule && typeof newApiModule === "function") {
123
123
  newApiModule(transformAPI);
124
124
  }
@@ -128,7 +128,7 @@ var server_default = () => ({
128
128
  apiMiddlewares: middlewares
129
129
  });
130
130
  if (event.type === "file-change") {
131
- const apiHandlerInfos = await apiRouter.getApiHandlers();
131
+ const apiHandlerInfos = apiRouter.getApiHandlers();
132
132
  const appContext2 = api.useAppContext();
133
133
  api.setAppContext({
134
134
  ...appContext2,
@@ -136,7 +136,7 @@ var server_default = () => ({
136
136
  });
137
137
  }
138
138
  },
139
- async prepareApiServer(props, next) {
139
+ prepareApiServer(props, next) {
140
140
  const { pwd, prefix, httpMethodDecider } = props;
141
141
  const apiDir = import_path.default.resolve(pwd, import_utils.API_DIR);
142
142
  const appContext = api.useAppContext();
@@ -149,7 +149,7 @@ var server_default = () => ({
149
149
  httpMethodDecider
150
150
  });
151
151
  const apiMode = apiRouter.getApiMode();
152
- const apiHandlerInfos = await apiRouter.getApiHandlers();
152
+ const apiHandlerInfos = apiRouter.getApiHandlers();
153
153
  api.setAppContext({
154
154
  ...appContext,
155
155
  apiRouter,
package/dist/esm/cli.js CHANGED
@@ -4,15 +4,17 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
4
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
5
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
6
  import path from "path";
7
- import { fs, API_DIR, normalizeOutputPath, SHARED_DIR } from "@modern-js/utils";
7
+ import { fs, API_DIR, normalizeOutputPath, SHARED_DIR, isProd } from "@modern-js/utils";
8
8
  import { compile } from "@modern-js/server-utils";
9
9
  import { ApiRouter } from "@modern-js/bff-core";
10
+ import { registerModernRuntimePath } from "./helper";
10
11
  var DEFAULT_API_PREFIX = "/api";
11
12
  var TS_CONFIG_FILENAME = "tsconfig.json";
12
13
  var bffPlugin = function() {
13
14
  return {
14
15
  name: "@modern-js/plugin-bff",
15
16
  setup: function(api) {
17
+ var unRegisterResolveRuntimePath = null;
16
18
  return {
17
19
  config: function config() {
18
20
  return {
@@ -104,33 +106,40 @@ var bffPlugin = function() {
104
106
  plugins
105
107
  };
106
108
  },
109
+ beforeBuild: function beforeBuild() {
110
+ return _async_to_generator(function() {
111
+ var internalDirectory;
112
+ return _ts_generator(this, function(_state) {
113
+ if (isProd()) {
114
+ internalDirectory = api.useAppContext().internalDirectory;
115
+ unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
116
+ }
117
+ return [
118
+ 2
119
+ ];
120
+ });
121
+ })();
122
+ },
107
123
  afterBuild: function afterBuild() {
108
124
  return _async_to_generator(function() {
109
- var _api_useAppContext, appDirectory, distDirectory, apiDirectory, sharedDirectory, moduleType, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, sourceDirs, server, alias, babel;
125
+ var _api_useAppContext, appDirectory, distDirectory, apiDirectory, sharedDirectory, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, sourceDirs, server, alias, babel;
110
126
  return _ts_generator(this, function(_state) {
111
127
  switch (_state.label) {
112
128
  case 0:
113
- _api_useAppContext = api.useAppContext(), appDirectory = _api_useAppContext.appDirectory, distDirectory = _api_useAppContext.distDirectory, apiDirectory = _api_useAppContext.apiDirectory, sharedDirectory = _api_useAppContext.sharedDirectory, moduleType = _api_useAppContext.moduleType;
129
+ if (unRegisterResolveRuntimePath) {
130
+ unRegisterResolveRuntimePath();
131
+ }
132
+ _api_useAppContext = api.useAppContext(), appDirectory = _api_useAppContext.appDirectory, distDirectory = _api_useAppContext.distDirectory, apiDirectory = _api_useAppContext.apiDirectory, sharedDirectory = _api_useAppContext.sharedDirectory;
114
133
  modernConfig = api.useResolvedConfigContext();
115
134
  distDir = path.resolve(distDirectory);
116
135
  apiDir = apiDirectory || path.resolve(appDirectory, API_DIR);
117
136
  sharedDir = sharedDirectory || path.resolve(appDirectory, SHARED_DIR);
118
137
  tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
119
138
  sourceDirs = [];
120
- return [
121
- 4,
122
- fs.pathExists(apiDir)
123
- ];
124
- case 1:
125
- if (_state.sent()) {
139
+ if (fs.existsSync(apiDir)) {
126
140
  sourceDirs.push(apiDir);
127
141
  }
128
- return [
129
- 4,
130
- fs.pathExists(sharedDir)
131
- ];
132
- case 2:
133
- if (_state.sent()) {
142
+ if (fs.existsSync(sharedDir)) {
134
143
  sourceDirs.push(sharedDir);
135
144
  }
136
145
  server = modernConfig.server;
@@ -139,7 +148,7 @@ var bffPlugin = function() {
139
148
  if (!(sourceDirs.length > 0))
140
149
  return [
141
150
  3,
142
- 4
151
+ 2
143
152
  ];
144
153
  return [
145
154
  4,
@@ -150,14 +159,13 @@ var bffPlugin = function() {
150
159
  }, {
151
160
  sourceDirs,
152
161
  distDir,
153
- tsconfigPath,
154
- moduleType
162
+ tsconfigPath
155
163
  })
156
164
  ];
157
- case 3:
165
+ case 1:
158
166
  _state.sent();
159
- _state.label = 4;
160
- case 4:
167
+ _state.label = 2;
168
+ case 2:
161
169
  return [
162
170
  2
163
171
  ];
@@ -46,12 +46,7 @@ function server_default() {
46
46
  root = isProd() ? distDirectory : appDirectory;
47
47
  apiPath = path.resolve(root || process.cwd(), API_DIR);
48
48
  apiAppPath = path.resolve(apiPath, API_APP_NAME);
49
- return [
50
- 4,
51
- requireExistModule(apiAppPath)
52
- ];
53
- case 1:
54
- apiMod = _state.sent();
49
+ apiMod = requireExistModule(apiAppPath);
55
50
  if (apiMod && typeof apiMod === "function") {
56
51
  apiMod(transformAPI);
57
52
  }
@@ -68,12 +63,12 @@ function server_default() {
68
63
  4,
69
64
  isWebOnly()
70
65
  ];
71
- case 2:
66
+ case 1:
72
67
  webOnly = _state.sent();
73
68
  if (!webOnly)
74
69
  return [
75
70
  3,
76
- 3
71
+ 2
77
72
  ];
78
73
  handler = function() {
79
74
  var _ref = _async_to_generator(function(c, next) {
@@ -99,9 +94,9 @@ function server_default() {
99
94
  }();
100
95
  return [
101
96
  3,
102
- 5
97
+ 4
103
98
  ];
104
- case 3:
99
+ case 2:
105
100
  runner = api.useHookRunners();
106
101
  renderHandler = enableHandleWeb ? render : null;
107
102
  return [
@@ -117,10 +112,10 @@ function server_default() {
117
112
  }
118
113
  })
119
114
  ];
120
- case 4:
115
+ case 3:
121
116
  handler = _state.sent();
122
- _state.label = 5;
123
- case 5:
117
+ _state.label = 4;
118
+ case 4:
124
119
  if (handler) {
125
120
  globalMiddlewares.push({
126
121
  name: "bind-bff",
@@ -148,86 +143,44 @@ function server_default() {
148
143
  },
149
144
  reset: function reset(param) {
150
145
  var event = param.event;
151
- return _async_to_generator(function() {
152
- var appContext, newApiModule, middlewares, apiHandlerInfos, appContext1;
153
- return _ts_generator(this, function(_state) {
154
- switch (_state.label) {
155
- case 0:
156
- storage.reset();
157
- appContext = api.useAppContext();
158
- return [
159
- 4,
160
- requireExistModule(apiAppPath)
161
- ];
162
- case 1:
163
- newApiModule = _state.sent();
164
- if (newApiModule && typeof newApiModule === "function") {
165
- newApiModule(transformAPI);
166
- }
167
- middlewares = storage.middlewares;
168
- api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
169
- apiMiddlewares: middlewares
170
- }));
171
- if (!(event.type === "file-change"))
172
- return [
173
- 3,
174
- 3
175
- ];
176
- return [
177
- 4,
178
- apiRouter.getApiHandlers()
179
- ];
180
- case 2:
181
- apiHandlerInfos = _state.sent();
182
- appContext1 = api.useAppContext();
183
- api.setAppContext(_object_spread_props(_object_spread({}, appContext1), {
184
- apiHandlerInfos
185
- }));
186
- _state.label = 3;
187
- case 3:
188
- return [
189
- 2
190
- ];
191
- }
192
- });
193
- })();
146
+ storage.reset();
147
+ var appContext = api.useAppContext();
148
+ var newApiModule = requireExistModule(apiAppPath);
149
+ if (newApiModule && typeof newApiModule === "function") {
150
+ newApiModule(transformAPI);
151
+ }
152
+ var middlewares = storage.middlewares;
153
+ api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
154
+ apiMiddlewares: middlewares
155
+ }));
156
+ if (event.type === "file-change") {
157
+ var apiHandlerInfos = apiRouter.getApiHandlers();
158
+ var appContext1 = api.useAppContext();
159
+ api.setAppContext(_object_spread_props(_object_spread({}, appContext1), {
160
+ apiHandlerInfos
161
+ }));
162
+ }
194
163
  },
195
164
  prepareApiServer: function prepareApiServer(props, next) {
196
- return _async_to_generator(function() {
197
- var pwd, prefix, httpMethodDecider, apiDir, appContext, apiDirectory, lambdaDirectory, apiMode, apiHandlerInfos;
198
- return _ts_generator(this, function(_state) {
199
- switch (_state.label) {
200
- case 0:
201
- pwd = props.pwd, prefix = props.prefix, httpMethodDecider = props.httpMethodDecider;
202
- apiDir = path.resolve(pwd, API_DIR);
203
- appContext = api.useAppContext();
204
- apiDirectory = appContext.apiDirectory, lambdaDirectory = appContext.lambdaDirectory;
205
- apiRouter = new ApiRouter({
206
- appDir: pwd,
207
- apiDir: apiDirectory || apiDir,
208
- lambdaDir: lambdaDirectory,
209
- prefix,
210
- httpMethodDecider
211
- });
212
- apiMode = apiRouter.getApiMode();
213
- return [
214
- 4,
215
- apiRouter.getApiHandlers()
216
- ];
217
- case 1:
218
- apiHandlerInfos = _state.sent();
219
- api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
220
- apiRouter,
221
- apiHandlerInfos,
222
- apiMode
223
- }));
224
- return [
225
- 2,
226
- next(props)
227
- ];
228
- }
229
- });
230
- })();
165
+ var pwd = props.pwd, prefix = props.prefix, httpMethodDecider = props.httpMethodDecider;
166
+ var apiDir = path.resolve(pwd, API_DIR);
167
+ var appContext = api.useAppContext();
168
+ var apiDirectory = appContext.apiDirectory, lambdaDirectory = appContext.lambdaDirectory;
169
+ apiRouter = new ApiRouter({
170
+ appDir: pwd,
171
+ apiDir: apiDirectory || apiDir,
172
+ lambdaDir: lambdaDirectory,
173
+ prefix,
174
+ httpMethodDecider
175
+ });
176
+ var apiMode = apiRouter.getApiMode();
177
+ var apiHandlerInfos = apiRouter.getApiHandlers();
178
+ api.setAppContext(_object_spread_props(_object_spread({}, appContext), {
179
+ apiRouter,
180
+ apiHandlerInfos,
181
+ apiMode
182
+ }));
183
+ return next(props);
231
184
  }
232
185
  };
233
186
  }
@@ -1,12 +1,14 @@
1
1
  import path from "path";
2
- import { fs, API_DIR, normalizeOutputPath, SHARED_DIR } from "@modern-js/utils";
2
+ import { fs, API_DIR, normalizeOutputPath, SHARED_DIR, isProd } from "@modern-js/utils";
3
3
  import { compile } from "@modern-js/server-utils";
4
4
  import { ApiRouter } from "@modern-js/bff-core";
5
+ import { registerModernRuntimePath } from "./helper";
5
6
  const DEFAULT_API_PREFIX = "/api";
6
7
  const TS_CONFIG_FILENAME = "tsconfig.json";
7
8
  const bffPlugin = () => ({
8
9
  name: "@modern-js/plugin-bff",
9
10
  setup: (api) => {
11
+ let unRegisterResolveRuntimePath = null;
10
12
  return {
11
13
  config() {
12
14
  return {
@@ -93,18 +95,27 @@ const bffPlugin = () => ({
93
95
  plugins
94
96
  };
95
97
  },
98
+ async beforeBuild() {
99
+ if (isProd()) {
100
+ const { internalDirectory } = api.useAppContext();
101
+ unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
102
+ }
103
+ },
96
104
  async afterBuild() {
97
- const { appDirectory, distDirectory, apiDirectory, sharedDirectory, moduleType } = api.useAppContext();
105
+ if (unRegisterResolveRuntimePath) {
106
+ unRegisterResolveRuntimePath();
107
+ }
108
+ const { appDirectory, distDirectory, apiDirectory, sharedDirectory } = api.useAppContext();
98
109
  const modernConfig = api.useResolvedConfigContext();
99
110
  const distDir = path.resolve(distDirectory);
100
111
  const apiDir = apiDirectory || path.resolve(appDirectory, API_DIR);
101
112
  const sharedDir = sharedDirectory || path.resolve(appDirectory, SHARED_DIR);
102
113
  const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
103
114
  const sourceDirs = [];
104
- if (await fs.pathExists(apiDir)) {
115
+ if (fs.existsSync(apiDir)) {
105
116
  sourceDirs.push(apiDir);
106
117
  }
107
- if (await fs.pathExists(sharedDir)) {
118
+ if (fs.existsSync(sharedDir)) {
108
119
  sourceDirs.push(sharedDir);
109
120
  }
110
121
  const { server } = modernConfig;
@@ -118,8 +129,7 @@ const bffPlugin = () => ({
118
129
  }, {
119
130
  sourceDirs,
120
131
  distDir,
121
- tsconfigPath,
122
- moduleType
132
+ tsconfigPath
123
133
  });
124
134
  }
125
135
  }
@@ -30,7 +30,7 @@ var server_default = () => ({
30
30
  const root = isProd() ? distDirectory : appDirectory;
31
31
  const apiPath = path.resolve(root || process.cwd(), API_DIR);
32
32
  apiAppPath = path.resolve(apiPath, API_APP_NAME);
33
- const apiMod = await requireExistModule(apiAppPath);
33
+ const apiMod = requireExistModule(apiAppPath);
34
34
  if (apiMod && typeof apiMod === "function") {
35
35
  apiMod(transformAPI);
36
36
  }
@@ -82,10 +82,10 @@ var server_default = () => ({
82
82
  });
83
83
  }
84
84
  },
85
- async reset({ event }) {
85
+ reset({ event }) {
86
86
  storage.reset();
87
87
  const appContext = api.useAppContext();
88
- const newApiModule = await requireExistModule(apiAppPath);
88
+ const newApiModule = requireExistModule(apiAppPath);
89
89
  if (newApiModule && typeof newApiModule === "function") {
90
90
  newApiModule(transformAPI);
91
91
  }
@@ -95,7 +95,7 @@ var server_default = () => ({
95
95
  apiMiddlewares: middlewares
96
96
  });
97
97
  if (event.type === "file-change") {
98
- const apiHandlerInfos = await apiRouter.getApiHandlers();
98
+ const apiHandlerInfos = apiRouter.getApiHandlers();
99
99
  const appContext2 = api.useAppContext();
100
100
  api.setAppContext({
101
101
  ...appContext2,
@@ -103,7 +103,7 @@ var server_default = () => ({
103
103
  });
104
104
  }
105
105
  },
106
- async prepareApiServer(props, next) {
106
+ prepareApiServer(props, next) {
107
107
  const { pwd, prefix, httpMethodDecider } = props;
108
108
  const apiDir = path.resolve(pwd, API_DIR);
109
109
  const appContext = api.useAppContext();
@@ -116,7 +116,7 @@ var server_default = () => ({
116
116
  httpMethodDecider
117
117
  });
118
118
  const apiMode = apiRouter.getApiMode();
119
- const apiHandlerInfos = await apiRouter.getApiHandlers();
119
+ const apiHandlerInfos = apiRouter.getApiHandlers();
120
120
  api.setAppContext({
121
121
  ...appContext,
122
122
  apiRouter,
package/package.json CHANGED
@@ -15,13 +15,12 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.57.1-alpha.0",
18
+ "version": "2.58.0",
19
19
  "jsnext:source": "./src/cli.ts",
20
20
  "types": "./dist/types/cli.d.ts",
21
21
  "main": "./dist/cjs/cli.js",
22
22
  "module": "./dist/esm/cli.js",
23
23
  "exports": {
24
- "./types": "./types.d.ts",
25
24
  ".": {
26
25
  "types": "./dist/types/cli.d.ts",
27
26
  "jsnext:source": "./src/cli.ts",
@@ -59,11 +58,11 @@
59
58
  "dependencies": {
60
59
  "@babel/core": "^7.24.7",
61
60
  "@swc/helpers": "0.5.3",
62
- "@modern-js/bff-core": "2.57.0",
63
- "@modern-js/server-utils": "2.57.0",
64
- "@modern-js/utils": "2.57.0",
65
- "@modern-js/server-core": "2.57.0",
66
- "@modern-js/create-request": "2.57.0"
61
+ "@modern-js/bff-core": "2.58.0",
62
+ "@modern-js/create-request": "2.58.0",
63
+ "@modern-js/server-utils": "2.58.0",
64
+ "@modern-js/utils": "2.58.0",
65
+ "@modern-js/server-core": "2.58.0"
67
66
  },
68
67
  "devDependencies": {
69
68
  "@types/babel__core": "^7.20.5",
@@ -74,13 +73,13 @@
74
73
  "ts-jest": "^29.1.0",
75
74
  "typescript": "^5",
76
75
  "webpack": "^5.93.0",
77
- "@modern-js/bff-runtime": "2.57.0",
78
- "@modern-js/types": "2.57.0",
79
- "@modern-js/core": "2.57.0",
80
- "@modern-js/app-tools": "2.57.1-alpha.3",
81
- "@scripts/build": "2.57.0",
82
- "@modern-js/runtime": "2.57.0",
83
- "@scripts/jest-config": "2.57.0"
76
+ "@modern-js/runtime": "2.58.0",
77
+ "@modern-js/core": "2.58.0",
78
+ "@modern-js/bff-runtime": "2.58.0",
79
+ "@modern-js/types": "2.58.0",
80
+ "@modern-js/app-tools": "2.58.0",
81
+ "@scripts/build": "2.58.0",
82
+ "@scripts/jest-config": "2.58.0"
84
83
  },
85
84
  "sideEffects": false,
86
85
  "publishConfig": {