@modern-js/plugin-bff 2.5.1-alpha.2 → 2.6.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.
@@ -1,370 +0,0 @@
1
- function _arrayLikeToArray(arr, len) {
2
- if (len == null || len > arr.length) len = arr.length;
3
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
- return arr2;
5
- }
6
- function _arrayWithoutHoles(arr) {
7
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
8
- }
9
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
- try {
11
- var info = gen[key](arg);
12
- var value = info.value;
13
- } catch (error) {
14
- reject(error);
15
- return;
16
- }
17
- if (info.done) {
18
- resolve(value);
19
- } else {
20
- Promise.resolve(value).then(_next, _throw);
21
- }
22
- }
23
- function _asyncToGenerator(fn) {
24
- return function() {
25
- var self = this, args = arguments;
26
- return new Promise(function(resolve, reject) {
27
- var gen = fn.apply(self, args);
28
- function _next(value) {
29
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
- }
31
- function _throw(err) {
32
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
33
- }
34
- _next(undefined);
35
- });
36
- };
37
- }
38
- function _defineProperty(obj, key, value) {
39
- if (key in obj) {
40
- Object.defineProperty(obj, key, {
41
- value: value,
42
- enumerable: true,
43
- configurable: true,
44
- writable: true
45
- });
46
- } else {
47
- obj[key] = value;
48
- }
49
- return obj;
50
- }
51
- function _iterableToArray(iter) {
52
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
53
- }
54
- function _nonIterableSpread() {
55
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
56
- }
57
- function _objectSpread(target) {
58
- for(var i = 1; i < arguments.length; i++){
59
- var source = arguments[i] != null ? arguments[i] : {};
60
- var ownKeys = Object.keys(source);
61
- if (typeof Object.getOwnPropertySymbols === "function") {
62
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
63
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
64
- }));
65
- }
66
- ownKeys.forEach(function(key) {
67
- _defineProperty(target, key, source[key]);
68
- });
69
- }
70
- return target;
71
- }
72
- function ownKeys(object, enumerableOnly) {
73
- var keys = Object.keys(object);
74
- if (Object.getOwnPropertySymbols) {
75
- var symbols = Object.getOwnPropertySymbols(object);
76
- if (enumerableOnly) {
77
- symbols = symbols.filter(function(sym) {
78
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
79
- });
80
- }
81
- keys.push.apply(keys, symbols);
82
- }
83
- return keys;
84
- }
85
- function _objectSpreadProps(target, source) {
86
- source = source != null ? source : {};
87
- if (Object.getOwnPropertyDescriptors) {
88
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
89
- } else {
90
- ownKeys(Object(source)).forEach(function(key) {
91
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
92
- });
93
- }
94
- return target;
95
- }
96
- function _toConsumableArray(arr) {
97
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
98
- }
99
- function _unsupportedIterableToArray(o, minLen) {
100
- if (!o) return;
101
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
102
- var n = Object.prototype.toString.call(o).slice(8, -1);
103
- if (n === "Object" && o.constructor) n = o.constructor.name;
104
- if (n === "Map" || n === "Set") return Array.from(n);
105
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
106
- }
107
- var __generator = this && this.__generator || function(thisArg, body) {
108
- var f, y, t, g, _ = {
109
- label: 0,
110
- sent: function() {
111
- if (t[0] & 1) throw t[1];
112
- return t[1];
113
- },
114
- trys: [],
115
- ops: []
116
- };
117
- return g = {
118
- next: verb(0),
119
- "throw": verb(1),
120
- "return": verb(2)
121
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
122
- return this;
123
- }), g;
124
- function verb(n) {
125
- return function(v) {
126
- return step([
127
- n,
128
- v
129
- ]);
130
- };
131
- }
132
- function step(op) {
133
- if (f) throw new TypeError("Generator is already executing.");
134
- while(_)try {
135
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
136
- if (y = 0, t) op = [
137
- op[0] & 2,
138
- t.value
139
- ];
140
- switch(op[0]){
141
- case 0:
142
- case 1:
143
- t = op;
144
- break;
145
- case 4:
146
- _.label++;
147
- return {
148
- value: op[1],
149
- done: false
150
- };
151
- case 5:
152
- _.label++;
153
- y = op[1];
154
- op = [
155
- 0
156
- ];
157
- continue;
158
- case 7:
159
- op = _.ops.pop();
160
- _.trys.pop();
161
- continue;
162
- default:
163
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
164
- _ = 0;
165
- continue;
166
- }
167
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
168
- _.label = op[1];
169
- break;
170
- }
171
- if (op[0] === 6 && _.label < t[1]) {
172
- _.label = t[1];
173
- t = op;
174
- break;
175
- }
176
- if (t && _.label < t[2]) {
177
- _.label = t[2];
178
- _.ops.push(op);
179
- break;
180
- }
181
- if (t[2]) _.ops.pop();
182
- _.trys.pop();
183
- continue;
184
- }
185
- op = body.call(thisArg, _);
186
- } catch (e) {
187
- op = [
188
- 6,
189
- e
190
- ];
191
- y = 0;
192
- } finally{
193
- f = t = 0;
194
- }
195
- if (op[0] & 5) throw op[1];
196
- return {
197
- value: op[0] ? op[1] : void 0,
198
- done: true
199
- };
200
- }
201
- };
202
- import path from "path";
203
- import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from "@modern-js/utils";
204
- import { compile } from "@modern-js/server-utils";
205
- import { ApiRouter } from "@modern-js/bff-core";
206
- import { registerModernRuntimePath } from "./helper";
207
- var DEFAULT_API_PREFIX = "/api";
208
- var TS_CONFIG_FILENAME = "tsconfig.json";
209
- var cli_default = function() {
210
- return {
211
- name: "@modern-js/plugin-bff",
212
- setup: function(api) {
213
- var unRegisterResolveRuntimePath = null;
214
- return {
215
- validateSchema: function validateSchema() {
216
- return PLUGIN_SCHEMAS["@modern-js/plugin-bff"];
217
- },
218
- config: function config() {
219
- return {
220
- tools: {
221
- webpackChain: function(chain, param) {
222
- var name = param.name, CHAIN_ID = param.CHAIN_ID;
223
- var _api_useAppContext = api.useAppContext(), appDirectory = _api_useAppContext.appDirectory, port = _api_useAppContext.port;
224
- var modernConfig = api.useResolvedConfigContext();
225
- var bff = (modernConfig || {}).bff;
226
- var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
227
- var rootDir = path.resolve(appDirectory, API_DIR);
228
- chain.resolve.alias.set("@api", rootDir);
229
- var apiRouter = new ApiRouter({
230
- apiDir: rootDir,
231
- prefix: prefix
232
- });
233
- var lambdaDir = apiRouter.getLambdaDir();
234
- var existLambda = apiRouter.isExistLambda();
235
- var apiRegexp = new RegExp(normalizeOutputPath("".concat(rootDir).concat(path.sep, ".*(.[tj]s)$")));
236
- chain.module.rule(CHAIN_ID.RULE.JS).exclude.add(apiRegexp);
237
- chain.module.rule(CHAIN_ID.RULE.JS_BFF_API).test(apiRegexp).use("custom-loader").loader(require.resolve("./loader").replace(/\\/g, "/")).options({
238
- prefix: prefix,
239
- apiDir: rootDir,
240
- lambdaDir: lambdaDir,
241
- existLambda: existLambda,
242
- port: port,
243
- target: name
244
- });
245
- }
246
- },
247
- source: {
248
- moduleScopes: [
249
- "./".concat(API_DIR),
250
- /create-request/
251
- ]
252
- }
253
- };
254
- },
255
- modifyServerRoutes: function modifyServerRoutes(param) {
256
- var routes = param.routes;
257
- var modernConfig = api.useResolvedConfigContext();
258
- var bff = (modernConfig || {}).bff;
259
- var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || "/api";
260
- var prefixList = [];
261
- if (Array.isArray(prefix)) {
262
- var _prefixList;
263
- (_prefixList = prefixList).push.apply(_prefixList, _toConsumableArray(prefix));
264
- } else {
265
- prefixList.push(prefix);
266
- }
267
- var apiServerRoutes = prefixList.map(function(pre) {
268
- return {
269
- urlPath: pre,
270
- isApi: true,
271
- entryPath: "",
272
- isSPA: false,
273
- isSSR: false
274
- };
275
- });
276
- if (bff === null || bff === void 0 ? void 0 : bff.enableHandleWeb) {
277
- return {
278
- routes: routes.map(function(route) {
279
- return _objectSpreadProps(_objectSpread({}, route), {
280
- isApi: true
281
- });
282
- }).concat(apiServerRoutes)
283
- };
284
- }
285
- return {
286
- routes: routes.concat(apiServerRoutes)
287
- };
288
- },
289
- collectServerPlugins: function collectServerPlugins(param) {
290
- var plugins = param.plugins;
291
- plugins.push({
292
- "@modern-js/plugin-bff": "@modern-js/plugin-bff/server"
293
- });
294
- return {
295
- plugins: plugins
296
- };
297
- },
298
- beforeBuild: function beforeBuild() {
299
- return _asyncToGenerator(function() {
300
- var internalDirectory;
301
- return __generator(this, function(_state) {
302
- if (isProd()) {
303
- internalDirectory = api.useAppContext().internalDirectory;
304
- unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
305
- }
306
- return [
307
- 2
308
- ];
309
- });
310
- })();
311
- },
312
- afterBuild: function afterBuild() {
313
- return _asyncToGenerator(function() {
314
- var _api_useAppContext, appDirectory, distDirectory, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, sourceDirs, server, _modernConfig_source, alias, define, globalVars, babel;
315
- return __generator(this, function(_state) {
316
- switch(_state.label){
317
- case 0:
318
- if (unRegisterResolveRuntimePath) {
319
- unRegisterResolveRuntimePath();
320
- }
321
- _api_useAppContext = api.useAppContext(), appDirectory = _api_useAppContext.appDirectory, distDirectory = _api_useAppContext.distDirectory;
322
- modernConfig = api.useResolvedConfigContext();
323
- distDir = path.resolve(distDirectory);
324
- apiDir = path.resolve(appDirectory, API_DIR);
325
- sharedDir = path.resolve(appDirectory, SHARED_DIR);
326
- tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
327
- sourceDirs = [];
328
- if (fs.existsSync(apiDir)) {
329
- sourceDirs.push(apiDir);
330
- }
331
- if (fs.existsSync(sharedDir)) {
332
- sourceDirs.push(sharedDir);
333
- }
334
- server = modernConfig.server;
335
- _modernConfig_source = modernConfig.source, alias = _modernConfig_source.alias, define = _modernConfig_source.define, globalVars = _modernConfig_source.globalVars;
336
- babel = modernConfig.tools.babel;
337
- if (!(sourceDirs.length > 0)) return [
338
- 3,
339
- 2
340
- ];
341
- return [
342
- 4,
343
- compile(appDirectory, {
344
- server: server,
345
- alias: alias,
346
- define: define,
347
- globalVars: globalVars,
348
- babelConfig: babel
349
- }, {
350
- sourceDirs: sourceDirs,
351
- distDir: distDir,
352
- tsconfigPath: tsconfigPath
353
- })
354
- ];
355
- case 1:
356
- _state.sent();
357
- _state.label = 2;
358
- case 2:
359
- return [
360
- 2
361
- ];
362
- }
363
- });
364
- })();
365
- }
366
- };
367
- }
368
- };
369
- };
370
- export { cli_default as default };
@@ -1,11 +0,0 @@
1
- var API_APP_NAME = "_app";
2
- var BUILD_FILES = [
3
- "**/*.[tj]sx?",
4
- "!**/*.test.jsx?",
5
- "!**/*.test.tsx?",
6
- "!**/*.spec.jsx?",
7
- "!**/*.spec.tsx?",
8
- "!__tests__/*.tsx?",
9
- "!__tests__/*.jsx?"
10
- ];
11
- export { API_APP_NAME, BUILD_FILES };
@@ -1,23 +0,0 @@
1
- function _defineProperty(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
13
- }
14
- import * as path from "path";
15
- import { registerPaths } from "@modern-js/bff-core";
16
- var serverRuntimeAlias = "@modern-js/runtime/server";
17
- var serverRuntimePath = ".runtime-exports/server";
18
- var registerModernRuntimePath = function(internalDirectory) {
19
- var paths = _defineProperty({}, serverRuntimeAlias, path.join(internalDirectory, serverRuntimePath));
20
- var unRegister = registerPaths(paths);
21
- return unRegister;
22
- };
23
- export { registerModernRuntimePath };
@@ -1 +0,0 @@
1
- export * from "./constants";
@@ -1,191 +0,0 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _asyncToGenerator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- var __generator = this && this.__generator || function(thisArg, body) {
31
- var f, y, t, g, _ = {
32
- label: 0,
33
- sent: function() {
34
- if (t[0] & 1) throw t[1];
35
- return t[1];
36
- },
37
- trys: [],
38
- ops: []
39
- };
40
- return g = {
41
- next: verb(0),
42
- "throw": verb(1),
43
- "return": verb(2)
44
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
- return this;
46
- }), g;
47
- function verb(n) {
48
- return function(v) {
49
- return step([
50
- n,
51
- v
52
- ]);
53
- };
54
- }
55
- function step(op) {
56
- if (f) throw new TypeError("Generator is already executing.");
57
- while(_)try {
58
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
- if (y = 0, t) op = [
60
- op[0] & 2,
61
- t.value
62
- ];
63
- switch(op[0]){
64
- case 0:
65
- case 1:
66
- t = op;
67
- break;
68
- case 4:
69
- _.label++;
70
- return {
71
- value: op[1],
72
- done: false
73
- };
74
- case 5:
75
- _.label++;
76
- y = op[1];
77
- op = [
78
- 0
79
- ];
80
- continue;
81
- case 7:
82
- op = _.ops.pop();
83
- _.trys.pop();
84
- continue;
85
- default:
86
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
- _ = 0;
88
- continue;
89
- }
90
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
- _.label = op[1];
92
- break;
93
- }
94
- if (op[0] === 6 && _.label < t[1]) {
95
- _.label = t[1];
96
- t = op;
97
- break;
98
- }
99
- if (t && _.label < t[2]) {
100
- _.label = t[2];
101
- _.ops.push(op);
102
- break;
103
- }
104
- if (t[2]) _.ops.pop();
105
- _.trys.pop();
106
- continue;
107
- }
108
- op = body.call(thisArg, _);
109
- } catch (e) {
110
- op = [
111
- 6,
112
- e
113
- ];
114
- y = 0;
115
- } finally{
116
- f = t = 0;
117
- }
118
- if (op[0] & 5) throw op[1];
119
- return {
120
- value: op[0] ? op[1] : void 0,
121
- done: true
122
- };
123
- }
124
- };
125
- import { generateClient } from "@modern-js/bff-core";
126
- import { logger } from "@modern-js/utils";
127
- function loader(source) {
128
- return _loader.apply(this, arguments);
129
- }
130
- function _loader() {
131
- _loader = _asyncToGenerator(function(source) {
132
- var callback, draftOptions, resourcePath, warning, options, lambdaDir, result;
133
- return __generator(this, function(_state) {
134
- switch(_state.label){
135
- case 0:
136
- this.cacheable();
137
- callback = this.async();
138
- draftOptions = this.getOptions();
139
- resourcePath = this.resourcePath;
140
- warning = "The file ".concat(resourcePath, " is not allowd to be imported in src directory, only API definition files are allowed.");
141
- if (!draftOptions.existLambda) {
142
- logger.warn(warning);
143
- callback(null, "throw new Error('".concat(warning, "')"));
144
- return [
145
- 2
146
- ];
147
- }
148
- options = {
149
- prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
150
- apiDir: draftOptions.apiDir,
151
- target: draftOptions.target,
152
- port: Number(draftOptions.port),
153
- source: source,
154
- resourcePath: resourcePath
155
- };
156
- lambdaDir = draftOptions.lambdaDir;
157
- if (!resourcePath.startsWith(lambdaDir)) {
158
- logger.warn(warning);
159
- callback(null, "throw new Error('".concat(warning, "')"));
160
- return [
161
- 2
162
- ];
163
- }
164
- if (draftOptions.fetcher) {
165
- options.fetcher = draftOptions.fetcher;
166
- }
167
- if (draftOptions.requestCreator) {
168
- options.requestCreator = draftOptions.requestCreator;
169
- }
170
- options.requireResolve = require.resolve;
171
- return [
172
- 4,
173
- generateClient(options)
174
- ];
175
- case 1:
176
- result = _state.sent();
177
- if (result.isOk) {
178
- callback(void 0, result.value);
179
- } else {
180
- callback(void 0, "throw new Error('".concat(result.value, "')"));
181
- }
182
- return [
183
- 2
184
- ];
185
- }
186
- });
187
- });
188
- return _loader.apply(this, arguments);
189
- }
190
- var loader_default = loader;
191
- export { loader_default as default };