@modern-js/plugin-bff 1.6.1 → 1.15.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 CHANGED
@@ -1,5 +1,35 @@
1
1
  # @modern-js/plugin-bff
2
2
 
3
+ ## 1.15.0
4
+
5
+ ### Patch Changes
6
+
7
+ - c0d8dac: fix: remove package adapter-helpers and bff runtime export
8
+ - Updated dependencies [c0d8dac]
9
+ - Updated dependencies [8658a78]
10
+ - Updated dependencies [05d4a4f]
11
+ - Updated dependencies [7bfaaf9]
12
+ - Updated dependencies [b1f7000]
13
+ - Updated dependencies [ad05af9]
14
+ - Updated dependencies [5d53d1c]
15
+ - Updated dependencies [37cd159]
16
+ - @modern-js/bff-core@1.15.0
17
+ - @modern-js/utils@1.15.0
18
+ - @modern-js/create-request@1.15.0
19
+ - @modern-js/server-utils@1.15.0
20
+ - @modern-js/babel-compiler@1.15.0
21
+
22
+ ## 1.6.2
23
+
24
+ ### Patch Changes
25
+
26
+ - a27ab8d: feat: add onApiChange hook for bff hot reload
27
+ feat: 为 BFF 热更新优化,添加 onApiChange 钩子
28
+ - Updated dependencies [a27ab8d]
29
+ - @modern-js/bff-core@1.1.2
30
+ - @modern-js/server-utils@1.2.11
31
+ - @modern-js/utils@1.7.12
32
+
3
33
  ## 1.6.1
4
34
 
5
35
  ### Patch Changes
@@ -79,6 +79,7 @@ export default (() => ({
79
79
  });
80
80
  const apiHandlerInfos = apiRouter.getApiHandlers();
81
81
  api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
82
+ apiRouter,
82
83
  apiHandlerInfos
83
84
  }));
84
85
  return next(props);
@@ -95,6 +95,7 @@ var _default = () => ({
95
95
  });
96
96
  const apiHandlerInfos = apiRouter.getApiHandlers();
97
97
  api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
98
+ apiRouter,
98
99
  apiHandlerInfos
99
100
  }));
100
101
  return next(props);
