@modern-js/server 1.1.2 → 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/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 +4 -0
- 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 +48 -20
- 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 +4 -0
- 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 +50 -20
- 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/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 +6 -5
- package/dist/types/utils.d.ts +1 -1
- package/package.json +19 -17
- 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/{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 +54 -34
- package/src/utils.ts +2 -2
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
1
2
|
import { ModernDevServer } from './dev-server';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
protected prepareWebHandler(
|
|
8
|
-
protected prepareAPIHandler(mode: ApiServerMode, extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
3
|
+
import { mergeExtension } from "../../utils.d";
|
|
4
|
+
import { ModernRouteInterface } from "../../libs/route";
|
|
5
|
+
import { ApiServerMode } from "../../constants.d";
|
|
6
|
+
export declare class WebModernDevServer extends ModernDevServer {
|
|
7
|
+
protected prepareAPIHandler(_m: ApiServerMode, _: APIServerStartInput['config']): any;
|
|
8
|
+
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
9
9
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
10
|
-
protected preServerInit(): Promise<void>;
|
|
11
10
|
}
|
|
12
11
|
export declare class APIModernDevServer extends ModernDevServer {
|
|
13
12
|
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Server } from 'http';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import http, { Server, IncomingMessage, ServerResponse } from 'http';
|
|
3
|
+
import { ModernServer } from '../modern-server';
|
|
4
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from "../../type.d";
|
|
5
5
|
export declare class ModernDevServer extends ModernServer {
|
|
6
6
|
private devProxyHandler;
|
|
7
7
|
private mockHandler;
|
|
@@ -10,11 +10,12 @@ export declare class ModernDevServer extends ModernServer {
|
|
|
10
10
|
private socketServer;
|
|
11
11
|
private watcher;
|
|
12
12
|
private devMiddleware;
|
|
13
|
-
constructor(options: ModernServerOptions
|
|
14
|
-
init(): Promise<void>;
|
|
13
|
+
constructor(options: ModernServerOptions);
|
|
14
|
+
init(runner: ServerHookRunner): Promise<void>;
|
|
15
15
|
ready(options?: ReadyOptions): void;
|
|
16
16
|
onListening(app: Server): void;
|
|
17
17
|
close(): Promise<void>;
|
|
18
|
+
createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<http.Server | import("https").Server>;
|
|
18
19
|
private setupCompiler;
|
|
19
20
|
private setupDevServerPlugin;
|
|
20
21
|
private setupHooks;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, ServerResponse, Server as httpServer } from 'http';
|
|
3
|
-
import { ModernServerOptions, ReadyOptions } from
|
|
3
|
+
import { ModernServerOptions, ReadyOptions } from "../type.d";
|
|
4
4
|
export declare class Server {
|
|
5
5
|
options: ModernServerOptions;
|
|
6
6
|
private server;
|
|
@@ -15,4 +15,6 @@ export declare class Server {
|
|
|
15
15
|
close(): Promise<void>;
|
|
16
16
|
private createProdServer;
|
|
17
17
|
private createDevServer;
|
|
18
|
+
private createHookRunner;
|
|
19
|
+
private initAppContext;
|
|
18
20
|
}
|
|
@@ -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.d";
|
|
4
4
|
import { ModernRouteInterface } from "../libs/route";
|
|
@@ -8,8 +8,9 @@ export declare class WebModernServer extends ModernServer {
|
|
|
8
8
|
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
9
9
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
10
10
|
}
|
|
11
|
-
export declare class
|
|
12
|
-
protected
|
|
13
|
-
protected
|
|
11
|
+
export declare class APIModernServer extends ModernServer {
|
|
12
|
+
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
|
|
13
|
+
protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
14
14
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
15
|
+
protected preServerInit(): Promise<void>;
|
|
15
16
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, ServerResponse, Server } from 'http';
|
|
3
|
-
import { Adapter } from '@modern-js/server-plugin';
|
|
3
|
+
import { Adapter, APIServerStartInput } from '@modern-js/server-plugin';
|
|
4
4
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
5
5
|
import { ModernServerOptions, NextFunction, ServerHookRunner, Measure, Logger, ReadyOptions } from '../type';
|
|
6
6
|
import { RouteMatchManager, ModernRouteInterface } from '../libs/route';
|
|
@@ -17,9 +17,9 @@ export declare class ModernServer {
|
|
|
17
17
|
protected conf: NormalizedConfig;
|
|
18
18
|
protected handlers: ModernServerAsyncHandler[];
|
|
19
19
|
protected presetRoutes?: ModernRouteInterface[];
|
|
20
|
+
protected runner: ServerHookRunner;
|
|
20
21
|
protected readonly logger: Logger;
|
|
21
22
|
protected readonly measure: Measure;
|
|
22
|
-
private readonly runner;
|
|
23
23
|
private readonly isDev;
|
|
24
24
|
private staticFileHandler;
|
|
25
25
|
private routeRenderHandler;
|
|
@@ -36,19 +36,20 @@ export declare class ModernServer {
|
|
|
36
36
|
staticGenerate,
|
|
37
37
|
logger,
|
|
38
38
|
measure
|
|
39
|
-
}: ModernServerOptions
|
|
39
|
+
}: ModernServerOptions);
|
|
40
40
|
getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
41
|
-
init(): Promise<void>;
|
|
41
|
+
init(runner: ServerHookRunner): Promise<void>;
|
|
42
42
|
ready(_: ReadyOptions): void;
|
|
43
43
|
onListening(_: Server): void;
|
|
44
44
|
close(): void;
|
|
45
|
+
createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<Server>;
|
|
45
46
|
protected warmupSSRBundle(): void;
|
|
46
47
|
protected readRouteSpec(): ModernRouteInterface[];
|
|
47
48
|
protected addHandler(handler: ModernServerHandler): void;
|
|
48
49
|
protected render404(context: ModernServerContext): void;
|
|
49
50
|
protected prepareFrameHandler(): Promise<void>;
|
|
50
51
|
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<Adapter>;
|
|
51
|
-
protected prepareAPIHandler(mode: ApiServerMode, extension:
|
|
52
|
+
protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<Adapter>;
|
|
52
53
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
53
54
|
protected preServerInit(): Promise<void>;
|
|
54
55
|
private prepareFavicons;
|
package/dist/types/utils.d.ts
CHANGED
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",
|
|
@@ -26,6 +26,14 @@
|
|
|
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
|
+
},
|
|
29
37
|
"dependencies": {
|
|
30
38
|
"@babel/core": "^7.15.0",
|
|
31
39
|
"@babel/compat-data": "^7.15.0",
|
|
@@ -33,12 +41,12 @@
|
|
|
33
41
|
"@babel/preset-typescript": "^7.15.0",
|
|
34
42
|
"@babel/register": "^7.15.3",
|
|
35
43
|
"@babel/runtime": "^7",
|
|
36
|
-
"@modern-js/core": "
|
|
37
|
-
"@modern-js/hmr-client": "
|
|
38
|
-
"@modern-js/server-plugin": "
|
|
39
|
-
"@modern-js/server-utils": "
|
|
40
|
-
"@modern-js/bff-utils": "
|
|
41
|
-
"@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",
|
|
42
50
|
"axios": "^0.21.4",
|
|
43
51
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
44
52
|
"chokidar": "^3.5.2",
|
|
@@ -61,9 +69,9 @@
|
|
|
61
69
|
"ws": "^8.2.0"
|
|
62
70
|
},
|
|
63
71
|
"devDependencies": {
|
|
64
|
-
"@modern-js/module-tools": "^1.1.
|
|
65
|
-
"@modern-js/plugin-testing": "^1.1.
|
|
66
|
-
"@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",
|
|
67
75
|
"@types/jest": "^26",
|
|
68
76
|
"@types/lru-cache": "^5.1.1",
|
|
69
77
|
"@types/mime-types": "^2.1.0",
|
|
@@ -92,11 +100,5 @@
|
|
|
92
100
|
"publishConfig": {
|
|
93
101
|
"registry": "https://registry.npmjs.org/",
|
|
94
102
|
"access": "public"
|
|
95
|
-
},
|
|
96
|
-
"scripts": {
|
|
97
|
-
"new": "modern new",
|
|
98
|
-
"build": "modern build",
|
|
99
|
-
"dev": "modern build --watch",
|
|
100
|
-
"test": "modern test --passWithNoTests"
|
|
101
103
|
}
|
|
102
|
-
}
|
|
104
|
+
}
|
|
@@ -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,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
|
}
|