@modern-js/prod-server 1.0.1 → 1.0.2
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 +17 -0
- package/dist/js/modern/constants.js +4 -0
- package/dist/js/modern/libs/render/modern/index.js +3 -1
- package/dist/js/modern/server/index.js +34 -61
- package/dist/js/modern/server/modern-server-split.js +42 -33
- package/dist/js/modern/server/modern-server.js +54 -47
- package/dist/js/modern/type.js +1 -1
- package/dist/js/node/constants.js +7 -2
- package/dist/js/node/libs/render/modern/index.js +3 -1
- package/dist/js/node/server/index.js +33 -60
- package/dist/js/node/server/modern-server-split.js +40 -37
- package/dist/js/node/server/modern-server.js +54 -47
- package/dist/js/node/type.js +1 -3
- package/dist/types/constants.d.ts +5 -1
- package/dist/types/server/index.d.ts +5 -10
- package/dist/types/server/modern-server-split.d.ts +2 -26
- package/dist/types/server/modern-server.d.ts +16 -14
- package/dist/types/type.d.ts +16 -2
- package/package.json +4 -4
- package/src/constants.ts +5 -0
- package/src/libs/render/index.ts +1 -0
- package/src/libs/render/modern/index.ts +1 -1
- package/src/server/index.ts +40 -78
- package/src/server/modern-server-split.ts +42 -46
- package/src/server/modern-server.ts +59 -51
- package/src/type.ts +41 -1
- package/tests/fixtures/pure/test-dist/bundles/main.js +5 -0
- package/tests/fixtures/pure/test-dist/html/main/index.html +36 -0
- package/tests/fixtures/pure/test-dist/route.json +13 -0
- package/tests/fixtures/ssr/bundle-error.js +3 -0
- package/tests/fixtures/ssr/tpl.html +11 -0
- package/tests/render.test.ts +106 -2
- package/tests/server.test.ts +118 -5
- package/tests/spr.test.ts +38 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 1.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- a78e32d8: remove server hook when api only
|
|
8
|
+
- e11eaafc: fix runner error in dev
|
|
9
|
+
- cbba492b: add serverless pre-render unit-test
|
|
10
|
+
- 59010b7a: rewrite server lifecycle, add unit test
|
|
11
|
+
- Updated dependencies [cc5e8001]
|
|
12
|
+
- Updated dependencies [2520ea86]
|
|
13
|
+
- Updated dependencies [db43dce6]
|
|
14
|
+
- Updated dependencies [e81fd9b7]
|
|
15
|
+
- Updated dependencies [1c411e71]
|
|
16
|
+
- @modern-js/core@1.4.6
|
|
17
|
+
- @modern-js/utils@1.3.4
|
|
18
|
+
|
|
3
19
|
## 1.0.1
|
|
20
|
+
|
|
4
21
|
### Patch Changes
|
|
5
22
|
|
|
6
23
|
- 02fb4146: support product server
|
|
@@ -5,7 +5,9 @@ import { NativeModuleNameMap } from "./browser-list";
|
|
|
5
5
|
const nativeModules = require('@babel/compat-data/native-modules');
|
|
6
6
|
|
|
7
7
|
export const supportModern = context => {
|
|
8
|
-
|
|
8
|
+
var _context$query;
|
|
9
|
+
|
|
10
|
+
if ((_context$query = context.query) !== null && _context$query !== void 0 && _context$query.modern_es6) {
|
|
9
11
|
return true;
|
|
10
12
|
} // no ua in request headers
|
|
11
13
|
|
|
@@ -7,23 +7,9 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
7
7
|
import path from 'path';
|
|
8
8
|
import { serverManager, AppContext, ConfigContext } from '@modern-js/server-core';
|
|
9
9
|
import { compatRequire, logger as defaultLogger } from '@modern-js/utils';
|
|
10
|
-
import { initAppContext
|
|
10
|
+
import { initAppContext } from '@modern-js/core';
|
|
11
11
|
import { metrics as defaultMetrics } from "../libs/metrics";
|
|
12
|
-
import {
|
|
13
|
-
import { ModernAPIServer, ModernSSRServer, ModernWebServer } from "./modern-server-split";
|
|
14
|
-
|
|
15
|
-
const createProdServer = options => {
|
|
16
|
-
if (options.apiOnly) {
|
|
17
|
-
return new ModernAPIServer(options);
|
|
18
|
-
} else if (options.ssrOnly) {
|
|
19
|
-
return new ModernSSRServer(options);
|
|
20
|
-
} else if (options.webOnly) {
|
|
21
|
-
return new ModernWebServer(options);
|
|
22
|
-
} else {
|
|
23
|
-
return new ModernServer(options);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
12
|
+
import { createProdServer } from "./modern-server-split";
|
|
27
13
|
export class Server {
|
|
28
14
|
constructor(options) {
|
|
29
15
|
this.options = void 0;
|
|
@@ -31,68 +17,68 @@ export class Server {
|
|
|
31
17
|
this.server = void 0;
|
|
32
18
|
this.app = void 0;
|
|
33
19
|
this.runner = void 0;
|
|
20
|
+
options.logger = options.logger || defaultLogger;
|
|
21
|
+
options.metrics = options.metrics || defaultMetrics;
|
|
34
22
|
this.options = options;
|
|
35
23
|
}
|
|
36
24
|
|
|
37
|
-
getRequestHandler() {
|
|
38
|
-
return (req, res, next) => {
|
|
39
|
-
const requestHandler = this.server.getRequestHandler();
|
|
40
|
-
return requestHandler(req, res, next);
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
ready(readyOptions = {}) {
|
|
45
|
-
this.server.ready(readyOptions);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
25
|
async init() {
|
|
49
26
|
const {
|
|
50
27
|
options
|
|
51
|
-
} = this;
|
|
52
|
-
|
|
53
|
-
|
|
28
|
+
} = this; // initialize server runner
|
|
29
|
+
|
|
30
|
+
this.runner = await this.createHookRunner(); // initialize server
|
|
54
31
|
|
|
55
32
|
this.server = this.serverImpl(options); // create http-server
|
|
56
33
|
|
|
57
|
-
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
58
|
-
this.runner = await this.createHookRunner(); // runner can only be used after server init
|
|
34
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler()); // runner can only be used after server init
|
|
59
35
|
|
|
60
|
-
await this.server.
|
|
36
|
+
await this.server.onInit(this.runner);
|
|
61
37
|
return this;
|
|
62
38
|
}
|
|
63
39
|
|
|
40
|
+
async close() {
|
|
41
|
+
await this.server.onClose();
|
|
42
|
+
await new Promise(resolve => this.app.close(() => {
|
|
43
|
+
resolve();
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
|
|
64
47
|
listen(port = 8080, listener) {
|
|
65
48
|
this.app.listen(process.env.PORT || port, () => {
|
|
66
49
|
if (listener) {
|
|
67
50
|
listener();
|
|
68
51
|
}
|
|
69
52
|
|
|
70
|
-
this.
|
|
53
|
+
this.server.onListening(this.app);
|
|
71
54
|
});
|
|
72
55
|
}
|
|
73
56
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await this.server.close();
|
|
80
|
-
await new Promise(resolve => this.app.close(() => {
|
|
81
|
-
resolve();
|
|
82
|
-
}));
|
|
57
|
+
getRequestHandler() {
|
|
58
|
+
return (req, res, next) => {
|
|
59
|
+
const requestHandler = this.server.getRequestHandler();
|
|
60
|
+
return requestHandler(req, res, next);
|
|
61
|
+
};
|
|
83
62
|
}
|
|
84
63
|
|
|
85
64
|
async createHookRunner() {
|
|
86
|
-
|
|
87
|
-
|
|
65
|
+
// clear server manager every create time
|
|
66
|
+
serverManager.clear();
|
|
88
67
|
const {
|
|
89
68
|
options
|
|
90
69
|
} = this;
|
|
91
|
-
|
|
92
|
-
|
|
70
|
+
const {
|
|
71
|
+
pwd: appDirectory,
|
|
72
|
+
plugins = []
|
|
73
|
+
} = options; // server app context for serve plugin
|
|
74
|
+
|
|
75
|
+
const serverPlugins = plugins.map(p => {
|
|
93
76
|
serverManager.usePlugin(compatRequire(p.pluginPath));
|
|
77
|
+
return {
|
|
78
|
+
server: p
|
|
79
|
+
};
|
|
94
80
|
});
|
|
95
|
-
const appContext =
|
|
81
|
+
const appContext = initAppContext(appDirectory, serverPlugins, '');
|
|
96
82
|
serverManager.run(() => {
|
|
97
83
|
var _options$config$outpu;
|
|
98
84
|
|
|
@@ -104,17 +90,4 @@ export class Server {
|
|
|
104
90
|
return serverManager.init({});
|
|
105
91
|
}
|
|
106
92
|
|
|
107
|
-
async initAppContext() {
|
|
108
|
-
var _this$options$plugins;
|
|
109
|
-
|
|
110
|
-
const appDirectory = await initAppDir();
|
|
111
|
-
const loaded = await loadUserConfig(appDirectory);
|
|
112
|
-
const plugins = (_this$options$plugins = this.options.plugins) === null || _this$options$plugins === void 0 ? void 0 : _this$options$plugins.map(p => ({
|
|
113
|
-
server: p,
|
|
114
|
-
cli: undefined
|
|
115
|
-
}));
|
|
116
|
-
const appContext = initAppContext(appDirectory, plugins || [], loaded.filePath);
|
|
117
|
-
return appContext;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
93
|
}
|
|
@@ -1,55 +1,52 @@
|
|
|
1
|
+
import { RUN_MODE } from "../constants";
|
|
1
2
|
import { ModernServer } from "./modern-server";
|
|
2
|
-
export class ModernSSRServer extends ModernServer {
|
|
3
|
-
// Todo should not invoke any route hook in modernSSRServer
|
|
4
|
-
async warmupSSRBundle() {// empty
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
verifyMatch(context, matched) {
|
|
8
|
-
if (matched.generate(context.url).isApi) {
|
|
9
|
-
this.render404(context);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
3
|
|
|
4
|
+
class ModernSSRServer extends ModernServer {
|
|
13
5
|
prepareAPIHandler(_m, _) {
|
|
14
6
|
return null;
|
|
15
7
|
}
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
return
|
|
19
|
-
} // protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
20
|
-
// return routes.filter(route => route.entryName);
|
|
21
|
-
// }
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
async preServerInit() {// empty
|
|
9
|
+
filterRoutes(routes) {
|
|
10
|
+
return routes.filter(route => route.isSSR);
|
|
25
11
|
}
|
|
26
12
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
13
|
+
async preServerInit() {
|
|
14
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
15
|
+
await super.preServerInit();
|
|
16
|
+
}
|
|
30
17
|
}
|
|
31
18
|
|
|
32
|
-
async
|
|
19
|
+
async emitRouteHook(_, _input) {
|
|
20
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
21
|
+
await super.emitRouteHook(_, _input);
|
|
22
|
+
}
|
|
33
23
|
}
|
|
34
24
|
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class ModernAPIServer extends ModernServer {
|
|
35
28
|
prepareWebHandler(_) {
|
|
36
29
|
return null;
|
|
37
30
|
}
|
|
38
31
|
|
|
39
|
-
async prepareAPIHandler(mode, extension) {
|
|
40
|
-
return super.prepareAPIHandler(mode, extension);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
32
|
filterRoutes(routes) {
|
|
44
33
|
return routes.filter(route => route.isApi);
|
|
45
34
|
}
|
|
46
35
|
|
|
47
|
-
async preServerInit() {
|
|
36
|
+
async preServerInit() {
|
|
37
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
38
|
+
await super.preServerInit();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async emitRouteHook(_, _input) {// empty
|
|
48
43
|
}
|
|
49
44
|
|
|
50
45
|
}
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
|
|
47
|
+
class ModernWebServer extends ModernServer {
|
|
48
|
+
async warmupSSRBundle() {
|
|
49
|
+
return null;
|
|
53
50
|
}
|
|
54
51
|
|
|
55
52
|
async handleAPI(context) {
|
|
@@ -57,10 +54,10 @@ export class ModernWebServer extends ModernServer {
|
|
|
57
54
|
proxyTarget
|
|
58
55
|
} = this;
|
|
59
56
|
|
|
60
|
-
if (
|
|
61
|
-
this.proxy();
|
|
57
|
+
if (proxyTarget !== null && proxyTarget !== void 0 && proxyTarget.api) {
|
|
58
|
+
return this.proxy();
|
|
62
59
|
} else {
|
|
63
|
-
this.render404(context);
|
|
60
|
+
return this.render404(context);
|
|
64
61
|
}
|
|
65
62
|
}
|
|
66
63
|
|
|
@@ -78,4 +75,16 @@ export class ModernWebServer extends ModernServer {
|
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
77
|
|
|
81
|
-
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const createProdServer = options => {
|
|
81
|
+
if (options.apiOnly) {
|
|
82
|
+
return new ModernAPIServer(options);
|
|
83
|
+
} else if (options.ssrOnly) {
|
|
84
|
+
return new ModernSSRServer(options);
|
|
85
|
+
} else if (options.webOnly) {
|
|
86
|
+
return new ModernWebServer(options);
|
|
87
|
+
} else {
|
|
88
|
+
return new ModernServer(options);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
@@ -25,7 +25,7 @@ import { createErrorDocument, createMiddlewareCollecter, getStaticReg, mergeExte
|
|
|
25
25
|
import * as reader from "../libs/render/reader";
|
|
26
26
|
import { createProxyHandler } from "../libs/proxy";
|
|
27
27
|
import { createContext } from "../libs/context";
|
|
28
|
-
import { AGGRED_DIR, ApiServerMode, ERROR_DIGEST, ERROR_PAGE_TEXT } from "../constants";
|
|
28
|
+
import { AGGRED_DIR, ApiServerMode, ERROR_DIGEST, ERROR_PAGE_TEXT, RUN_MODE } from "../constants";
|
|
29
29
|
import { createTemplateAPI } from "../libs/hook-api/template";
|
|
30
30
|
import { createRouteAPI } from "../libs/hook-api/route";
|
|
31
31
|
const API_DIR = './api';
|
|
@@ -41,6 +41,7 @@ export class ModernServer {
|
|
|
41
41
|
staticGenerate,
|
|
42
42
|
logger,
|
|
43
43
|
metrics,
|
|
44
|
+
runMode,
|
|
44
45
|
proxyTarget
|
|
45
46
|
}) {
|
|
46
47
|
var _config$output;
|
|
@@ -55,6 +56,7 @@ export class ModernServer {
|
|
|
55
56
|
this.runner = void 0;
|
|
56
57
|
this.logger = void 0;
|
|
57
58
|
this.metrics = void 0;
|
|
59
|
+
this.runMode = void 0;
|
|
58
60
|
this.reader = reader;
|
|
59
61
|
this.proxyTarget = void 0;
|
|
60
62
|
this.staticFileHandler = void 0;
|
|
@@ -63,7 +65,7 @@ export class ModernServer {
|
|
|
63
65
|
this.frameAPIHandler = null;
|
|
64
66
|
this.proxyHandler = null;
|
|
65
67
|
this._handler = void 0;
|
|
66
|
-
this.staticGenerate =
|
|
68
|
+
this.staticGenerate = void 0;
|
|
67
69
|
|
|
68
70
|
require('ignore-styles');
|
|
69
71
|
|
|
@@ -76,21 +78,13 @@ export class ModernServer {
|
|
|
76
78
|
this.router = new RouteMatchManager();
|
|
77
79
|
this.presetRoutes = routes;
|
|
78
80
|
this.proxyTarget = proxyTarget;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.staticGenerate = staticGenerate;
|
|
82
|
-
}
|
|
83
|
-
|
|
81
|
+
this.staticGenerate = staticGenerate || false;
|
|
82
|
+
this.runMode = runMode || RUN_MODE.TYPE;
|
|
84
83
|
process.env.BUILD_TYPE = `${this.staticGenerate ? 'ssg' : 'ssr'}`;
|
|
85
|
-
} // exposed requestHandler
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
getRequestHandler() {
|
|
89
|
-
return this.requestHandler.bind(this);
|
|
90
84
|
} // server prepare
|
|
91
85
|
|
|
92
86
|
|
|
93
|
-
async
|
|
87
|
+
async onInit(runner) {
|
|
94
88
|
var _conf$bff;
|
|
95
89
|
|
|
96
90
|
this.runner = runner;
|
|
@@ -98,7 +92,8 @@ export class ModernServer {
|
|
|
98
92
|
distDir,
|
|
99
93
|
staticGenerate,
|
|
100
94
|
conf
|
|
101
|
-
} = this;
|
|
95
|
+
} = this; // Todo: why add this middleware
|
|
96
|
+
|
|
102
97
|
this.addHandler((ctx, next) => {
|
|
103
98
|
ctx.res.setHeader('Access-Control-Allow-Origin', '*');
|
|
104
99
|
ctx.res.setHeader('Access-Control-Allow-Credentials', 'false');
|
|
@@ -111,12 +106,14 @@ export class ModernServer {
|
|
|
111
106
|
this.proxyHandler.forEach(handler => {
|
|
112
107
|
this.addHandler(handler);
|
|
113
108
|
});
|
|
114
|
-
} // start reader
|
|
109
|
+
} // start file reader
|
|
115
110
|
|
|
116
111
|
|
|
117
112
|
this.reader.init(); // use preset routes priority
|
|
118
113
|
|
|
119
|
-
this.
|
|
114
|
+
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
115
|
+
this.router.reset(usageRoutes); // warmup ssr bundle in production env
|
|
116
|
+
|
|
120
117
|
this.warmupSSRBundle();
|
|
121
118
|
await this.prepareFrameHandler(); // Only work when without setting `assetPrefix`.
|
|
122
119
|
// Setting `assetPrefix` means these resources should be uploaded to CDN.
|
|
@@ -132,29 +129,47 @@ export class ModernServer {
|
|
|
132
129
|
});
|
|
133
130
|
await this.preServerInit();
|
|
134
131
|
this.addHandler(this.staticFileHandler);
|
|
135
|
-
this.addHandler(this.routeHandler.bind(this));
|
|
132
|
+
this.addHandler(this.routeHandler.bind(this)); // compose middlewares to http handler
|
|
133
|
+
|
|
136
134
|
this.compose();
|
|
135
|
+
} // close any thing run in server
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async onClose() {
|
|
139
|
+
this.reader.close();
|
|
137
140
|
} // server ready
|
|
138
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
139
141
|
|
|
140
142
|
|
|
141
|
-
|
|
143
|
+
onRepack(_) {// empty
|
|
144
|
+
} // invoke when http server listen
|
|
142
145
|
|
|
143
146
|
|
|
144
147
|
onListening(_) {// empty
|
|
145
|
-
}
|
|
148
|
+
}
|
|
146
149
|
|
|
150
|
+
onServerChange(_) {
|
|
151
|
+
this.prepareFrameHandler();
|
|
152
|
+
} // exposed requestHandler
|
|
147
153
|
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
|
|
155
|
+
getRequestHandler() {
|
|
156
|
+
return this.requestHandler.bind(this);
|
|
150
157
|
}
|
|
151
158
|
|
|
152
159
|
async createHTTPServer(handler) {
|
|
153
160
|
return createServer(handler);
|
|
154
|
-
}
|
|
161
|
+
}
|
|
162
|
+
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
163
|
+
// get routes info
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
getRoutes() {
|
|
167
|
+
// Preferred to use preset routes
|
|
168
|
+
if (this.presetRoutes) {
|
|
169
|
+
return this.presetRoutes;
|
|
170
|
+
} // read routes from spec file
|
|
155
171
|
|
|
156
172
|
|
|
157
|
-
readRouteSpec() {
|
|
158
173
|
const file = path.join(this.distDir, ROUTE_SPEC_FILE);
|
|
159
174
|
|
|
160
175
|
if (fs.existsSync(file)) {
|
|
@@ -213,14 +228,7 @@ export class ModernServer {
|
|
|
213
228
|
const apiExtension = mergeExtension(pluginAPIExt);
|
|
214
229
|
this.frameAPIHandler = await this.prepareAPIHandler(mode, apiExtension);
|
|
215
230
|
}
|
|
216
|
-
} // Todo
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
async proxy() {
|
|
220
|
-
return null;
|
|
221
231
|
}
|
|
222
|
-
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
223
|
-
|
|
224
232
|
|
|
225
233
|
async prepareWebHandler(extension) {
|
|
226
234
|
const {
|
|
@@ -264,19 +272,6 @@ export class ModernServer {
|
|
|
264
272
|
return this.runner[eventName](input, {
|
|
265
273
|
onLast: noop
|
|
266
274
|
});
|
|
267
|
-
} // warmup ssr function
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
warmupSSRBundle() {
|
|
271
|
-
const {
|
|
272
|
-
distDir
|
|
273
|
-
} = this;
|
|
274
|
-
const bundles = this.router.getBundles();
|
|
275
|
-
bundles.forEach(bundle => {
|
|
276
|
-
const filepath = path.join(distDir, bundle); // if error, just throw and let process die
|
|
277
|
-
|
|
278
|
-
require(filepath);
|
|
279
|
-
});
|
|
280
275
|
}
|
|
281
276
|
|
|
282
277
|
async preServerInit() {
|
|
@@ -311,7 +306,21 @@ export class ModernServer {
|
|
|
311
306
|
});
|
|
312
307
|
}
|
|
313
308
|
|
|
314
|
-
|
|
309
|
+
async proxy() {
|
|
310
|
+
return null;
|
|
311
|
+
} // warmup ssr function
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
warmupSSRBundle() {
|
|
315
|
+
const {
|
|
316
|
+
distDir
|
|
317
|
+
} = this;
|
|
318
|
+
const bundles = this.router.getBundles();
|
|
319
|
+
bundles.forEach(bundle => {
|
|
320
|
+
const filepath = path.join(distDir, bundle); // if error, just throw and let process die
|
|
321
|
+
|
|
322
|
+
require(filepath);
|
|
323
|
+
});
|
|
315
324
|
}
|
|
316
325
|
/* —————————————————————— private function —————————————————————— */
|
|
317
326
|
// handler route.json, include api / csr / ssr
|
|
@@ -332,8 +341,6 @@ export class ModernServer {
|
|
|
332
341
|
if (!matched) {
|
|
333
342
|
this.render404(context);
|
|
334
343
|
return;
|
|
335
|
-
} else {
|
|
336
|
-
this.verifyMatch(context, matched);
|
|
337
344
|
}
|
|
338
345
|
|
|
339
346
|
if (res.headersSent) {
|
package/dist/js/modern/type.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import { IncomingMessage } from 'http';
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.ERROR_PAGE_TEXT = exports.ERROR_DIGEST = exports.ApiServerMode = exports.AGGRED_DIR = void 0;
|
|
6
|
+
exports.RUN_MODE = exports.ERROR_PAGE_TEXT = exports.ERROR_DIGEST = exports.ApiServerMode = exports.AGGRED_DIR = void 0;
|
|
7
7
|
const AGGRED_DIR = {
|
|
8
8
|
mock: 'config/mock',
|
|
9
9
|
server: 'server',
|
|
@@ -33,4 +33,9 @@ const ERROR_PAGE_TEXT = {
|
|
|
33
33
|
404: 'This page could not be found.',
|
|
34
34
|
500: 'Internal Server Error.'
|
|
35
35
|
};
|
|
36
|
-
exports.ERROR_PAGE_TEXT = ERROR_PAGE_TEXT;
|
|
36
|
+
exports.ERROR_PAGE_TEXT = ERROR_PAGE_TEXT;
|
|
37
|
+
const RUN_MODE = {
|
|
38
|
+
FULL: 'full',
|
|
39
|
+
TYPE: 'type'
|
|
40
|
+
};
|
|
41
|
+
exports.RUN_MODE = RUN_MODE;
|
|
@@ -16,7 +16,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
16
16
|
const nativeModules = require('@babel/compat-data/native-modules');
|
|
17
17
|
|
|
18
18
|
const supportModern = context => {
|
|
19
|
-
|
|
19
|
+
var _context$query;
|
|
20
|
+
|
|
21
|
+
if ((_context$query = context.query) !== null && _context$query !== void 0 && _context$query.modern_es6) {
|
|
20
22
|
return true;
|
|
21
23
|
} // no ua in request headers
|
|
22
24
|
|
|
@@ -15,8 +15,6 @@ var _core = require("@modern-js/core");
|
|
|
15
15
|
|
|
16
16
|
var _metrics = require("../libs/metrics");
|
|
17
17
|
|
|
18
|
-
var _modernServer = require("./modern-server");
|
|
19
|
-
|
|
20
18
|
var _modernServerSplit = require("./modern-server-split");
|
|
21
19
|
|
|
22
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -27,89 +25,77 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
27
25
|
|
|
28
26
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
29
27
|
|
|
30
|
-
const createProdServer = options => {
|
|
31
|
-
if (options.apiOnly) {
|
|
32
|
-
return new _modernServerSplit.ModernAPIServer(options);
|
|
33
|
-
} else if (options.ssrOnly) {
|
|
34
|
-
return new _modernServerSplit.ModernSSRServer(options);
|
|
35
|
-
} else if (options.webOnly) {
|
|
36
|
-
return new _modernServerSplit.ModernWebServer(options);
|
|
37
|
-
} else {
|
|
38
|
-
return new _modernServer.ModernServer(options);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
28
|
class Server {
|
|
43
29
|
constructor(options) {
|
|
44
30
|
this.options = void 0;
|
|
45
|
-
this.serverImpl = createProdServer;
|
|
31
|
+
this.serverImpl = _modernServerSplit.createProdServer;
|
|
46
32
|
this.server = void 0;
|
|
47
33
|
this.app = void 0;
|
|
48
34
|
this.runner = void 0;
|
|
35
|
+
options.logger = options.logger || _utils.logger;
|
|
36
|
+
options.metrics = options.metrics || _metrics.metrics;
|
|
49
37
|
this.options = options;
|
|
50
38
|
}
|
|
51
39
|
|
|
52
|
-
getRequestHandler() {
|
|
53
|
-
return (req, res, next) => {
|
|
54
|
-
const requestHandler = this.server.getRequestHandler();
|
|
55
|
-
return requestHandler(req, res, next);
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
ready(readyOptions = {}) {
|
|
60
|
-
this.server.ready(readyOptions);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
40
|
async init() {
|
|
64
41
|
const {
|
|
65
42
|
options
|
|
66
|
-
} = this;
|
|
67
|
-
|
|
68
|
-
|
|
43
|
+
} = this; // initialize server runner
|
|
44
|
+
|
|
45
|
+
this.runner = await this.createHookRunner(); // initialize server
|
|
69
46
|
|
|
70
47
|
this.server = this.serverImpl(options); // create http-server
|
|
71
48
|
|
|
72
|
-
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
73
|
-
this.runner = await this.createHookRunner(); // runner can only be used after server init
|
|
49
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler()); // runner can only be used after server init
|
|
74
50
|
|
|
75
|
-
await this.server.
|
|
51
|
+
await this.server.onInit(this.runner);
|
|
76
52
|
return this;
|
|
77
53
|
}
|
|
78
54
|
|
|
55
|
+
async close() {
|
|
56
|
+
await this.server.onClose();
|
|
57
|
+
await new Promise(resolve => this.app.close(() => {
|
|
58
|
+
resolve();
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
79
62
|
listen(port = 8080, listener) {
|
|
80
63
|
this.app.listen(process.env.PORT || port, () => {
|
|
81
64
|
if (listener) {
|
|
82
65
|
listener();
|
|
83
66
|
}
|
|
84
67
|
|
|
85
|
-
this.
|
|
68
|
+
this.server.onListening(this.app);
|
|
86
69
|
});
|
|
87
70
|
}
|
|
88
71
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
await this.server.close();
|
|
95
|
-
await new Promise(resolve => this.app.close(() => {
|
|
96
|
-
resolve();
|
|
97
|
-
}));
|
|
72
|
+
getRequestHandler() {
|
|
73
|
+
return (req, res, next) => {
|
|
74
|
+
const requestHandler = this.server.getRequestHandler();
|
|
75
|
+
return requestHandler(req, res, next);
|
|
76
|
+
};
|
|
98
77
|
}
|
|
99
78
|
|
|
100
79
|
async createHookRunner() {
|
|
101
|
-
|
|
80
|
+
// clear server manager every create time
|
|
81
|
+
_serverCore.serverManager.clear();
|
|
102
82
|
|
|
103
83
|
const {
|
|
104
84
|
options
|
|
105
85
|
} = this;
|
|
86
|
+
const {
|
|
87
|
+
pwd: appDirectory,
|
|
88
|
+
plugins = []
|
|
89
|
+
} = options; // server app context for serve plugin
|
|
106
90
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
(_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
|
|
91
|
+
const serverPlugins = plugins.map(p => {
|
|
110
92
|
_serverCore.serverManager.usePlugin((0, _utils.compatRequire)(p.pluginPath));
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
server: p
|
|
96
|
+
};
|
|
111
97
|
});
|
|
112
|
-
const appContext =
|
|
98
|
+
const appContext = (0, _core.initAppContext)(appDirectory, serverPlugins, '');
|
|
113
99
|
|
|
114
100
|
_serverCore.serverManager.run(() => {
|
|
115
101
|
var _options$config$outpu;
|
|
@@ -124,19 +110,6 @@ class Server {
|
|
|
124
110
|
return _serverCore.serverManager.init({});
|
|
125
111
|
}
|
|
126
112
|
|
|
127
|
-
async initAppContext() {
|
|
128
|
-
var _this$options$plugins;
|
|
129
|
-
|
|
130
|
-
const appDirectory = await (0, _core.initAppDir)();
|
|
131
|
-
const loaded = await (0, _core.loadUserConfig)(appDirectory);
|
|
132
|
-
const plugins = (_this$options$plugins = this.options.plugins) === null || _this$options$plugins === void 0 ? void 0 : _this$options$plugins.map(p => ({
|
|
133
|
-
server: p,
|
|
134
|
-
cli: undefined
|
|
135
|
-
}));
|
|
136
|
-
const appContext = (0, _core.initAppContext)(appDirectory, plugins || [], loaded.filePath);
|
|
137
|
-
return appContext;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
113
|
}
|
|
141
114
|
|
|
142
115
|
exports.Server = Server;
|