@modern-js/server 1.1.2-beta.0 → 1.1.3
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 +41 -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/render/cache/type.js +0 -1
- 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 +62 -62
- package/dist/js/modern/server/{api-server.js → modern-server-split.js} +6 -10
- package/dist/js/modern/server/modern-server.js +76 -27
- package/dist/js/modern/type.js +0 -1
- 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 +69 -63
- package/dist/js/node/server/{web-server.js → modern-server-split.js} +10 -9
- package/dist/js/node/server/modern-server.js +78 -27
- 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/{web-server.d.ts → dev-server/dev-server-split.d.ts} +8 -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/{api-server.d.ts → modern-server-split.d.ts} +4 -6
- package/dist/types/server/modern-server.d.ts +5 -3
- package/dist/types/utils.d.ts +1 -1
- package/package.json +17 -19
- 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/{web-server.ts → dev-server/dev-server-split.ts} +11 -10
- 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 +72 -46
- package/src/server/{api-server.ts → modern-server-split.ts} +10 -13
- package/src/server/modern-server.ts +82 -41
- 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",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -26,14 +26,6 @@
|
|
|
26
26
|
"default": "./dist/js/treeshaking/index.js"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
-
"scripts": {
|
|
30
|
-
"prepare": "pnpm build",
|
|
31
|
-
"prepublishOnly": "pnpm build --platform",
|
|
32
|
-
"new": "modern new",
|
|
33
|
-
"build": "modern build",
|
|
34
|
-
"dev": "modern build --watch",
|
|
35
|
-
"test": "modern test --passWithNoTests"
|
|
36
|
-
},
|
|
37
29
|
"dependencies": {
|
|
38
30
|
"@babel/core": "^7.15.0",
|
|
39
31
|
"@babel/compat-data": "^7.15.0",
|
|
@@ -41,12 +33,12 @@
|
|
|
41
33
|
"@babel/preset-typescript": "^7.15.0",
|
|
42
34
|
"@babel/register": "^7.15.3",
|
|
43
35
|
"@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": "
|
|
36
|
+
"@modern-js/core": "^1.1.3",
|
|
37
|
+
"@modern-js/hmr-client": "^1.1.1",
|
|
38
|
+
"@modern-js/server-plugin": "^1.1.2",
|
|
39
|
+
"@modern-js/server-utils": "^1.1.2",
|
|
40
|
+
"@modern-js/bff-utils": "^1.1.1",
|
|
41
|
+
"@modern-js/utils": "^1.1.3",
|
|
50
42
|
"axios": "^0.21.4",
|
|
51
43
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
52
44
|
"chokidar": "^3.5.2",
|
|
@@ -69,9 +61,9 @@
|
|
|
69
61
|
"ws": "^8.2.0"
|
|
70
62
|
},
|
|
71
63
|
"devDependencies": {
|
|
72
|
-
"@modern-js/module-tools": "^1.1.
|
|
73
|
-
"@modern-js/plugin-testing": "^1.1.
|
|
74
|
-
"@modern-js/types": "
|
|
64
|
+
"@modern-js/module-tools": "^1.1.1",
|
|
65
|
+
"@modern-js/plugin-testing": "^1.1.1",
|
|
66
|
+
"@modern-js/types": "^1.1.2",
|
|
75
67
|
"@types/jest": "^26",
|
|
76
68
|
"@types/lru-cache": "^5.1.1",
|
|
77
69
|
"@types/mime-types": "^2.1.0",
|
|
@@ -100,5 +92,11 @@
|
|
|
100
92
|
"publishConfig": {
|
|
101
93
|
"registry": "https://registry.npmjs.org/",
|
|
102
94
|
"access": "public"
|
|
95
|
+
},
|
|
96
|
+
"scripts": {
|
|
97
|
+
"new": "modern new",
|
|
98
|
+
"build": "modern build",
|
|
99
|
+
"dev": "modern build --watch",
|
|
100
|
+
"test": "modern test --passWithNoTests"
|
|
103
101
|
}
|
|
104
|
-
}
|
|
102
|
+
}
|
|
@@ -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,11 +1,10 @@
|
|
|
1
1
|
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
2
|
import { ModernDevServer } from './dev-server';
|
|
3
|
-
import { ModernServer } from './modern-server';
|
|
4
3
|
import { mergeExtension } from '@/utils';
|
|
5
4
|
import { ModernRouteInterface } from '@/libs/route';
|
|
6
5
|
import { ApiServerMode } from '@/constants';
|
|
7
6
|
|
|
8
|
-
export class
|
|
7
|
+
export class WebModernDevServer extends ModernDevServer {
|
|
9
8
|
protected prepareAPIHandler(
|
|
10
9
|
_m: ApiServerMode,
|
|
11
10
|
_: APIServerStartInput['config'],
|
|
@@ -24,21 +23,23 @@ export class WebModernServer extends ModernServer {
|
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
export class
|
|
28
|
-
protected
|
|
29
|
-
_m: ApiServerMode,
|
|
30
|
-
_: APIServerStartInput['config'],
|
|
31
|
-
) {
|
|
26
|
+
export class APIModernDevServer extends ModernDevServer {
|
|
27
|
+
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>) {
|
|
32
28
|
return null as any;
|
|
33
29
|
}
|
|
34
30
|
|
|
35
|
-
protected async
|
|
31
|
+
protected async prepareAPIHandler(
|
|
32
|
+
mode: ApiServerMode,
|
|
36
33
|
extension: ReturnType<typeof mergeExtension>,
|
|
37
34
|
) {
|
|
38
|
-
return super.
|
|
35
|
+
return super.prepareAPIHandler(mode, extension);
|
|
39
36
|
}
|
|
40
37
|
|
|
41
38
|
protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
42
|
-
return routes.filter(route => route.
|
|
39
|
+
return routes.filter(route => route.isApi);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected async preServerInit() {
|
|
43
|
+
// noop
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -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}/!(typings)/**`,
|
|
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,19 @@
|
|
|
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
|
+
ConfigContext,
|
|
11
|
+
UserConfig,
|
|
12
|
+
} from '@modern-js/core';
|
|
11
13
|
import { ModernServer } from './modern-server';
|
|
12
14
|
import type { ModernDevServer } from './dev-server';
|
|
15
|
+
import { APIModernServer, WebModernServer } from './modern-server-split';
|
|
16
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '@/type';
|
|
13
17
|
import { measure as defaultMeasure } from '@/libs/measure';
|
|
14
18
|
|
|
15
19
|
export class Server {
|
|
@@ -23,9 +27,6 @@ export class Server {
|
|
|
23
27
|
|
|
24
28
|
constructor(options: ModernServerOptions) {
|
|
25
29
|
this.options = options;
|
|
26
|
-
options.plugins?.forEach(p => {
|
|
27
|
-
serverManager.usePlugin(p);
|
|
28
|
-
});
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
public getRequestHandler() {
|
|
@@ -41,38 +42,24 @@ export class Server {
|
|
|
41
42
|
|
|
42
43
|
public async init() {
|
|
43
44
|
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
45
|
|
|
54
|
-
options.logger = options.logger ||
|
|
55
|
-
options.measure = options.measure ||
|
|
46
|
+
options.logger = options.logger || defaultLogger;
|
|
47
|
+
options.measure = options.measure || defaultMeasure;
|
|
56
48
|
|
|
49
|
+
// initialize server
|
|
57
50
|
if (options.dev) {
|
|
58
51
|
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
52
|
} else {
|
|
71
53
|
this.server = this.createProdServer();
|
|
72
|
-
this.app = createServer(this.getRequestHandler());
|
|
73
54
|
}
|
|
55
|
+
// check if https is configured when start dev server
|
|
56
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
57
|
+
|
|
58
|
+
this.runner = await this.createHookRunner();
|
|
59
|
+
|
|
60
|
+
// runner can only be used after server init
|
|
61
|
+
await this.server.init(this.runner);
|
|
74
62
|
|
|
75
|
-
await this.server.init();
|
|
76
63
|
return this;
|
|
77
64
|
}
|
|
78
65
|
|
|
@@ -103,28 +90,67 @@ export class Server {
|
|
|
103
90
|
const { options } = this;
|
|
104
91
|
|
|
105
92
|
if (options.apiOnly) {
|
|
106
|
-
|
|
107
|
-
return new APIModernServer(options, this.runner);
|
|
93
|
+
return new APIModernServer(options);
|
|
108
94
|
} else if (options.webOnly) {
|
|
109
|
-
|
|
110
|
-
return new WebModernServer(options, this.runner);
|
|
95
|
+
return new WebModernServer(options);
|
|
111
96
|
} else {
|
|
112
|
-
return new ModernServer(options
|
|
97
|
+
return new ModernServer(options);
|
|
113
98
|
}
|
|
114
99
|
}
|
|
115
100
|
|
|
116
101
|
private createDevServer() {
|
|
117
102
|
const { options } = this;
|
|
103
|
+
const {
|
|
104
|
+
APIModernDevServer,
|
|
105
|
+
WebModernDevServer,
|
|
106
|
+
ModernDevServer,
|
|
107
|
+
} = require('./dev-server');
|
|
118
108
|
|
|
119
109
|
if (options.apiOnly) {
|
|
120
|
-
|
|
121
|
-
return new APIModernDevServer(options, this.runner);
|
|
110
|
+
return new APIModernDevServer(options);
|
|
122
111
|
} else if (options.webOnly) {
|
|
123
|
-
|
|
124
|
-
return new WebModernDevServer(options, this.runner);
|
|
112
|
+
return new WebModernDevServer(options);
|
|
125
113
|
} else {
|
|
126
|
-
|
|
127
|
-
return new ModernDevServer(options, this.runner);
|
|
114
|
+
return new ModernDevServer(options);
|
|
128
115
|
}
|
|
129
116
|
}
|
|
117
|
+
|
|
118
|
+
private async createHookRunner() {
|
|
119
|
+
const { options } = this;
|
|
120
|
+
const appContext = await this.initAppContext();
|
|
121
|
+
serverManager.run(() => {
|
|
122
|
+
ConfigContext.set(this.options.config as UserConfig);
|
|
123
|
+
AppContext.set({
|
|
124
|
+
...appContext,
|
|
125
|
+
distDirectory: path.join(
|
|
126
|
+
options.pwd,
|
|
127
|
+
options.config.output.path || 'dist',
|
|
128
|
+
),
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
options.plugins?.forEach(p => {
|
|
133
|
+
serverManager.usePlugin(p);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return serverManager.init({});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private async initAppContext() {
|
|
140
|
+
const appDirectory = await initAppDir();
|
|
141
|
+
|
|
142
|
+
const loaded = await loadUserConfig(appDirectory);
|
|
143
|
+
|
|
144
|
+
const plugins = this.options.plugins?.map(p => ({
|
|
145
|
+
server: p,
|
|
146
|
+
cli: undefined,
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
const appContext = initAppContext(
|
|
150
|
+
appDirectory,
|
|
151
|
+
plugins || [],
|
|
152
|
+
loaded.filePath,
|
|
153
|
+
);
|
|
154
|
+
return appContext;
|
|
155
|
+
}
|
|
130
156
|
}
|
|
@@ -1,32 +1,29 @@
|
|
|
1
1
|
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
-
import { ModernDevServer } from './dev-server';
|
|
3
2
|
import { ModernServer } from './modern-server';
|
|
4
3
|
import { mergeExtension } from '@/utils';
|
|
5
4
|
import { ModernRouteInterface } from '@/libs/route';
|
|
6
5
|
import { ApiServerMode } from '@/constants';
|
|
7
6
|
|
|
8
|
-
export class
|
|
9
|
-
protected
|
|
7
|
+
export class WebModernServer extends ModernServer {
|
|
8
|
+
protected prepareAPIHandler(
|
|
9
|
+
_m: ApiServerMode,
|
|
10
|
+
_: ReturnType<typeof mergeExtension>,
|
|
11
|
+
) {
|
|
10
12
|
return null as any;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
protected async
|
|
14
|
-
|
|
15
|
-
extension: APIServerStartInput['config'],
|
|
15
|
+
protected async prepareWebHandler(
|
|
16
|
+
extension: ReturnType<typeof mergeExtension>,
|
|
16
17
|
) {
|
|
17
|
-
return super.
|
|
18
|
+
return super.prepareWebHandler(extension);
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
21
|
-
return routes.filter(route => route.
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
protected async preServerInit() {
|
|
25
|
-
// noop
|
|
22
|
+
return routes.filter(route => route.entryName);
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
export class
|
|
26
|
+
export class APIModernServer extends ModernServer {
|
|
30
27
|
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>) {
|
|
31
28
|
return null as any;
|
|
32
29
|
}
|