@@ -0,0 +1,251 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
+ import path from 'path';
5
+ import { compiler } from '@modern-js/babel-compiler';
6
+ import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
7
+ import { resolveBabelConfig } from '@modern-js/server-utils';
8
+ import { registerModernRuntimePath } from "./helper";
9
+ var DEFAULT_API_PREFIX = '/api';
10
+ var TS_CONFIG_FILENAME = 'tsconfig.json';
11
+ var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
12
+
13
+ var compile = /*#__PURE__*/function () {
14
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
15
+ var patterns, results;
16
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
17
+ while (1) {
18
+ switch (_context2.prev = _context2.next) {
19
+ case 0:
20
+ patterns = compileOptions.patterns;
21
+ _context2.next = 3;
22
+ return Promise.all(patterns.map( /*#__PURE__*/function () {
23
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern) {
24
+ var from, to, tsconfigPath, babelConfig, basename, targetDir;
25
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
26
+ while (1) {
27
+ switch (_context.prev = _context.next) {
28
+ case 0:
29
+ from = pattern.from, to = pattern.to, tsconfigPath = pattern.tsconfigPath;
30
+ babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
31
+ tsconfigPath: tsconfigPath ? tsconfigPath : '',
32
+ syntax: 'es6+',
33
+ type: 'commonjs'
34
+ });
35
+ _context.next = 4;
36
+ return fs.pathExists(from);
37
+
38
+ case 4:
39
+ if (!_context.sent) {
40
+ _context.next = 9;
41
+ break;
42
+ }
43
+
44
+ basename = path.basename(from);
45
+ targetDir = path.join(to, basename);
46
+ _context.next = 9;
47
+ return fs.copy(from, targetDir, {
48
+ filter: function filter(src) {
49
+ return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
50
+ }
51
+ });
52
+
53
+ case 9:
54
+ return _context.abrupt("return", compiler({
55
+ rootDir: appDirectory,
56
+ distDir: to,
57
+ sourceDir: from,
58
+ extensions: FILE_EXTENSIONS
59
+ }, babelConfig));
60
+
61
+ case 10:
62
+ case "end":
63
+ return _context.stop();
64
+ }
65
+ }
66
+ }, _callee);
67
+ }));
68
+
69
+ return function (_x4) {
70
+ return _ref2.apply(this, arguments);
71
+ };
72
+ }()));
73
+
74
+ case 3:
75
+ results = _context2.sent;
76
+ results.forEach(function (result) {
77
+ if (result.code === 1) {
78
+ throw new Error(result.message);
79
+ }
80
+ });
81
+
82
+ case 5:
83
+ case "end":
84
+ return _context2.stop();
85
+ }
86
+ }
87
+ }, _callee2);
88
+ }));
89
+
90
+ return function compile(_x, _x2, _x3) {
91
+ return _ref.apply(this, arguments);
92
+ };
93
+ }();
94
+
95
+ export default (function () {
96
+ return {
97
+ name: '@modern-js/plugin-bff',
98
+ setup: function setup(api) {
99
+ var unRegisterResolveRuntimePath = null;
100
+ return {
101
+ validateSchema: function validateSchema() {
102
+ return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
103
+ },
104
+ config: function config() {
105
+ return {
106
+ tools: {
107
+ webpackChain: function webpackChain(chain, _ref3) {
108
+ var name = _ref3.name,
109
+ CHAIN_ID = _ref3.CHAIN_ID;
110
+
111
+ var _api$useAppContext = api.useAppContext(),
112
+ appDirectory = _api$useAppContext.appDirectory,
113
+ port = _api$useAppContext.port;
114
+
115
+ var modernConfig = api.useResolvedConfigContext();
116
+
117
+ var _ref4 = modernConfig || {},
118
+ bff = _ref4.bff;
119
+
120
+ var _ref5 = bff || {},
121
+ fetcher = _ref5.fetcher;
122
+
123
+ var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
124
+ var rootDir = path.resolve(appDirectory, API_DIR);
125
+ chain.resolve.alias.set('@api', rootDir);
126
+ var apiRegexp = new RegExp(normalizeOutputPath("".concat(appDirectory).concat(path.sep, "api").concat(path.sep, ".*(.[tj]s)$")));
127
+ chain.module.rule(CHAIN_ID.RULE.LOADERS).oneOf(CHAIN_ID.ONE_OF.BFF_CLIENT).before(CHAIN_ID.ONE_OF.FALLBACK).test(apiRegexp).use('custom-loader').loader(require.resolve("./loader").replace(/\\/g, '/')).options({
128
+ prefix: prefix,
129
+ apiDir: rootDir,
130
+ port: port,
131
+ fetcher: fetcher,
132
+ target: name,
133
+ requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
134
+ });
135
+ }
136
+ },
137
+ source: {
138
+ moduleScopes: ["./".concat(API_DIR), /create-request/]
139
+ }
140
+ };
141
+ },
142
+ modifyServerRoutes: function modifyServerRoutes(_ref6) {
143
+ var routes = _ref6.routes;
144
+ var modernConfig = api.useResolvedConfigContext();
145
+
146
+ var _ref7 = modernConfig || {},
147
+ bff = _ref7.bff;
148
+
149
+ var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
150
+ var prefixList = [];
151
+
152
+ if (Array.isArray(prefix)) {
153
+ prefixList.push.apply(prefixList, _toConsumableArray(prefix));
154
+ } else {
155
+ prefixList.push(prefix);
156
+ }
157
+
158
+ var apiServerRoutes = prefixList.map(function (pre) {
159
+ return {
160
+ urlPath: pre,
161
+ isApi: true,
162
+ entryPath: '',
163
+ isSPA: false,
164
+ isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
165
+
166
+ };
167
+ });
168
+ return {
169
+ routes: routes.concat(apiServerRoutes)
170
+ };
171
+ },
172
+ beforeBuild: function beforeBuild() {
173
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
174
+ var _api$useAppContext2, internalDirectory;
175
+
176
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
177
+ while (1) {
178
+ switch (_context3.prev = _context3.next) {
179
+ case 0:
180
+ // help esbuild-register resolve @modern-js/server/runtime
181
+ if (isProd()) {
182
+ _api$useAppContext2 = api.useAppContext(), internalDirectory = _api$useAppContext2.internalDirectory;
183
+ unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
184
+ }
185
+
186
+ case 1:
187
+ case "end":
188
+ return _context3.stop();
189
+ }
190
+ }
191
+ }, _callee3);
192
+ }))();
193
+ },
194
+ afterBuild: function afterBuild() {
195
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
196
+ var _api$useAppContext3, appDirectory, distDirectory, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, patterns;
197
+
198
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
199
+ while (1) {
200
+ switch (_context4.prev = _context4.next) {
201
+ case 0:
202
+ if (unRegisterResolveRuntimePath) {
203
+ unRegisterResolveRuntimePath();
204
+ }
205
+
206
+ _api$useAppContext3 = api.useAppContext(), appDirectory = _api$useAppContext3.appDirectory, distDirectory = _api$useAppContext3.distDirectory;
207
+ modernConfig = api.useResolvedConfigContext();
208
+ distDir = path.resolve(distDirectory);
209
+ apiDir = path.resolve(appDirectory, API_DIR);
210
+ sharedDir = path.resolve(appDirectory, SHARED_DIR);
211
+ tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
212
+ patterns = [];
213
+
214
+ if (fs.existsSync(apiDir)) {
215
+ patterns.push({
216
+ from: apiDir,
217
+ to: distDir,
218
+ tsconfigPath: tsconfigPath
219
+ });
220
+ }
221
+
222
+ if (fs.existsSync(sharedDir)) {
223
+ patterns.push({
224
+ from: sharedDir,
225
+ to: distDir,
226
+ tsconfigPath: tsconfigPath
227
+ });
228
+ }
229
+
230
+ if (!(patterns.length > 0)) {
231
+ _context4.next = 13;
232
+ break;
233
+ }
234
+
235
+ _context4.next = 13;
236
+ return compile(appDirectory, modernConfig, {
237
+ patterns: patterns
238
+ });
239
+
240
+ case 13:
241
+ case "end":
242
+ return _context4.stop();
243
+ }
244
+ }
245
+ }, _callee4);
246
+ }))();
247
+ }
248
+ };
249
+ }
250
+ };
251
+ });
@@ -0,0 +1,2 @@
1
+ export var API_APP_NAME = '_app';
2
+ export var BUILD_FILES = ['**/*.[tj]sx?', '!**/*.test.jsx?', '!**/*.test.tsx?', '!**/*.spec.jsx?', '!**/*.spec.tsx?', '!__tests__/*.tsx?', '!__tests__/*.jsx?'];
@@ -0,0 +1,14 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ import * as path from 'path';
3
+ import { registerPaths } from '@modern-js/bff-core';
4
+ var serverRuntimeAlias = '@modern-js/runtime/server';
5
+ var serverRuntimePath = '.runtime-exports/server';
6
+
7
+ var registerModernRuntimePath = function registerModernRuntimePath(internalDirectory) {
8
+ var paths = _defineProperty({}, serverRuntimeAlias, path.join(internalDirectory, serverRuntimePath));
9
+
10
+ var unRegister = registerPaths(paths);
11
+ return unRegister;
12
+ };
13
+
14
+ export { registerModernRuntimePath };
@@ -0,0 +1 @@
1
+ export * from "./constants";
@@ -0,0 +1,63 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import { generateClient } from '@modern-js/bff-core';
4
+
5
+ function loader(_x) {
6
+ return _loader.apply(this, arguments);
7
+ }
8
+
9
+ function _loader() {
10
+ _loader = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(source) {
11
+ var callback, draftOptions, options, result;
12
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
13
+ while (1) {
14
+ switch (_context.prev = _context.next) {
15
+ case 0:
16
+ // eslint-disable-next-line @babel/no-invalid-this
17
+ this.cacheable(); // eslint-disable-next-line @babel/no-invalid-this
18
+
19
+ callback = this.async(); // eslint-disable-next-line @babel/no-invalid-this
20
+
21
+ draftOptions = this.getOptions();
22
+ options = {
23
+ prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
24
+ apiDir: draftOptions.apiDir,
25
+ target: draftOptions.target,
26
+ port: Number(draftOptions.port),
27
+ source: source,
28
+ // eslint-disable-next-line @babel/no-invalid-this
29
+ resourcePath: this.resourcePath
30
+ };
31
+
32
+ if (draftOptions.fetcher) {
33
+ options.fetcher = draftOptions.fetcher;
34
+ }
35
+
36
+ if (draftOptions.requestCreator) {
37
+ options.requestCreator = draftOptions.requestCreator;
38
+ }
39
+
40
+ options.requireResolve = require.resolve;
41
+ _context.next = 9;
42
+ return generateClient(options);
43
+
44
+ case 9:
45
+ result = _context.sent;
46
+
47
+ if (result.isOk) {
48
+ callback(undefined, result.value);
49
+ } else {
50
+ callback(undefined, "throw new Error('".concat(result.value, "')"));
51
+ }
52
+
53
+ case 11:
54
+ case "end":
55
+ return _context.stop();
56
+ }
57
+ }
58
+ }, _callee, this);
59
+ }));
60
+ return _loader.apply(this, arguments);
61
+ }
62
+
63
+ export default loader;
@@ -0,0 +1,90 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
4
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
+ import path from 'path';
6
+ import { ApiRouter } from '@modern-js/bff-core';
7
+ import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
8
+ import { API_APP_NAME } from "./constants";
9
+
10
+ var Storage = /*#__PURE__*/function () {
11
+ function Storage() {
12
+ _classCallCheck(this, Storage);
13
+
14
+ _defineProperty(this, "middlewares", []);
15
+ }
16
+
17
+ _createClass(Storage, [{
18
+ key: "reset",
19
+ value: function reset() {
20
+ this.middlewares = [];
21
+ }
22
+ }]);
23
+
24
+ return Storage;
25
+ }();
26
+
27
+ var createTransformAPI = function createTransformAPI(storage) {
28
+ return {
29
+ addMiddleware: function addMiddleware(fn) {
30
+ storage.middlewares.push(fn);
31
+ }
32
+ };
33
+ };
34
+
35
+ export default (function () {
36
+ return {
37
+ name: '@modern-js/plugin-bff',
38
+ setup: function setup(api) {
39
+ var storage = new Storage();
40
+ var transformAPI = createTransformAPI(storage);
41
+ var apiAppPath = '';
42
+ return {
43
+ prepare: function prepare() {
44
+ var _api$useAppContext = api.useAppContext(),
45
+ appDirectory = _api$useAppContext.appDirectory,
46
+ distDirectory = _api$useAppContext.distDirectory;
47
+
48
+ var root = isProd() ? distDirectory : appDirectory;
49
+ var apiPath = path.resolve(root || process.cwd(), API_DIR);
50
+ apiAppPath = path.resolve(apiPath, API_APP_NAME);
51
+ var apiMod = requireExistModule(apiAppPath);
52
+
53
+ if (apiMod && typeof apiMod === 'function') {
54
+ apiMod(transformAPI);
55
+ }
56
+ },
57
+ reset: function reset() {
58
+ storage.reset();
59
+ var newApiModule = requireExistModule(apiAppPath);
60
+
61
+ if (newApiModule && typeof newApiModule === 'function') {
62
+ newApiModule(transformAPI);
63
+ }
64
+ },
65
+ gather: function gather(_ref) {
66
+ var addAPIMiddleware = _ref.addAPIMiddleware;
67
+ storage.middlewares.forEach(function (mid) {
68
+ addAPIMiddleware(mid);
69
+ });
70
+ },
71
+ prepareApiServer: function prepareApiServer(props, next) {
72
+ var pwd = props.pwd,
73
+ prefix = props.prefix;
74
+ var apiDir = path.resolve(pwd, API_DIR);
75
+ var appContext = api.useAppContext();
76
+ var apiRouter = new ApiRouter({
77
+ apiDir: apiDir,
78
+ prefix: prefix
79
+ });
80
+ var apiHandlerInfos = apiRouter.getApiHandlers();
81
+ api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
82
+ apiRouter: apiRouter,
83
+ apiHandlerInfos: apiHandlerInfos
84
+ }));
85
+ return next(props);
86
+ }
87
+ };
88
+ }
89
+ };
90
+ });
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.6.1",
14
+ "version": "1.15.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -55,20 +55,20 @@
55
55
  "dependencies": {
56
56
  "@babel/core": "^7.18.0",
57
57
  "@babel/runtime": "^7.18.0",
58
- "@modern-js/babel-compiler": "^1.2.6",
59
- "@modern-js/bff-core": "^1.1.1",
60
- "@modern-js/create-request": "^1.3.0",
61
- "@modern-js/server-utils": "^1.2.11",
62
- "@modern-js/utils": "^1.7.12"
58
+ "@modern-js/babel-compiler": "1.15.0",
59
+ "@modern-js/bff-core": "1.15.0",
60
+ "@modern-js/create-request": "1.15.0",
61
+ "@modern-js/server-utils": "1.15.0",
62
+ "@modern-js/utils": "1.15.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@modern-js/core": "1.12.4",
66
- "@modern-js/plugin-analyze": "1.4.7",
67
- "@modern-js/runtime": "1.3.4",
68
- "@modern-js/server-core": "1.4.0",
69
- "@modern-js/types": "1.5.6",
70
- "@scripts/build": "0.0.0",
71
- "@scripts/jest-config": "0.0.0",
65
+ "@modern-js/core": "1.15.0",
66
+ "@modern-js/bff-runtime": "1.15.0",
67
+ "@modern-js/runtime": "1.15.0",
68
+ "@modern-js/server-core": "1.15.0",
69
+ "@modern-js/types": "1.15.0",
70
+ "@scripts/build": "1.15.0",
71
+ "@scripts/jest-config": "1.15.0",
72
72
  "@types/babel__core": "^7.1.15",
73
73
  "@types/jest": "^27",
74
74
  "@types/node": "^14",