@modern-js/server 1.1.2 → 1.1.4-rc.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 +46 -0
- package/dist/js/modern/dev-tools/babel/register.js +2 -2
- package/dist/js/modern/dev-tools/mock/getMockData.js +2 -2
- 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/proxy.js +2 -2
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +2 -2
- package/dist/js/modern/libs/render/cache/index.js +2 -2
- 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 +4 -0
- package/dist/js/modern/server/dev-server/dev-server-split.js +28 -0
- 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 +64 -62
- package/dist/js/modern/server/modern-server-split.js +81 -0
- package/dist/js/modern/server/modern-server.js +117 -59
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +11 -3
- package/dist/js/node/dev-tools/babel/register.js +2 -2
- package/dist/js/node/dev-tools/mock/getMockData.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/proxy.js +2 -2
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +2 -2
- package/dist/js/node/libs/render/cache/index.js +2 -2
- package/dist/js/node/libs/route/index.js +4 -0
- package/dist/js/node/libs/route/matcher.js +4 -0
- package/dist/js/node/server/dev-server/dev-server-split.js +41 -0
- 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 +71 -63
- package/dist/js/node/server/modern-server-split.js +97 -0
- package/dist/js/node/server/modern-server.js +120 -60
- package/dist/js/node/utils.js +14 -4
- 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/server/dev-server/dev-server-split.d.ts +15 -0
- 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/modern-server-split.d.ts +26 -0
- package/dist/types/server/modern-server.d.ts +19 -11
- package/dist/types/type.d.ts +5 -0
- package/dist/types/utils.d.ts +3 -2
- package/package.json +12 -11
- 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 +4 -0
- package/src/server/{web-server.ts → dev-server/dev-server-split.ts} +12 -14
- package/src/server/{dev-server.ts → dev-server/dev-server.ts} +62 -33
- package/src/server/dev-server/index.ts +2 -0
- package/src/server/index.ts +80 -47
- package/src/server/modern-server-split.ts +97 -0
- package/src/server/modern-server.ts +135 -81
- package/src/type.ts +5 -0
- package/src/utils.ts +16 -2
- package/dist/js/modern/server/api-server.js +0 -36
- package/dist/js/modern/server/web-server.js +0 -30
- package/dist/js/node/server/api-server.js +0 -50
- package/dist/js/node/server/web-server.js +0 -44
- package/dist/types/server/api-server.d.ts +0 -17
- package/dist/types/server/web-server.d.ts +0 -15
- package/src/server/api-server.ts +0 -47
|
@@ -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)
|
|
@@ -1,13 +1,13 @@
|
|
|
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
|
|
7
|
+
export class ModernSSRDevServer extends ModernDevServer {
|
|
8
8
|
protected prepareAPIHandler(
|
|
9
9
|
_m: ApiServerMode,
|
|
10
|
-
_:
|
|
10
|
+
_: APIServerStartInput['config'],
|
|
11
11
|
) {
|
|
12
12
|
return null as any;
|
|
13
13
|
}
|
|
@@ -23,21 +23,19 @@ export class WebModernServer extends ModernServer {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export class
|
|
27
|
-
protected prepareAPIHandler(
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
export class ModernAPIDevServer extends ModernDevServer {
|
|
27
|
+
protected async prepareAPIHandler(
|
|
28
|
+
mode: ApiServerMode,
|
|
29
|
+
extension: APIServerStartInput['config'],
|
|
30
30
|
) {
|
|
31
|
-
return
|
|
31
|
+
return super.prepareAPIHandler(mode, extension);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
protected
|
|
35
|
-
|
|
36
|
-
) {
|
|
37
|
-
return super.prepareWebHandler(extension);
|
|
34
|
+
protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
35
|
+
return routes.filter(route => route.isApi);
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
protected
|
|
41
|
-
|
|
38
|
+
protected async preServerInit() {
|
|
39
|
+
// noop
|
|
42
40
|
}
|
|
43
41
|
}
|
|
@@ -1,27 +1,36 @@
|
|
|
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
|
-
import webpackDevMiddleware
|
|
6
|
-
|
|
7
|
-
} from '
|
|
8
|
-
import {
|
|
9
|
-
import { createProxyHandler, ProxyOptions } from '../libs/proxy';
|
|
16
|
+
import webpackDevMiddleware from 'webpack-dev-middleware';
|
|
17
|
+
import { ModernServer } from '../modern-server';
|
|
18
|
+
import { createMockHandler } from '@/dev-tools/mock';
|
|
19
|
+
import { createProxyHandler, ProxyOptions } from '@/libs/proxy';
|
|
10
20
|
import {
|
|
11
21
|
DevServerOptions,
|
|
12
22
|
ModernServerOptions,
|
|
13
23
|
NextFunction,
|
|
14
24
|
ServerHookRunner,
|
|
15
25
|
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';
|
|
26
|
+
} from '@/type';
|
|
27
|
+
import SocketServer from '@/dev-tools/socket-server';
|
|
28
|
+
import DevServerPlugin from '@/dev-tools/dev-server-plugin';
|
|
29
|
+
import { ModernServerContext } from '@/libs/context';
|
|
30
|
+
import { createLaunchEditorHandler } from '@/dev-tools/launch-editor';
|
|
31
|
+
import { enableRegister } from '@/dev-tools/babel/register';
|
|
32
|
+
import * as reader from '@/libs/render/reader';
|
|
33
|
+
import Watcher from '@/dev-tools/watcher';
|
|
25
34
|
import { AGGRED_DIR } from '@/constants';
|
|
26
35
|
|
|
27
36
|
const DEFAULT_DEV_OPTIONS: DevServerOptions = {
|
|
@@ -50,15 +59,13 @@ export class ModernDevServer extends ModernServer {
|
|
|
50
59
|
|
|
51
60
|
private watcher!: Watcher;
|
|
52
61
|
|
|
53
|
-
private devMiddleware!:
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
next: NextFunction,
|
|
58
|
-
) => void);
|
|
62
|
+
private devMiddleware!: webpackDevMiddleware.API<
|
|
63
|
+
http.IncomingMessage,
|
|
64
|
+
http.ServerResponse
|
|
65
|
+
>;
|
|
59
66
|
|
|
60
|
-
constructor(options: ModernServerOptions
|
|
61
|
-
super(options
|
|
67
|
+
constructor(options: ModernServerOptions) {
|
|
68
|
+
super(options);
|
|
62
69
|
|
|
63
70
|
// set webpack compiler
|
|
64
71
|
this.compiler = options.compiler!;
|
|
@@ -66,14 +73,14 @@ export class ModernDevServer extends ModernServer {
|
|
|
66
73
|
// set dev server options, like webpack-dev-server
|
|
67
74
|
this.dev =
|
|
68
75
|
typeof options.dev === 'boolean' ? DEFAULT_DEV_OPTIONS : options.dev!;
|
|
76
|
+
|
|
77
|
+
enableRegister(this.pwd, this.conf);
|
|
69
78
|
}
|
|
70
79
|
|
|
71
80
|
// Complete the preparation of services
|
|
72
|
-
public async init() {
|
|
81
|
+
public async init(runner: ServerHookRunner) {
|
|
73
82
|
const { conf, pwd, compiler } = this;
|
|
74
83
|
|
|
75
|
-
enableRegister(pwd, conf);
|
|
76
|
-
|
|
77
84
|
// mock handler
|
|
78
85
|
this.mockHandler = createMockHandler({ pwd });
|
|
79
86
|
this.addHandler((ctx: ModernServerContext, next: NextFunction) => {
|
|
@@ -110,7 +117,7 @@ export class ModernDevServer extends ModernServer {
|
|
|
110
117
|
this.addHandler(devMiddlewareHandler);
|
|
111
118
|
}
|
|
112
119
|
|
|
113
|
-
await super.init();
|
|
120
|
+
await super.init(runner);
|
|
114
121
|
|
|
115
122
|
// watch mock/ server/ api/ dir file change
|
|
116
123
|
this.startWatcher();
|
|
@@ -125,6 +132,8 @@ export class ModernDevServer extends ModernServer {
|
|
|
125
132
|
|
|
126
133
|
// reset static file
|
|
127
134
|
reader.updateFile();
|
|
135
|
+
|
|
136
|
+
this.runner.reset();
|
|
128
137
|
}
|
|
129
138
|
|
|
130
139
|
public onListening(app: Server) {
|
|
@@ -141,6 +150,24 @@ export class ModernDevServer extends ModernServer {
|
|
|
141
150
|
});
|
|
142
151
|
}
|
|
143
152
|
|
|
153
|
+
public async createHTTPServer(
|
|
154
|
+
handler: (
|
|
155
|
+
req: IncomingMessage,
|
|
156
|
+
res: ServerResponse,
|
|
157
|
+
next?: () => void,
|
|
158
|
+
) => void,
|
|
159
|
+
) {
|
|
160
|
+
const { dev } = this;
|
|
161
|
+
const devHttpsOption = typeof dev === 'object' && dev.https;
|
|
162
|
+
if (devHttpsOption) {
|
|
163
|
+
const { genHttpsOptions } = require('@/dev-tools/https');
|
|
164
|
+
const httpsOptions = await genHttpsOptions(devHttpsOption);
|
|
165
|
+
return createHttpsServer(httpsOptions, handler);
|
|
166
|
+
} else {
|
|
167
|
+
return createServer(handler);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
144
171
|
// set up plugin to each compiler
|
|
145
172
|
// register hooks for each compilation, update socket stats if recompiled
|
|
146
173
|
// start dev middleware
|
|
@@ -208,7 +235,7 @@ export class ModernDevServer extends ModernServer {
|
|
|
208
235
|
const bundles = this.router.getBundles();
|
|
209
236
|
|
|
210
237
|
bundles.forEach(bundle => {
|
|
211
|
-
const filepath = path.join(distDir, bundle
|
|
238
|
+
const filepath = path.join(distDir, bundle as string);
|
|
212
239
|
if (require.cache[filepath]) {
|
|
213
240
|
delete require.cache[filepath];
|
|
214
241
|
}
|
|
@@ -217,12 +244,12 @@ export class ModernDevServer extends ModernServer {
|
|
|
217
244
|
|
|
218
245
|
private startWatcher() {
|
|
219
246
|
const { pwd } = this;
|
|
220
|
-
const { mock
|
|
247
|
+
const { mock } = AGGRED_DIR;
|
|
221
248
|
const defaultWatched = [
|
|
222
249
|
`${pwd}/${mock}/**/*`,
|
|
223
|
-
`${pwd}/${
|
|
224
|
-
`${pwd}/${
|
|
225
|
-
`${pwd}/${
|
|
250
|
+
`${pwd}/${SERVER_DIR}/**/*`,
|
|
251
|
+
`${pwd}/${API_DIR}/!(typings)/**`,
|
|
252
|
+
`${pwd}/${SHARED_DIR}/**/*`,
|
|
226
253
|
];
|
|
227
254
|
|
|
228
255
|
const watcher = new Watcher();
|
|
@@ -233,6 +260,8 @@ export class ModernDevServer extends ModernServer {
|
|
|
233
260
|
watcher.updateDepTree();
|
|
234
261
|
watcher.cleanDepCache(filepath);
|
|
235
262
|
|
|
263
|
+
this.runner.reset();
|
|
264
|
+
|
|
236
265
|
if (filepath.startsWith(`${pwd}/${mock}`)) {
|
|
237
266
|
this.mockHandler = createMockHandler({ pwd });
|
|
238
267
|
} else {
|
package/src/server/index.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
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 {
|
|
16
|
+
ModernAPIServer,
|
|
17
|
+
ModernSSRServer,
|
|
18
|
+
ModernWebServer,
|
|
19
|
+
} from './modern-server-split';
|
|
20
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '@/type';
|
|
13
21
|
import { measure as defaultMeasure } from '@/libs/measure';
|
|
14
22
|
|
|
15
23
|
export class Server {
|
|
@@ -23,9 +31,6 @@ export class Server {
|
|
|
23
31
|
|
|
24
32
|
constructor(options: ModernServerOptions) {
|
|
25
33
|
this.options = options;
|
|
26
|
-
options.plugins?.forEach(p => {
|
|
27
|
-
serverManager.usePlugin(p);
|
|
28
|
-
});
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
public getRequestHandler() {
|
|
@@ -41,38 +46,24 @@ export class Server {
|
|
|
41
46
|
|
|
42
47
|
public async init() {
|
|
43
48
|
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
49
|
|
|
54
|
-
options.logger = options.logger ||
|
|
55
|
-
options.measure = options.measure ||
|
|
50
|
+
options.logger = options.logger || defaultLogger;
|
|
51
|
+
options.measure = options.measure || defaultMeasure;
|
|
56
52
|
|
|
53
|
+
// initialize server
|
|
57
54
|
if (options.dev) {
|
|
58
55
|
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
56
|
} else {
|
|
71
57
|
this.server = this.createProdServer();
|
|
72
|
-
this.app = createServer(this.getRequestHandler());
|
|
73
58
|
}
|
|
59
|
+
// check if https is configured when start dev server
|
|
60
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
61
|
+
|
|
62
|
+
this.runner = await this.createHookRunner();
|
|
63
|
+
|
|
64
|
+
// runner can only be used after server init
|
|
65
|
+
await this.server.init(this.runner);
|
|
74
66
|
|
|
75
|
-
await this.server.init();
|
|
76
67
|
return this;
|
|
77
68
|
}
|
|
78
69
|
|
|
@@ -103,28 +94,70 @@ export class Server {
|
|
|
103
94
|
const { options } = this;
|
|
104
95
|
|
|
105
96
|
if (options.apiOnly) {
|
|
106
|
-
|
|
107
|
-
|
|
97
|
+
return new ModernAPIServer(options);
|
|
98
|
+
} else if (options.ssrOnly) {
|
|
99
|
+
return new ModernSSRServer(options);
|
|
108
100
|
} else if (options.webOnly) {
|
|
109
|
-
|
|
110
|
-
return new WebModernServer(options, this.runner);
|
|
101
|
+
return new ModernWebServer(options);
|
|
111
102
|
} else {
|
|
112
|
-
return new ModernServer(options
|
|
103
|
+
return new ModernServer(options);
|
|
113
104
|
}
|
|
114
105
|
}
|
|
115
106
|
|
|
116
107
|
private createDevServer() {
|
|
117
108
|
const { options } = this;
|
|
109
|
+
const {
|
|
110
|
+
ModernAPIDevServer,
|
|
111
|
+
ModernSSRDevServer,
|
|
112
|
+
ModernDevServer,
|
|
113
|
+
} = require('./dev-server');
|
|
118
114
|
|
|
119
115
|
if (options.apiOnly) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const { WebModernDevServer } = require('./web-server');
|
|
124
|
-
return new WebModernDevServer(options, this.runner);
|
|
116
|
+
return new ModernAPIDevServer(options);
|
|
117
|
+
} else if (options.ssrOnly) {
|
|
118
|
+
return new ModernSSRDevServer(options);
|
|
125
119
|
} else {
|
|
126
|
-
|
|
127
|
-
return new ModernDevServer(options, this.runner);
|
|
120
|
+
return new ModernDevServer(options);
|
|
128
121
|
}
|
|
129
122
|
}
|
|
123
|
+
|
|
124
|
+
private async createHookRunner() {
|
|
125
|
+
const { options } = this;
|
|
126
|
+
|
|
127
|
+
options.plugins?.forEach(p => {
|
|
128
|
+
serverManager.usePlugin(p);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const appContext = await this.initAppContext();
|
|
132
|
+
serverManager.run(() => {
|
|
133
|
+
ConfigContext.set(this.options.config as UserConfig);
|
|
134
|
+
AppContext.set({
|
|
135
|
+
...appContext,
|
|
136
|
+
distDirectory: path.join(
|
|
137
|
+
options.pwd,
|
|
138
|
+
options.config.output.path || 'dist',
|
|
139
|
+
),
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return serverManager.init({});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private async initAppContext() {
|
|
147
|
+
const appDirectory = await initAppDir();
|
|
148
|
+
|
|
149
|
+
const loaded = await loadUserConfig(appDirectory);
|
|
150
|
+
|
|
151
|
+
const plugins = this.options.plugins?.map(p => ({
|
|
152
|
+
server: p,
|
|
153
|
+
cli: undefined,
|
|
154
|
+
}));
|
|
155
|
+
|
|
156
|
+
const appContext = initAppContext(
|
|
157
|
+
appDirectory,
|
|
158
|
+
plugins || [],
|
|
159
|
+
loaded.filePath,
|
|
160
|
+
);
|
|
161
|
+
return appContext;
|
|
162
|
+
}
|
|
130
163
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
+
import { ModernServer } from './modern-server';
|
|
3
|
+
import { mergeExtension } from '@/utils';
|
|
4
|
+
import { ModernRoute, ModernRouteInterface, RouteMatcher } from '@/libs/route';
|
|
5
|
+
import { ApiServerMode } from '@/constants';
|
|
6
|
+
import { ModernServerContext } from '@/libs/context';
|
|
7
|
+
|
|
8
|
+
export class ModernSSRServer extends ModernServer {
|
|
9
|
+
// Todo should not invoke any route hook in modernSSRServer
|
|
10
|
+
|
|
11
|
+
protected async warmupSSRBundle() {
|
|
12
|
+
// empty
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected verifyMatch(context: ModernServerContext, matched: RouteMatcher) {
|
|
16
|
+
if (matched.generate().isApi) {
|
|
17
|
+
this.render404(context);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected prepareAPIHandler(
|
|
22
|
+
_m: ApiServerMode,
|
|
23
|
+
_: APIServerStartInput['config'],
|
|
24
|
+
) {
|
|
25
|
+
return null as any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected async prepareWebHandler(
|
|
29
|
+
extension: ReturnType<typeof mergeExtension>,
|
|
30
|
+
) {
|
|
31
|
+
return super.prepareWebHandler(extension);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
35
|
+
// return routes.filter(route => route.entryName);
|
|
36
|
+
// }
|
|
37
|
+
|
|
38
|
+
protected async preServerInit() {
|
|
39
|
+
// empty
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class ModernAPIServer extends ModernServer {
|
|
44
|
+
protected async emitRouteHook(_: string, _input: any) {
|
|
45
|
+
// empty
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected async warmupSSRBundle() {
|
|
49
|
+
// empty
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>) {
|
|
53
|
+
return null as any;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected async prepareAPIHandler(
|
|
57
|
+
mode: ApiServerMode,
|
|
58
|
+
extension: APIServerStartInput['config'],
|
|
59
|
+
) {
|
|
60
|
+
return super.prepareAPIHandler(mode, extension);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected filterRoutes(routes: ModernRouteInterface[]) {
|
|
64
|
+
return routes.filter(route => route.isApi);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected async preServerInit() {
|
|
68
|
+
// empty
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class ModernWebServer extends ModernServer {
|
|
73
|
+
protected async warmupSSRBundle() {
|
|
74
|
+
// empty
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected async handleAPI(context: ModernServerContext) {
|
|
78
|
+
const { proxyTarget } = this;
|
|
79
|
+
if (!proxyTarget?.api) {
|
|
80
|
+
this.proxy();
|
|
81
|
+
} else {
|
|
82
|
+
this.render404(context);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected async handleWeb(context: ModernServerContext, route: ModernRoute) {
|
|
87
|
+
const { proxyTarget } = this;
|
|
88
|
+
|
|
89
|
+
if (route.isSSR && proxyTarget?.ssr) {
|
|
90
|
+
return this.proxy();
|
|
91
|
+
} else {
|
|
92
|
+
// if no proxyTarget but access web server, degradation to csr
|
|
93
|
+
route.isSSR = false;
|
|
94
|
+
return super.handleWeb(context, route);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|