@modern-js/server 1.1.1-canary.3 → 1.1.3-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 +28 -0
- package/dist/js/modern/libs/hook-api/route.js +37 -0
- package/dist/js/modern/libs/{hook-api.js → hook-api/template.js} +0 -0
- package/dist/js/modern/libs/route/index.js +4 -0
- package/dist/js/modern/libs/route/matcher.js +5 -1
- package/dist/js/modern/libs/serve-file.js +23 -25
- package/dist/js/modern/server/{web-server.js → dev-server/dev-server-split.js} +9 -7
- package/dist/js/modern/server/{dev-server.js → dev-server/dev-server.js} +41 -22
- package/dist/js/modern/server/dev-server/index.js +2 -0
- package/dist/js/modern/server/index.js +61 -62
- package/dist/js/modern/server/{api-server.js → modern-server-split.js} +6 -10
- package/dist/js/modern/server/modern-server.js +78 -25
- package/dist/js/modern/utils.js +2 -2
- package/dist/js/node/libs/hook-api/route.js +46 -0
- package/dist/js/node/libs/{hook-api.js → hook-api/template.js} +0 -0
- package/dist/js/node/libs/route/index.js +4 -0
- package/dist/js/node/libs/route/matcher.js +5 -1
- package/dist/js/node/libs/serve-file.js +25 -26
- package/dist/js/node/server/{api-server.js → dev-server/dev-server-split.js} +7 -12
- package/dist/js/node/server/{dev-server.js → dev-server/dev-server.js} +42 -21
- package/dist/js/node/server/dev-server/index.js +27 -0
- package/dist/js/node/server/index.js +67 -63
- package/dist/js/node/server/{web-server.js → modern-server-split.js} +10 -9
- package/dist/js/node/server/modern-server.js +80 -25
- package/dist/js/node/utils.js +2 -2
- package/dist/types/libs/hook-api/route.d.ts +13 -0
- package/dist/types/libs/{hook-api.d.ts → hook-api/template.d.ts} +0 -0
- package/dist/types/libs/route/index.d.ts +1 -0
- package/dist/types/libs/route/matcher.d.ts +1 -0
- package/dist/types/libs/serve-file.d.ts +1 -1
- package/dist/types/server/{api-server.d.ts → dev-server/dev-server-split.d.ts} +7 -8
- package/dist/types/server/{dev-server.d.ts → dev-server/dev-server.d.ts} +6 -5
- package/dist/types/server/dev-server/index.d.ts +2 -0
- package/dist/types/server/index.d.ts +3 -1
- package/dist/types/server/{web-server.d.ts → modern-server-split.d.ts} +5 -4
- package/dist/types/server/modern-server.d.ts +7 -5
- package/dist/types/utils.d.ts +1 -1
- package/package.json +10 -10
- package/src/libs/hook-api/route.ts +38 -0
- package/src/libs/{hook-api.ts → hook-api/template.ts} +0 -0
- package/src/libs/route/index.ts +4 -0
- package/src/libs/route/matcher.ts +5 -1
- package/src/libs/serve-file.ts +16 -20
- package/src/server/{api-server.ts → dev-server/dev-server-split.ts} +9 -11
- package/src/server/{dev-server.ts → dev-server/dev-server.ts} +56 -23
- package/src/server/dev-server/index.ts +2 -0
- package/src/server/index.ts +69 -46
- package/src/server/{web-server.ts → modern-server-split.ts} +12 -10
- package/src/server/modern-server.ts +87 -40
- package/src/utils.ts +2 -2
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.1.
|
|
14
|
+
"version": "1.1.3-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",
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
"@babel/preset-typescript": "^7.15.0",
|
|
42
42
|
"@babel/register": "^7.15.3",
|
|
43
43
|
"@babel/runtime": "^7",
|
|
44
|
-
"@modern-js/core": "
|
|
45
|
-
"@modern-js/hmr-client": "
|
|
46
|
-
"@modern-js/server-plugin": "
|
|
47
|
-
"@modern-js/server-utils": "
|
|
48
|
-
"@modern-js/bff-utils": "
|
|
49
|
-
"@modern-js/utils": "
|
|
44
|
+
"@modern-js/core": "workspace:^1.1.2",
|
|
45
|
+
"@modern-js/hmr-client": "workspace:^1.1.1",
|
|
46
|
+
"@modern-js/server-plugin": "workspace:^1.1.1",
|
|
47
|
+
"@modern-js/server-utils": "workspace:^1.1.1",
|
|
48
|
+
"@modern-js/bff-utils": "workspace:^1.1.1",
|
|
49
|
+
"@modern-js/utils": "workspace:^1.1.2",
|
|
50
50
|
"axios": "^0.21.4",
|
|
51
51
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
52
52
|
"chokidar": "^3.5.2",
|
|
@@ -69,9 +69,9 @@
|
|
|
69
69
|
"ws": "^8.2.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@modern-js/module-tools": "^1.1.
|
|
73
|
-
"@modern-js/plugin-testing": "^1.1.
|
|
74
|
-
"@modern-js/types": "
|
|
72
|
+
"@modern-js/module-tools": "^1.1.1",
|
|
73
|
+
"@modern-js/plugin-testing": "^1.1.1",
|
|
74
|
+
"@modern-js/types": "workspace:^1.1.2",
|
|
75
75
|
"@types/jest": "^26",
|
|
76
76
|
"@types/lru-cache": "^5.1.1",
|
|
77
77
|
"@types/mime-types": "^2.1.0",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RouteMatchManager, RouteMatcher } from '../route';
|
|
2
|
+
|
|
3
|
+
class RouteAPI {
|
|
4
|
+
private readonly router: RouteMatchManager;
|
|
5
|
+
|
|
6
|
+
private current: RouteMatcher;
|
|
7
|
+
|
|
8
|
+
constructor(matched: RouteMatcher, router: RouteMatchManager) {
|
|
9
|
+
this.current = matched;
|
|
10
|
+
this.router = router;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public cur() {
|
|
14
|
+
return this.current.generate();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public get(entryName: string) {
|
|
18
|
+
const { router } = this;
|
|
19
|
+
const matched = router.matchEntry(entryName);
|
|
20
|
+
return matched ? matched.generate() : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public use(entryName: string) {
|
|
24
|
+
const { router } = this;
|
|
25
|
+
const matched = router.matchEntry(entryName);
|
|
26
|
+
if (matched) {
|
|
27
|
+
this.current = matched;
|
|
28
|
+
return true;
|
|
29
|
+
} else {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const createRouteAPI = (
|
|
36
|
+
matched: RouteMatcher,
|
|
37
|
+
router: RouteMatchManager,
|
|
38
|
+
) => new RouteAPI(matched, router);
|
|
File without changes
|
package/src/libs/route/index.ts
CHANGED
|
@@ -60,6 +60,10 @@ export class RouteMatchManager {
|
|
|
60
60
|
return best;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
public matchEntry(entryname: string) {
|
|
64
|
+
return this.matchers.find(matcher => matcher.matchEntry(entryname));
|
|
65
|
+
}
|
|
66
|
+
|
|
63
67
|
public getBundles() {
|
|
64
68
|
const bundles = this.specs
|
|
65
69
|
.filter(route => route.isSSR)
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import { ModernRoute, ModernRouteInterface } from './route';
|
|
9
9
|
|
|
10
10
|
// eslint-disable-next-line no-useless-escape
|
|
11
|
-
const regCharsDetector = /[^a-zA-Z\-_0-9
|
|
11
|
+
const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
|
|
12
12
|
export class RouteMatcher {
|
|
13
13
|
public spec: ModernRouteInterface;
|
|
14
14
|
|
|
@@ -75,6 +75,10 @@ export class RouteMatcher {
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
public matchEntry(entryName: string): boolean {
|
|
79
|
+
return this.spec.entryName === entryName;
|
|
80
|
+
}
|
|
81
|
+
|
|
78
82
|
// compiler urlPath to regexp if necessary
|
|
79
83
|
private setupUrlPath() {
|
|
80
84
|
const { urlPath } = this.spec;
|
package/src/libs/serve-file.ts
CHANGED
|
@@ -1,38 +1,34 @@
|
|
|
1
1
|
// Todo 看看是不是能 fork 一份,即使命中也返回
|
|
2
2
|
import serve from 'serve-static';
|
|
3
|
+
import { isString, isRegExp } from '@modern-js/utils';
|
|
3
4
|
import { NextFunction } from '../type';
|
|
4
5
|
import { ModernServerContext } from './context';
|
|
5
6
|
|
|
6
7
|
type Rule = {
|
|
7
|
-
path: string;
|
|
8
|
+
path: string | RegExp;
|
|
8
9
|
target: string;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
export const createStaticFileHandler =
|
|
12
|
-
|
|
13
|
-
(map: Record<string, ReturnType<typeof serve>>, rule) => {
|
|
14
|
-
map[rule.path] = serve(rule.target);
|
|
15
|
-
return map;
|
|
16
|
-
},
|
|
17
|
-
{},
|
|
18
|
-
);
|
|
19
|
-
|
|
12
|
+
export const createStaticFileHandler =
|
|
13
|
+
(rules: Rule[]) =>
|
|
20
14
|
// eslint-disable-next-line consistent-return
|
|
21
|
-
|
|
15
|
+
async (context: ModernServerContext, next: NextFunction) => {
|
|
22
16
|
const { url: requestUrl, req, res } = context;
|
|
23
|
-
const hit = Object.keys(middlewares).find(u => requestUrl.startsWith(u));
|
|
24
17
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
const hitRule = rules.find(item => {
|
|
19
|
+
if (isString(item.path) && requestUrl.startsWith(item.path)) {
|
|
20
|
+
return true;
|
|
21
|
+
} else if (isRegExp(item.path) && item.path.test(requestUrl)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (hitRule) {
|
|
28
|
+
serve(hitRule.target)(req, res, () => {
|
|
32
29
|
next();
|
|
33
30
|
});
|
|
34
31
|
} else {
|
|
35
32
|
return next();
|
|
36
33
|
}
|
|
37
34
|
};
|
|
38
|
-
};
|
|
@@ -1,27 +1,25 @@
|
|
|
1
|
+
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
1
2
|
import { ModernDevServer } from './dev-server';
|
|
2
|
-
import { ModernServer } from './modern-server';
|
|
3
3
|
import { mergeExtension } from '@/utils';
|
|
4
4
|
import { ModernRouteInterface } from '@/libs/route';
|
|
5
5
|
import { ApiServerMode } from '@/constants';
|
|
6
6
|
|
|
7
|
-
export class
|
|
8
|
-
protected
|
|
7
|
+
export class WebModernDevServer extends ModernDevServer {
|
|
8
|
+
protected prepareAPIHandler(
|
|
9
|
+
_m: ApiServerMode,
|
|
10
|
+
_: APIServerStartInput['config'],
|
|
11
|
+
) {
|
|
9
12
|
return null as any;
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
protected async
|
|
13
|
-
mode: ApiServerMode,
|
|
15
|
+
protected async prepareWebHandler(
|
|
14
16
|
extension: ReturnType<typeof mergeExtension>,
|
|
15
17
|
) {
|
|
16
|
-
return super.
|
|
18
|
+
return super.prepareWebHandler(extension);
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
20
|
-
return routes.filter(route => route.
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
protected async preServerInit() {
|
|
24
|
-
// noop
|
|
22
|
+
return routes.filter(route => route.entryName);
|
|
25
23
|
}
|
|
26
24
|
}
|
|
27
25
|
|
|
@@ -1,27 +1,38 @@
|
|
|
1
|
-
import http, {
|
|
1
|
+
import http, {
|
|
2
|
+
Server,
|
|
3
|
+
createServer,
|
|
4
|
+
IncomingMessage,
|
|
5
|
+
ServerResponse,
|
|
6
|
+
} from 'http';
|
|
2
7
|
import path from 'path';
|
|
3
|
-
import {
|
|
8
|
+
import { createServer as createHttpsServer } from 'https';
|
|
9
|
+
import {
|
|
10
|
+
API_DIR,
|
|
11
|
+
HMR_SOCK_PATH,
|
|
12
|
+
SERVER_DIR,
|
|
13
|
+
SHARED_DIR,
|
|
14
|
+
} from '@modern-js/utils';
|
|
4
15
|
import type { MultiCompiler, Compiler } from 'webpack';
|
|
5
16
|
import webpackDevMiddleware, {
|
|
6
17
|
WebpackDevMiddleware,
|
|
7
18
|
} from 'webpack-dev-middleware';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
19
|
+
import { ModernServer } from '../modern-server';
|
|
20
|
+
import { createMockHandler } from '@/dev-tools/mock';
|
|
21
|
+
import { createProxyHandler, ProxyOptions } from '@/libs/proxy';
|
|
10
22
|
import {
|
|
11
23
|
DevServerOptions,
|
|
12
24
|
ModernServerOptions,
|
|
13
25
|
NextFunction,
|
|
14
26
|
ServerHookRunner,
|
|
15
27
|
ReadyOptions,
|
|
16
|
-
} from '
|
|
17
|
-
import SocketServer from '
|
|
18
|
-
import DevServerPlugin from '
|
|
19
|
-
import { ModernServerContext } from '
|
|
20
|
-
import { createLaunchEditorHandler } from '
|
|
21
|
-
import { enableRegister } from '
|
|
22
|
-
import * as reader from '
|
|
23
|
-
import Watcher from '
|
|
24
|
-
import { ModernServer } from './modern-server';
|
|
28
|
+
} from '@/type';
|
|
29
|
+
import SocketServer from '@/dev-tools/socket-server';
|
|
30
|
+
import DevServerPlugin from '@/dev-tools/dev-server-plugin';
|
|
31
|
+
import { ModernServerContext } from '@/libs/context';
|
|
32
|
+
import { createLaunchEditorHandler } from '@/dev-tools/launch-editor';
|
|
33
|
+
import { enableRegister } from '@/dev-tools/babel/register';
|
|
34
|
+
import * as reader from '@/libs/render/reader';
|
|
35
|
+
import Watcher from '@/dev-tools/watcher';
|
|
25
36
|
import { AGGRED_DIR } from '@/constants';
|
|
26
37
|
|
|
27
38
|
const DEFAULT_DEV_OPTIONS: DevServerOptions = {
|
|
@@ -57,8 +68,8 @@ export class ModernDevServer extends ModernServer {
|
|
|
57
68
|
next: NextFunction,
|
|
58
69
|
) => void);
|
|
59
70
|
|
|
60
|
-
constructor(options: ModernServerOptions
|
|
61
|
-
super(options
|
|
71
|
+
constructor(options: ModernServerOptions) {
|
|
72
|
+
super(options);
|
|
62
73
|
|
|
63
74
|
// set webpack compiler
|
|
64
75
|
this.compiler = options.compiler!;
|
|
@@ -66,14 +77,14 @@ export class ModernDevServer extends ModernServer {
|
|
|
66
77
|
// set dev server options, like webpack-dev-server
|
|
67
78
|
this.dev =
|
|
68
79
|
typeof options.dev === 'boolean' ? DEFAULT_DEV_OPTIONS : options.dev!;
|
|
80
|
+
|
|
81
|
+
enableRegister(this.pwd, this.conf);
|
|
69
82
|
}
|
|
70
83
|
|
|
71
84
|
// Complete the preparation of services
|
|
72
|
-
public async init() {
|
|
85
|
+
public async init(runner: ServerHookRunner) {
|
|
73
86
|
const { conf, pwd, compiler } = this;
|
|
74
87
|
|
|
75
|
-
enableRegister(pwd, conf);
|
|
76
|
-
|
|
77
88
|
// mock handler
|
|
78
89
|
this.mockHandler = createMockHandler({ pwd });
|
|
79
90
|
this.addHandler((ctx: ModernServerContext, next: NextFunction) => {
|
|
@@ -110,7 +121,7 @@ export class ModernDevServer extends ModernServer {
|
|
|
110
121
|
this.addHandler(devMiddlewareHandler);
|
|
111
122
|
}
|
|
112
123
|
|
|
113
|
-
await super.init();
|
|
124
|
+
await super.init(runner);
|
|
114
125
|
|
|
115
126
|
// watch mock/ server/ api/ dir file change
|
|
116
127
|
this.startWatcher();
|
|
@@ -125,6 +136,8 @@ export class ModernDevServer extends ModernServer {
|
|
|
125
136
|
|
|
126
137
|
// reset static file
|
|
127
138
|
reader.updateFile();
|
|
139
|
+
|
|
140
|
+
this.runner.reset();
|
|
128
141
|
}
|
|
129
142
|
|
|
130
143
|
public onListening(app: Server) {
|
|
@@ -141,6 +154,24 @@ export class ModernDevServer extends ModernServer {
|
|
|
141
154
|
});
|
|
142
155
|
}
|
|
143
156
|
|
|
157
|
+
public async createHTTPServer(
|
|
158
|
+
handler: (
|
|
159
|
+
req: IncomingMessage,
|
|
160
|
+
res: ServerResponse,
|
|
161
|
+
next?: () => void,
|
|
162
|
+
) => void,
|
|
163
|
+
) {
|
|
164
|
+
const { dev } = this;
|
|
165
|
+
const devHttpsOption = typeof dev === 'object' && dev.https;
|
|
166
|
+
if (devHttpsOption) {
|
|
167
|
+
const { genHttpsOptions } = require('@/dev-tools/https');
|
|
168
|
+
const httpsOptions = await genHttpsOptions(devHttpsOption);
|
|
169
|
+
return createHttpsServer(httpsOptions, handler);
|
|
170
|
+
} else {
|
|
171
|
+
return createServer(handler);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
144
175
|
// set up plugin to each compiler
|
|
145
176
|
// register hooks for each compilation, update socket stats if recompiled
|
|
146
177
|
// start dev middleware
|
|
@@ -217,12 +248,12 @@ export class ModernDevServer extends ModernServer {
|
|
|
217
248
|
|
|
218
249
|
private startWatcher() {
|
|
219
250
|
const { pwd } = this;
|
|
220
|
-
const { mock
|
|
251
|
+
const { mock } = AGGRED_DIR;
|
|
221
252
|
const defaultWatched = [
|
|
222
253
|
`${pwd}/${mock}/**/*`,
|
|
223
|
-
`${pwd}/${
|
|
224
|
-
`${pwd}/${
|
|
225
|
-
`${pwd}/${
|
|
254
|
+
`${pwd}/${SERVER_DIR}/**/*`,
|
|
255
|
+
`${pwd}/${API_DIR}/**/*`,
|
|
256
|
+
`${pwd}/${SHARED_DIR}/**/*`,
|
|
226
257
|
];
|
|
227
258
|
|
|
228
259
|
const watcher = new Watcher();
|
|
@@ -233,6 +264,8 @@ export class ModernDevServer extends ModernServer {
|
|
|
233
264
|
watcher.updateDepTree();
|
|
234
265
|
watcher.cleanDepCache(filepath);
|
|
235
266
|
|
|
267
|
+
this.runner.reset();
|
|
268
|
+
|
|
236
269
|
if (filepath.startsWith(`${pwd}/${mock}`)) {
|
|
237
270
|
this.mockHandler = createMockHandler({ pwd });
|
|
238
271
|
} else {
|
package/src/server/index.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
ServerResponse,
|
|
4
|
-
createServer,
|
|
5
|
-
Server as httpServer,
|
|
6
|
-
} from 'http';
|
|
7
|
-
import { createServer as createHttpsServer } from 'https';
|
|
1
|
+
import { IncomingMessage, ServerResponse, Server as httpServer } from 'http';
|
|
2
|
+
import path from 'path';
|
|
8
3
|
import { serverManager } from '@modern-js/server-plugin';
|
|
9
4
|
import { logger as defaultLogger } from '@modern-js/utils';
|
|
10
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
AppContext,
|
|
7
|
+
initAppContext,
|
|
8
|
+
initAppDir,
|
|
9
|
+
loadUserConfig,
|
|
10
|
+
} from '@modern-js/core';
|
|
11
11
|
import { ModernServer } from './modern-server';
|
|
12
12
|
import type { ModernDevServer } from './dev-server';
|
|
13
|
+
import { APIModernServer, WebModernServer } from './modern-server-split';
|
|
14
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '@/type';
|
|
13
15
|
import { measure as defaultMeasure } from '@/libs/measure';
|
|
14
16
|
|
|
15
17
|
export class Server {
|
|
@@ -23,9 +25,6 @@ export class Server {
|
|
|
23
25
|
|
|
24
26
|
constructor(options: ModernServerOptions) {
|
|
25
27
|
this.options = options;
|
|
26
|
-
options.plugins?.forEach(p => {
|
|
27
|
-
serverManager.usePlugin(p);
|
|
28
|
-
});
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
public getRequestHandler() {
|
|
@@ -41,38 +40,24 @@ export class Server {
|
|
|
41
40
|
|
|
42
41
|
public async init() {
|
|
43
42
|
const { options } = this;
|
|
44
|
-
this.runner = await serverManager.init({});
|
|
45
|
-
|
|
46
|
-
const { logger, measure } = await this.runner.create(
|
|
47
|
-
{
|
|
48
|
-
loggerOptions: options.logger,
|
|
49
|
-
measureOptions: options.measure,
|
|
50
|
-
},
|
|
51
|
-
{ onLast: () => ({} as any) },
|
|
52
|
-
);
|
|
53
43
|
|
|
54
|
-
options.logger = options.logger ||
|
|
55
|
-
options.measure = options.measure ||
|
|
44
|
+
options.logger = options.logger || defaultLogger;
|
|
45
|
+
options.measure = options.measure || defaultMeasure;
|
|
56
46
|
|
|
47
|
+
// initialize server
|
|
57
48
|
if (options.dev) {
|
|
58
49
|
this.server = this.createDevServer();
|
|
59
|
-
|
|
60
|
-
// check if https is configured when start dev server
|
|
61
|
-
const devHttpsOption =
|
|
62
|
-
typeof options.dev === 'object' && options.dev.https;
|
|
63
|
-
if (devHttpsOption) {
|
|
64
|
-
const { genHttpsOptions } = require('@/dev-tools/https');
|
|
65
|
-
const httpsOptions = await genHttpsOptions(devHttpsOption);
|
|
66
|
-
this.app = createHttpsServer(httpsOptions, this.getRequestHandler());
|
|
67
|
-
} else {
|
|
68
|
-
this.app = createServer(this.getRequestHandler());
|
|
69
|
-
}
|
|
70
50
|
} else {
|
|
71
51
|
this.server = this.createProdServer();
|
|
72
|
-
this.app = createServer(this.getRequestHandler());
|
|
73
52
|
}
|
|
53
|
+
// check if https is configured when start dev server
|
|
54
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
55
|
+
|
|
56
|
+
this.runner = await this.createHookRunner();
|
|
57
|
+
|
|
58
|
+
// runner can only be used after server init
|
|
59
|
+
await this.server.init(this.runner);
|
|
74
60
|
|
|
75
|
-
await this.server.init();
|
|
76
61
|
return this;
|
|
77
62
|
}
|
|
78
63
|
|
|
@@ -103,28 +88,66 @@ export class Server {
|
|
|
103
88
|
const { options } = this;
|
|
104
89
|
|
|
105
90
|
if (options.apiOnly) {
|
|
106
|
-
|
|
107
|
-
return new APIModernServer(options, this.runner);
|
|
91
|
+
return new APIModernServer(options);
|
|
108
92
|
} else if (options.webOnly) {
|
|
109
|
-
|
|
110
|
-
return new WebModernServer(options, this.runner);
|
|
93
|
+
return new WebModernServer(options);
|
|
111
94
|
} else {
|
|
112
|
-
return new ModernServer(options
|
|
95
|
+
return new ModernServer(options);
|
|
113
96
|
}
|
|
114
97
|
}
|
|
115
98
|
|
|
116
99
|
private createDevServer() {
|
|
117
100
|
const { options } = this;
|
|
101
|
+
const {
|
|
102
|
+
APIModernDevServer,
|
|
103
|
+
WebModernDevServer,
|
|
104
|
+
ModernDevServer,
|
|
105
|
+
} = require('./dev-server');
|
|
118
106
|
|
|
119
107
|
if (options.apiOnly) {
|
|
120
|
-
|
|
121
|
-
return new APIModernDevServer(options, this.runner);
|
|
108
|
+
return new APIModernDevServer(options);
|
|
122
109
|
} else if (options.webOnly) {
|
|
123
|
-
|
|
124
|
-
return new WebModernDevServer(options, this.runner);
|
|
110
|
+
return new WebModernDevServer(options);
|
|
125
111
|
} else {
|
|
126
|
-
|
|
127
|
-
return new ModernDevServer(options, this.runner);
|
|
112
|
+
return new ModernDevServer(options);
|
|
128
113
|
}
|
|
129
114
|
}
|
|
115
|
+
|
|
116
|
+
private async createHookRunner() {
|
|
117
|
+
const { options } = this;
|
|
118
|
+
const appContext = await this.initAppContext();
|
|
119
|
+
serverManager.run(() => {
|
|
120
|
+
AppContext.set({
|
|
121
|
+
...appContext,
|
|
122
|
+
distDirectory: path.join(
|
|
123
|
+
options.pwd,
|
|
124
|
+
options.config.output.path || 'dist',
|
|
125
|
+
),
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
options.plugins?.forEach(p => {
|
|
130
|
+
serverManager.usePlugin(p);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return serverManager.init({});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private async initAppContext() {
|
|
137
|
+
const appDirectory = await initAppDir();
|
|
138
|
+
|
|
139
|
+
const loaded = await loadUserConfig(appDirectory);
|
|
140
|
+
|
|
141
|
+
const plugins = this.options.plugins?.map(p => ({
|
|
142
|
+
server: p,
|
|
143
|
+
cli: undefined,
|
|
144
|
+
}));
|
|
145
|
+
|
|
146
|
+
const appContext = initAppContext(
|
|
147
|
+
appDirectory,
|
|
148
|
+
plugins || [],
|
|
149
|
+
loaded.filePath,
|
|
150
|
+
);
|
|
151
|
+
return appContext;
|
|
152
|
+
}
|
|
130
153
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
2
|
import { ModernServer } from './modern-server';
|
|
3
3
|
import { mergeExtension } from '@/utils';
|
|
4
4
|
import { ModernRouteInterface } from '@/libs/route';
|
|
@@ -23,21 +23,23 @@ export class WebModernServer extends ModernServer {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export class
|
|
27
|
-
protected
|
|
28
|
-
_m: ApiServerMode,
|
|
29
|
-
_: ReturnType<typeof mergeExtension>,
|
|
30
|
-
) {
|
|
26
|
+
export class APIModernServer extends ModernServer {
|
|
27
|
+
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>) {
|
|
31
28
|
return null as any;
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
protected async
|
|
35
|
-
|
|
31
|
+
protected async prepareAPIHandler(
|
|
32
|
+
mode: ApiServerMode,
|
|
33
|
+
extension: APIServerStartInput['config'],
|
|
36
34
|
) {
|
|
37
|
-
return super.
|
|
35
|
+
return super.prepareAPIHandler(mode, extension);
|
|
38
36
|
}
|
|
39
37
|
|
|
40
38
|
protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
41
|
-
return routes.filter(route => route.
|
|
39
|
+
return routes.filter(route => route.isApi);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected async preServerInit() {
|
|
43
|
+
// noop
|
|
42
44
|
}
|
|
43
45
|
}
|