@modern-js/plugin-bff 1.6.2-beta.0 → 1.15.1-beta.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 +30 -0
- package/dist/js/modern/cli.js +40 -41
- package/dist/js/node/cli.js +40 -44
- package/dist/js/treeshaking/cli.js +206 -0
- package/dist/js/treeshaking/constants.js +2 -0
- package/dist/js/treeshaking/helper.js +14 -0
- package/dist/js/treeshaking/index.js +1 -0
- package/dist/js/treeshaking/loader.js +63 -0
- package/dist/js/treeshaking/server.js +90 -0
- package/package.json +13 -13
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
|
package/dist/js/modern/cli.js
CHANGED
|
@@ -1,49 +1,48 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { compiler } from '@modern-js/babel-compiler';
|
|
3
2
|
import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
|
|
4
|
-
import {
|
|
3
|
+
import { compile } from '@modern-js/server-utils';
|
|
5
4
|
import { registerModernRuntimePath } from "./helper";
|
|
6
5
|
const DEFAULT_API_PREFIX = '/api';
|
|
7
6
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
8
|
-
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; //
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
};
|
|
7
|
+
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // const compile = async (
|
|
8
|
+
// appDirectory: string,
|
|
9
|
+
// modernConfig: NormalizedConfig,
|
|
10
|
+
// compileOptions: CompileOptions,
|
|
11
|
+
// ) => {
|
|
12
|
+
// const { patterns } = compileOptions;
|
|
13
|
+
// const results = await Promise.all(
|
|
14
|
+
// patterns.map(async pattern => {
|
|
15
|
+
// const { from, to, tsconfigPath } = pattern;
|
|
16
|
+
// const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
17
|
+
// tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
18
|
+
// syntax: 'es6+',
|
|
19
|
+
// type: 'commonjs',
|
|
20
|
+
// });
|
|
21
|
+
// if (await fs.pathExists(from)) {
|
|
22
|
+
// const basename = path.basename(from);
|
|
23
|
+
// const targetDir = path.join(to, basename);
|
|
24
|
+
// await fs.copy(from, targetDir, {
|
|
25
|
+
// filter: src =>
|
|
26
|
+
// !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath,
|
|
27
|
+
// });
|
|
28
|
+
// }
|
|
29
|
+
// return compiler(
|
|
30
|
+
// {
|
|
31
|
+
// rootDir: appDirectory,
|
|
32
|
+
// distDir: to,
|
|
33
|
+
// sourceDir: from,
|
|
34
|
+
// extensions: FILE_EXTENSIONS,
|
|
35
|
+
// },
|
|
36
|
+
// babelConfig,
|
|
37
|
+
// );
|
|
38
|
+
// }),
|
|
39
|
+
// );
|
|
40
|
+
// results.forEach(result => {
|
|
41
|
+
// if (result.code === 1) {
|
|
42
|
+
// throw new Error(result.message);
|
|
43
|
+
// }
|
|
44
|
+
// });
|
|
45
|
+
// };
|
|
47
46
|
|
|
48
47
|
export default (() => ({
|
|
49
48
|
name: '@modern-js/plugin-bff',
|
package/dist/js/node/cli.js
CHANGED
|
@@ -7,8 +7,6 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _path = _interopRequireDefault(require("path"));
|
|
9
9
|
|
|
10
|
-
var _babelCompiler = require("@modern-js/babel-compiler");
|
|
11
|
-
|
|
12
10
|
var _utils = require("@modern-js/utils");
|
|
13
11
|
|
|
14
12
|
var _serverUtils = require("@modern-js/server-utils");
|
|
@@ -19,47 +17,45 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
19
17
|
|
|
20
18
|
const DEFAULT_API_PREFIX = '/api';
|
|
21
19
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
22
|
-
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; //
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
});
|
|
62
|
-
};
|
|
20
|
+
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // const compile = async (
|
|
21
|
+
// appDirectory: string,
|
|
22
|
+
// modernConfig: NormalizedConfig,
|
|
23
|
+
// compileOptions: CompileOptions,
|
|
24
|
+
// ) => {
|
|
25
|
+
// const { patterns } = compileOptions;
|
|
26
|
+
// const results = await Promise.all(
|
|
27
|
+
// patterns.map(async pattern => {
|
|
28
|
+
// const { from, to, tsconfigPath } = pattern;
|
|
29
|
+
// const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
30
|
+
// tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
31
|
+
// syntax: 'es6+',
|
|
32
|
+
// type: 'commonjs',
|
|
33
|
+
// });
|
|
34
|
+
// if (await fs.pathExists(from)) {
|
|
35
|
+
// const basename = path.basename(from);
|
|
36
|
+
// const targetDir = path.join(to, basename);
|
|
37
|
+
// await fs.copy(from, targetDir, {
|
|
38
|
+
// filter: src =>
|
|
39
|
+
// !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath,
|
|
40
|
+
// });
|
|
41
|
+
// }
|
|
42
|
+
// return compiler(
|
|
43
|
+
// {
|
|
44
|
+
// rootDir: appDirectory,
|
|
45
|
+
// distDir: to,
|
|
46
|
+
// sourceDir: from,
|
|
47
|
+
// extensions: FILE_EXTENSIONS,
|
|
48
|
+
// },
|
|
49
|
+
// babelConfig,
|
|
50
|
+
// );
|
|
51
|
+
// }),
|
|
52
|
+
// );
|
|
53
|
+
// results.forEach(result => {
|
|
54
|
+
// if (result.code === 1) {
|
|
55
|
+
// throw new Error(result.message);
|
|
56
|
+
// }
|
|
57
|
+
// });
|
|
58
|
+
// };
|
|
63
59
|
|
|
64
60
|
var _default = () => ({
|
|
65
61
|
name: '@modern-js/plugin-bff',
|
|
@@ -187,7 +183,7 @@ var _default = () => ({
|
|
|
187
183
|
}
|
|
188
184
|
|
|
189
185
|
if (patterns.length > 0) {
|
|
190
|
-
await compile(appDirectory, modernConfig, {
|
|
186
|
+
await (0, _serverUtils.compile)(appDirectory, modernConfig, {
|
|
191
187
|
patterns
|
|
192
188
|
});
|
|
193
189
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
|
|
6
|
+
import { compile } from '@modern-js/server-utils';
|
|
7
|
+
import { registerModernRuntimePath } from "./helper";
|
|
8
|
+
var DEFAULT_API_PREFIX = '/api';
|
|
9
|
+
var TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
10
|
+
var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // const compile = async (
|
|
11
|
+
// appDirectory: string,
|
|
12
|
+
// modernConfig: NormalizedConfig,
|
|
13
|
+
// compileOptions: CompileOptions,
|
|
14
|
+
// ) => {
|
|
15
|
+
// const { patterns } = compileOptions;
|
|
16
|
+
// const results = await Promise.all(
|
|
17
|
+
// patterns.map(async pattern => {
|
|
18
|
+
// const { from, to, tsconfigPath } = pattern;
|
|
19
|
+
// const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
20
|
+
// tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
21
|
+
// syntax: 'es6+',
|
|
22
|
+
// type: 'commonjs',
|
|
23
|
+
// });
|
|
24
|
+
// if (await fs.pathExists(from)) {
|
|
25
|
+
// const basename = path.basename(from);
|
|
26
|
+
// const targetDir = path.join(to, basename);
|
|
27
|
+
// await fs.copy(from, targetDir, {
|
|
28
|
+
// filter: src =>
|
|
29
|
+
// !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath,
|
|
30
|
+
// });
|
|
31
|
+
// }
|
|
32
|
+
// return compiler(
|
|
33
|
+
// {
|
|
34
|
+
// rootDir: appDirectory,
|
|
35
|
+
// distDir: to,
|
|
36
|
+
// sourceDir: from,
|
|
37
|
+
// extensions: FILE_EXTENSIONS,
|
|
38
|
+
// },
|
|
39
|
+
// babelConfig,
|
|
40
|
+
// );
|
|
41
|
+
// }),
|
|
42
|
+
// );
|
|
43
|
+
// results.forEach(result => {
|
|
44
|
+
// if (result.code === 1) {
|
|
45
|
+
// throw new Error(result.message);
|
|
46
|
+
// }
|
|
47
|
+
// });
|
|
48
|
+
// };
|
|
49
|
+
|
|
50
|
+
export default (function () {
|
|
51
|
+
return {
|
|
52
|
+
name: '@modern-js/plugin-bff',
|
|
53
|
+
setup: function setup(api) {
|
|
54
|
+
var unRegisterResolveRuntimePath = null;
|
|
55
|
+
return {
|
|
56
|
+
validateSchema: function validateSchema() {
|
|
57
|
+
return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
|
|
58
|
+
},
|
|
59
|
+
config: function config() {
|
|
60
|
+
return {
|
|
61
|
+
tools: {
|
|
62
|
+
webpackChain: function webpackChain(chain, _ref) {
|
|
63
|
+
var name = _ref.name,
|
|
64
|
+
CHAIN_ID = _ref.CHAIN_ID;
|
|
65
|
+
|
|
66
|
+
var _api$useAppContext = api.useAppContext(),
|
|
67
|
+
appDirectory = _api$useAppContext.appDirectory,
|
|
68
|
+
port = _api$useAppContext.port;
|
|
69
|
+
|
|
70
|
+
var modernConfig = api.useResolvedConfigContext();
|
|
71
|
+
|
|
72
|
+
var _ref2 = modernConfig || {},
|
|
73
|
+
bff = _ref2.bff;
|
|
74
|
+
|
|
75
|
+
var _ref3 = bff || {},
|
|
76
|
+
fetcher = _ref3.fetcher;
|
|
77
|
+
|
|
78
|
+
var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
|
|
79
|
+
var rootDir = path.resolve(appDirectory, API_DIR);
|
|
80
|
+
chain.resolve.alias.set('@api', rootDir);
|
|
81
|
+
var apiRegexp = new RegExp(normalizeOutputPath("".concat(appDirectory).concat(path.sep, "api").concat(path.sep, ".*(.[tj]s)$")));
|
|
82
|
+
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({
|
|
83
|
+
prefix: prefix,
|
|
84
|
+
apiDir: rootDir,
|
|
85
|
+
port: port,
|
|
86
|
+
fetcher: fetcher,
|
|
87
|
+
target: name,
|
|
88
|
+
requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
source: {
|
|
93
|
+
moduleScopes: ["./".concat(API_DIR), /create-request/]
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
modifyServerRoutes: function modifyServerRoutes(_ref4) {
|
|
98
|
+
var routes = _ref4.routes;
|
|
99
|
+
var modernConfig = api.useResolvedConfigContext();
|
|
100
|
+
|
|
101
|
+
var _ref5 = modernConfig || {},
|
|
102
|
+
bff = _ref5.bff;
|
|
103
|
+
|
|
104
|
+
var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
|
|
105
|
+
var prefixList = [];
|
|
106
|
+
|
|
107
|
+
if (Array.isArray(prefix)) {
|
|
108
|
+
prefixList.push.apply(prefixList, _toConsumableArray(prefix));
|
|
109
|
+
} else {
|
|
110
|
+
prefixList.push(prefix);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var apiServerRoutes = prefixList.map(function (pre) {
|
|
114
|
+
return {
|
|
115
|
+
urlPath: pre,
|
|
116
|
+
isApi: true,
|
|
117
|
+
entryPath: '',
|
|
118
|
+
isSPA: false,
|
|
119
|
+
isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
|
|
120
|
+
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
routes: routes.concat(apiServerRoutes)
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
beforeBuild: function beforeBuild() {
|
|
128
|
+
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
129
|
+
var _api$useAppContext2, internalDirectory;
|
|
130
|
+
|
|
131
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
132
|
+
while (1) {
|
|
133
|
+
switch (_context.prev = _context.next) {
|
|
134
|
+
case 0:
|
|
135
|
+
// help esbuild-register resolve @modern-js/server/runtime
|
|
136
|
+
if (isProd()) {
|
|
137
|
+
_api$useAppContext2 = api.useAppContext(), internalDirectory = _api$useAppContext2.internalDirectory;
|
|
138
|
+
unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
case 1:
|
|
142
|
+
case "end":
|
|
143
|
+
return _context.stop();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}, _callee);
|
|
147
|
+
}))();
|
|
148
|
+
},
|
|
149
|
+
afterBuild: function afterBuild() {
|
|
150
|
+
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
151
|
+
var _api$useAppContext3, appDirectory, distDirectory, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, patterns;
|
|
152
|
+
|
|
153
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
154
|
+
while (1) {
|
|
155
|
+
switch (_context2.prev = _context2.next) {
|
|
156
|
+
case 0:
|
|
157
|
+
if (unRegisterResolveRuntimePath) {
|
|
158
|
+
unRegisterResolveRuntimePath();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_api$useAppContext3 = api.useAppContext(), appDirectory = _api$useAppContext3.appDirectory, distDirectory = _api$useAppContext3.distDirectory;
|
|
162
|
+
modernConfig = api.useResolvedConfigContext();
|
|
163
|
+
distDir = path.resolve(distDirectory);
|
|
164
|
+
apiDir = path.resolve(appDirectory, API_DIR);
|
|
165
|
+
sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
|
166
|
+
tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
|
167
|
+
patterns = [];
|
|
168
|
+
|
|
169
|
+
if (fs.existsSync(apiDir)) {
|
|
170
|
+
patterns.push({
|
|
171
|
+
from: apiDir,
|
|
172
|
+
to: distDir,
|
|
173
|
+
tsconfigPath: tsconfigPath
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (fs.existsSync(sharedDir)) {
|
|
178
|
+
patterns.push({
|
|
179
|
+
from: sharedDir,
|
|
180
|
+
to: distDir,
|
|
181
|
+
tsconfigPath: tsconfigPath
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (!(patterns.length > 0)) {
|
|
186
|
+
_context2.next = 13;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
_context2.next = 13;
|
|
191
|
+
return compile(appDirectory, modernConfig, {
|
|
192
|
+
patterns: patterns
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
case 13:
|
|
196
|
+
case "end":
|
|
197
|
+
return _context2.stop();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}, _callee2);
|
|
201
|
+
}))();
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
});
|
|
@@ -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.
|
|
14
|
+
"version": "1.15.1-beta.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": "
|
|
59
|
-
"@modern-js/bff-core": "
|
|
60
|
-
"@modern-js/create-request": "
|
|
61
|
-
"@modern-js/server-utils": "
|
|
62
|
-
"@modern-js/utils": "
|
|
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.1-beta.0",
|
|
62
|
+
"@modern-js/utils": "1.15.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@modern-js/core": "1.
|
|
66
|
-
"@modern-js/
|
|
67
|
-
"@modern-js/runtime": "1.
|
|
68
|
-
"@modern-js/server-core": "1.
|
|
69
|
-
"@modern-js/types": "1.
|
|
70
|
-
"@scripts/build": "
|
|
71
|
-
"@scripts/jest-config": "
|
|
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",
|