@modern-js/plugin-koa 2.0.0-beta.3 → 2.0.0-beta.4
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 +44 -0
- package/dist/js/modern/cli/index.js +34 -23
- package/dist/js/modern/context.js +4 -4
- package/dist/js/modern/index.js +4 -1
- package/dist/js/modern/plugin.js +94 -81
- package/dist/js/modern/registerRoutes.js +5 -6
- package/dist/js/modern/runtime.js +7 -3
- package/dist/js/modern/utils.js +60 -27
- package/dist/js/node/cli/index.js +57 -30
- package/dist/js/node/context.js +24 -12
- package/dist/js/node/index.js +31 -22
- package/dist/js/node/plugin.js +123 -95
- package/dist/js/node/registerRoutes.js +25 -14
- package/dist/js/node/runtime.js +24 -28
- package/dist/js/node/utils.js +84 -40
- package/dist/js/treeshaking/cli/index.js +49 -0
- package/dist/js/treeshaking/context.js +3 -0
- package/dist/js/treeshaking/index.js +4 -0
- package/dist/js/treeshaking/plugin.js +363 -0
- package/dist/js/treeshaking/registerRoutes.js +11 -0
- package/dist/js/treeshaking/runtime.js +6 -0
- package/dist/js/treeshaking/utils.js +410 -0
- package/dist/types/cli/index.d.ts +2 -0
- package/dist/types/plugin.d.ts +2 -0
- package/package.json +9 -14
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
3
|
+
import { getRelativeRuntimePath } from "@modern-js/bff-core";
|
|
4
|
+
var cli_default = function() {
|
|
5
|
+
return {
|
|
6
|
+
name: "@modern-js/plugin-koa",
|
|
7
|
+
setup: function(api) {
|
|
8
|
+
var bffExportsUtils;
|
|
9
|
+
var useAppContext = api.useAppContext;
|
|
10
|
+
var runtimeModulePath = path.resolve(__dirname, "../runtime");
|
|
11
|
+
return {
|
|
12
|
+
config: function config() {
|
|
13
|
+
var appContext = useAppContext();
|
|
14
|
+
var appDirectory = appContext.appDirectory;
|
|
15
|
+
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
|
|
16
|
+
var serverRuntimePath = bffExportsUtils.getPath();
|
|
17
|
+
var relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
|
|
18
|
+
if (process.env.NODE_ENV === "production") {
|
|
19
|
+
return {
|
|
20
|
+
source: {
|
|
21
|
+
alias: {
|
|
22
|
+
"@modern-js/runtime/server": relativeRuntimePath,
|
|
23
|
+
"@modern-js/runtime/koa": relativeRuntimePath
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
} else {
|
|
28
|
+
return {
|
|
29
|
+
source: {
|
|
30
|
+
alias: {
|
|
31
|
+
"@modern-js/runtime/server": serverRuntimePath,
|
|
32
|
+
"@modern-js/runtime/koa": serverRuntimePath
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
addRuntimeExports: function addRuntimeExports(input) {
|
|
39
|
+
var currentFile = bffExportsUtils.getPath();
|
|
40
|
+
var relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
41
|
+
var relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("koa"));
|
|
42
|
+
bffExportsUtils.addExport("const pluginRuntime = require('".concat(relativeRuntimeModulePath, "');\n const Koa = require('").concat(relativeFramePath, "')\n module.exports = {\n Koa: Koa,\n ...pluginRuntime\n }\n "));
|
|
43
|
+
return input;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export { cli_default as default };
|
|
@@ -0,0 +1,363 @@
|
|
|
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
|
+
function _instanceof(left, right) {
|
|
31
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
32
|
+
return !!right[Symbol.hasInstance](left);
|
|
33
|
+
} else {
|
|
34
|
+
return left instanceof right;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
|
38
|
+
var f, y, t, g, _ = {
|
|
39
|
+
label: 0,
|
|
40
|
+
sent: function() {
|
|
41
|
+
if (t[0] & 1) throw t[1];
|
|
42
|
+
return t[1];
|
|
43
|
+
},
|
|
44
|
+
trys: [],
|
|
45
|
+
ops: []
|
|
46
|
+
};
|
|
47
|
+
return(g = {
|
|
48
|
+
next: verb(0),
|
|
49
|
+
"throw": verb(1),
|
|
50
|
+
"return": verb(2)
|
|
51
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
52
|
+
return this;
|
|
53
|
+
}), g);
|
|
54
|
+
function verb(n) {
|
|
55
|
+
return function(v) {
|
|
56
|
+
return step([
|
|
57
|
+
n,
|
|
58
|
+
v
|
|
59
|
+
]);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function step(op) {
|
|
63
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
64
|
+
while(_)try {
|
|
65
|
+
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;
|
|
66
|
+
if (y = 0, t) op = [
|
|
67
|
+
op[0] & 2,
|
|
68
|
+
t.value
|
|
69
|
+
];
|
|
70
|
+
switch(op[0]){
|
|
71
|
+
case 0:
|
|
72
|
+
case 1:
|
|
73
|
+
t = op;
|
|
74
|
+
break;
|
|
75
|
+
case 4:
|
|
76
|
+
_.label++;
|
|
77
|
+
return {
|
|
78
|
+
value: op[1],
|
|
79
|
+
done: false
|
|
80
|
+
};
|
|
81
|
+
case 5:
|
|
82
|
+
_.label++;
|
|
83
|
+
y = op[1];
|
|
84
|
+
op = [
|
|
85
|
+
0
|
|
86
|
+
];
|
|
87
|
+
continue;
|
|
88
|
+
case 7:
|
|
89
|
+
op = _.ops.pop();
|
|
90
|
+
_.trys.pop();
|
|
91
|
+
continue;
|
|
92
|
+
default:
|
|
93
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
94
|
+
_ = 0;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
98
|
+
_.label = op[1];
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
102
|
+
_.label = t[1];
|
|
103
|
+
t = op;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
if (t && _.label < t[2]) {
|
|
107
|
+
_.label = t[2];
|
|
108
|
+
_.ops.push(op);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (t[2]) _.ops.pop();
|
|
112
|
+
_.trys.pop();
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
op = body.call(thisArg, _);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
op = [
|
|
118
|
+
6,
|
|
119
|
+
e
|
|
120
|
+
];
|
|
121
|
+
y = 0;
|
|
122
|
+
} finally{
|
|
123
|
+
f = t = 0;
|
|
124
|
+
}
|
|
125
|
+
if (op[0] & 5) throw op[1];
|
|
126
|
+
return {
|
|
127
|
+
value: op[0] ? op[1] : void 0,
|
|
128
|
+
done: true
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
import * as path from "path";
|
|
133
|
+
import Koa from "koa";
|
|
134
|
+
import Router from "koa-router";
|
|
135
|
+
import koaBody from "koa-body";
|
|
136
|
+
import { fs, compatRequire } from "@modern-js/utils";
|
|
137
|
+
import { run } from "./context";
|
|
138
|
+
import registerRoutes from "./registerRoutes";
|
|
139
|
+
var findAppModule = function() {
|
|
140
|
+
var _ref = _asyncToGenerator(function(apiDir) {
|
|
141
|
+
var exts, paths, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, filename, err;
|
|
142
|
+
return __generator(this, function(_state) {
|
|
143
|
+
switch(_state.label){
|
|
144
|
+
case 0:
|
|
145
|
+
exts = [
|
|
146
|
+
".ts",
|
|
147
|
+
".js"
|
|
148
|
+
];
|
|
149
|
+
paths = exts.map(function(ext) {
|
|
150
|
+
return path.join(apiDir, "app".concat(ext));
|
|
151
|
+
});
|
|
152
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
153
|
+
_state.label = 1;
|
|
154
|
+
case 1:
|
|
155
|
+
_state.trys.push([
|
|
156
|
+
1,
|
|
157
|
+
6,
|
|
158
|
+
7,
|
|
159
|
+
8
|
|
160
|
+
]);
|
|
161
|
+
_iterator = paths[Symbol.iterator]();
|
|
162
|
+
_state.label = 2;
|
|
163
|
+
case 2:
|
|
164
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
165
|
+
3,
|
|
166
|
+
5
|
|
167
|
+
];
|
|
168
|
+
filename = _step.value;
|
|
169
|
+
return [
|
|
170
|
+
4,
|
|
171
|
+
fs.pathExists(filename)
|
|
172
|
+
];
|
|
173
|
+
case 3:
|
|
174
|
+
if (_state.sent()) {
|
|
175
|
+
delete require.cache[filename];
|
|
176
|
+
return [
|
|
177
|
+
2,
|
|
178
|
+
compatRequire(filename)
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
_state.label = 4;
|
|
182
|
+
case 4:
|
|
183
|
+
_iteratorNormalCompletion = true;
|
|
184
|
+
return [
|
|
185
|
+
3,
|
|
186
|
+
2
|
|
187
|
+
];
|
|
188
|
+
case 5:
|
|
189
|
+
return [
|
|
190
|
+
3,
|
|
191
|
+
8
|
|
192
|
+
];
|
|
193
|
+
case 6:
|
|
194
|
+
err = _state.sent();
|
|
195
|
+
_didIteratorError = true;
|
|
196
|
+
_iteratorError = err;
|
|
197
|
+
return [
|
|
198
|
+
3,
|
|
199
|
+
8
|
|
200
|
+
];
|
|
201
|
+
case 7:
|
|
202
|
+
try {
|
|
203
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
204
|
+
_iterator.return();
|
|
205
|
+
}
|
|
206
|
+
} finally{
|
|
207
|
+
if (_didIteratorError) {
|
|
208
|
+
throw _iteratorError;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return [
|
|
212
|
+
7
|
|
213
|
+
];
|
|
214
|
+
case 8:
|
|
215
|
+
return [
|
|
216
|
+
2,
|
|
217
|
+
null
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
return function findAppModule(apiDir) {
|
|
223
|
+
return _ref.apply(this, arguments);
|
|
224
|
+
};
|
|
225
|
+
}();
|
|
226
|
+
var initMiddlewares = function(middleware, app) {
|
|
227
|
+
middleware.forEach(function(middlewareItem) {
|
|
228
|
+
var middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
229
|
+
app.use(middlewareFunc);
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
var plugin_default = function() {
|
|
233
|
+
return {
|
|
234
|
+
name: "@modern-js/plugin-koa",
|
|
235
|
+
pre: [
|
|
236
|
+
"@modern-js/plugin-bff"
|
|
237
|
+
],
|
|
238
|
+
post: [
|
|
239
|
+
"@modern-js/plugin-server"
|
|
240
|
+
],
|
|
241
|
+
setup: function(api) {
|
|
242
|
+
return {
|
|
243
|
+
prepareApiServer: function prepareApiServer(param) {
|
|
244
|
+
var pwd = param.pwd, config = param.config;
|
|
245
|
+
return _asyncToGenerator(function() {
|
|
246
|
+
var app, router, apiDir, appContext, apiHandlerInfos, mode, middleware, middleware1;
|
|
247
|
+
return __generator(this, function(_state) {
|
|
248
|
+
switch(_state.label){
|
|
249
|
+
case 0:
|
|
250
|
+
router = new Router();
|
|
251
|
+
apiDir = path.join(pwd, "./api");
|
|
252
|
+
appContext = api.useAppContext();
|
|
253
|
+
apiHandlerInfos = appContext.apiHandlerInfos;
|
|
254
|
+
mode = appContext.apiMode;
|
|
255
|
+
if (!(mode === "framework")) return [
|
|
256
|
+
3,
|
|
257
|
+
2
|
|
258
|
+
];
|
|
259
|
+
return [
|
|
260
|
+
4,
|
|
261
|
+
findAppModule(apiDir)
|
|
262
|
+
];
|
|
263
|
+
case 1:
|
|
264
|
+
app = _state.sent();
|
|
265
|
+
if (!_instanceof(app, Koa)) {
|
|
266
|
+
app = new Koa();
|
|
267
|
+
app.use(koaBody({
|
|
268
|
+
multipart: true
|
|
269
|
+
}));
|
|
270
|
+
}
|
|
271
|
+
if (config) {
|
|
272
|
+
middleware = config.middleware;
|
|
273
|
+
initMiddlewares(middleware, app);
|
|
274
|
+
}
|
|
275
|
+
app.use(run);
|
|
276
|
+
registerRoutes(router, apiHandlerInfos);
|
|
277
|
+
return [
|
|
278
|
+
3,
|
|
279
|
+
3
|
|
280
|
+
];
|
|
281
|
+
case 2:
|
|
282
|
+
if (mode === "function") {
|
|
283
|
+
app = new Koa();
|
|
284
|
+
app.use(koaBody({
|
|
285
|
+
multipart: true
|
|
286
|
+
}));
|
|
287
|
+
if (config) {
|
|
288
|
+
middleware1 = config.middleware;
|
|
289
|
+
initMiddlewares(middleware1, app);
|
|
290
|
+
}
|
|
291
|
+
app.use(run);
|
|
292
|
+
registerRoutes(router, apiHandlerInfos);
|
|
293
|
+
} else {
|
|
294
|
+
throw new Error("mode must be function or framework");
|
|
295
|
+
}
|
|
296
|
+
_state.label = 3;
|
|
297
|
+
case 3:
|
|
298
|
+
app.use(router.routes());
|
|
299
|
+
return [
|
|
300
|
+
2,
|
|
301
|
+
function(req, res) {
|
|
302
|
+
return Promise.resolve(app.callback()(req, res));
|
|
303
|
+
}
|
|
304
|
+
];
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
})();
|
|
308
|
+
},
|
|
309
|
+
prepareWebServer: function prepareWebServer(param, next) {
|
|
310
|
+
var config = param.config;
|
|
311
|
+
var ref;
|
|
312
|
+
var userConfig = api.useConfigContext();
|
|
313
|
+
if (userConfig === null || userConfig === void 0 ? void 0 : (ref = userConfig.server) === null || ref === void 0 ? void 0 : ref.disableFrameworkExt) {
|
|
314
|
+
return next();
|
|
315
|
+
}
|
|
316
|
+
var app = new Koa();
|
|
317
|
+
app.use(function() {
|
|
318
|
+
var _ref = _asyncToGenerator(function(ctx, next2) {
|
|
319
|
+
return __generator(this, function(_state) {
|
|
320
|
+
switch(_state.label){
|
|
321
|
+
case 0:
|
|
322
|
+
return [
|
|
323
|
+
4,
|
|
324
|
+
next2()
|
|
325
|
+
];
|
|
326
|
+
case 1:
|
|
327
|
+
_state.sent();
|
|
328
|
+
if (!ctx.body) {
|
|
329
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
330
|
+
ctx.res.statusCode = 200;
|
|
331
|
+
}
|
|
332
|
+
ctx.respond = false;
|
|
333
|
+
}
|
|
334
|
+
return [
|
|
335
|
+
2
|
|
336
|
+
];
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
return function(ctx, next2) {
|
|
341
|
+
return _ref.apply(this, arguments);
|
|
342
|
+
};
|
|
343
|
+
}());
|
|
344
|
+
app.use(koaBody());
|
|
345
|
+
if (config) {
|
|
346
|
+
var middleware = config.middleware;
|
|
347
|
+
initMiddlewares(middleware, app);
|
|
348
|
+
}
|
|
349
|
+
return function(ctx) {
|
|
350
|
+
var _source = ctx.source, req = _source.req, res = _source.res;
|
|
351
|
+
app.on("error", function(err) {
|
|
352
|
+
if (err) {
|
|
353
|
+
throw err;
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
return Promise.resolve(app.callback()(req, res));
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
export { plugin_default as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createRouteHandler } from "./utils";
|
|
2
|
+
var registerRoutes = function(router, handlerInfos) {
|
|
3
|
+
handlerInfos.forEach(function(param) {
|
|
4
|
+
var routePath = param.routePath, handler = param.handler, httpMethod = param.httpMethod;
|
|
5
|
+
var routeHandler = createRouteHandler(handler);
|
|
6
|
+
var method = httpMethod.toLowerCase();
|
|
7
|
+
router[method](routePath, routeHandler);
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var registerRoutes_default = registerRoutes;
|
|
11
|
+
export { registerRoutes_default as default };
|