@modern-js/prod-server 1.0.6 → 1.1.1
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/.eslintrc.js +8 -0
- package/CHANGELOG.md +34 -0
- package/dist/js/modern/libs/loadConfig.js +54 -0
- package/dist/js/modern/libs/proxy.js +3 -0
- package/dist/js/modern/server/index.js +102 -14
- package/dist/js/modern/server/modern-server.js +7 -6
- package/dist/js/modern/utils.js +2 -0
- package/dist/js/node/libs/loadConfig.js +79 -0
- package/dist/js/node/libs/proxy.js +4 -0
- package/dist/js/node/server/index.js +107 -14
- package/dist/js/node/server/modern-server.js +6 -5
- package/dist/js/node/utils.js +6 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/libs/loadConfig.d.ts +19 -0
- package/dist/types/server/index.d.ts +31 -0
- package/dist/types/type.d.ts +2 -1
- package/dist/types/utils.d.ts +1 -0
- package/package.json +19 -17
- package/src/index.ts +1 -0
- package/src/libs/loadConfig.ts +65 -0
- package/src/libs/proxy.ts +2 -0
- package/src/server/index.ts +112 -7
- package/src/server/modern-server.ts +4 -3
- package/src/type.ts +2 -1
- package/src/utils.ts +3 -0
- package/tests/.eslintrc.js +0 -6
- package/tests/context.test.ts +0 -52
- package/tests/fixtures/hosting-files/static/index.js +0 -1
- package/tests/fixtures/pure/modern.config.js +0 -5
- package/tests/fixtures/pure/package.json +0 -21
- package/tests/fixtures/pure/src/App.css +0 -119
- package/tests/fixtures/pure/src/App.tsx +0 -43
- package/tests/fixtures/pure/test-dist/bundles/main.js +0 -5
- package/tests/fixtures/pure/test-dist/html/main/index.html +0 -36
- package/tests/fixtures/pure/test-dist/route.json +0 -31
- package/tests/fixtures/pure/tsconfig.json +0 -12
- package/tests/fixtures/reader/index.ts +0 -3
- package/tests/fixtures/route-spec/dynamic.json +0 -13
- package/tests/fixtures/route-spec/index.json +0 -29
- package/tests/fixtures/ssr/bundle-error.js +0 -3
- package/tests/fixtures/ssr/bundle.js +0 -5
- package/tests/fixtures/ssr/tpl.html +0 -11
- package/tests/fixtures/static-dir/bar.html +0 -11
- package/tests/fixtures/static-dir/baz/index.html +0 -11
- package/tests/fixtures/static-dir/foo/index.html +0 -11
- package/tests/helper.ts +0 -8
- package/tests/hook.test.ts +0 -44
- package/tests/middleware.test.ts +0 -179
- package/tests/render.test.ts +0 -206
- package/tests/route.test.ts +0 -77
- package/tests/server.test.ts +0 -288
- package/tests/spr.test.ts +0 -38
- package/tests/tsconfig.json +0 -12
- package/tests/utils.test.ts +0 -106
package/dist/js/node/utils.js
CHANGED
|
@@ -3,10 +3,15 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.toPath = exports.toMessage = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.getStaticReg = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
|
|
6
|
+
exports.toPath = exports.toMessage = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
|
|
7
7
|
|
|
8
8
|
var _pathToRegexp = require("path-to-regexp");
|
|
9
9
|
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
const debug = (0, _utils.createDebugger)('prod-server');
|
|
13
|
+
exports.debug = debug;
|
|
14
|
+
|
|
10
15
|
const mergeExtension = users => {
|
|
11
16
|
const output = [];
|
|
12
17
|
return {
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ModernServerOptions } from './type';
|
|
2
2
|
import { Server } from './server';
|
|
3
3
|
export { Server };
|
|
4
|
+
export type { ServerConfig } from '@modern-js/server-core';
|
|
4
5
|
export { ModernServer } from './server/modern-server';
|
|
5
6
|
export { createProxyHandler } from './libs/proxy';
|
|
6
7
|
export * from './type';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { NormalizedConfig } from '@modern-js/core';
|
|
2
|
+
import type { ServerConfig } from '@modern-js/server-core';
|
|
3
|
+
export declare const getServerConfigPath: (distDirectory: string, serverConfigFile?: string) => string;
|
|
4
|
+
export declare const requireConfig: (serverConfigPath: string) => any;
|
|
5
|
+
/**
|
|
6
|
+
* 对配置进行合并,开发环境下,cliConfig 与 serverConfig 进行深合并
|
|
7
|
+
* 生产环境下,resolvedConfig 与 serverConfig 进行深合并
|
|
8
|
+
* resolvedConfigPath: 构建序列化后的 modern.config.js 文件路径
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export declare const loadConfig: ({
|
|
12
|
+
cliConfig,
|
|
13
|
+
serverConfig,
|
|
14
|
+
resolvedConfigPath
|
|
15
|
+
}: {
|
|
16
|
+
cliConfig: NormalizedConfig;
|
|
17
|
+
serverConfig: ServerConfig;
|
|
18
|
+
resolvedConfigPath: string;
|
|
19
|
+
}) => any;
|
|
@@ -6,11 +6,42 @@ export declare class Server {
|
|
|
6
6
|
private server;
|
|
7
7
|
private app;
|
|
8
8
|
private runner;
|
|
9
|
+
private serverConfig;
|
|
9
10
|
constructor(options: ModernServerOptions);
|
|
11
|
+
/**
|
|
12
|
+
* 初始化顺序
|
|
13
|
+
* - 获取 server runtime config
|
|
14
|
+
* - 设置 context
|
|
15
|
+
* - 创建 hooksRunner
|
|
16
|
+
* - 合并插件,内置插件和 serverConfig 中配置的插件
|
|
17
|
+
* - 执行 config hook
|
|
18
|
+
* - 获取最终的配置
|
|
19
|
+
* - 设置配置到 context
|
|
20
|
+
* - 初始化 server
|
|
21
|
+
* - 执行 prepare hook
|
|
22
|
+
* - 执行 server init
|
|
23
|
+
*/
|
|
24
|
+
|
|
10
25
|
init(): Promise<this>;
|
|
26
|
+
/**
|
|
27
|
+
* Execute config hooks
|
|
28
|
+
* @param runner
|
|
29
|
+
* @param options
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
private runConfigHook;
|
|
33
|
+
private runPrepareHook;
|
|
34
|
+
private initServerConfig;
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* merge cliConfig and serverConfig
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
private initConfig;
|
|
11
41
|
close(): Promise<void>;
|
|
12
42
|
listen(port: number | undefined, listener: any): void;
|
|
13
43
|
getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: (() => void) | undefined) => void;
|
|
14
44
|
private createHookRunner;
|
|
45
|
+
private injectContext;
|
|
15
46
|
private initAppContext;
|
|
16
47
|
}
|
package/dist/types/type.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, Server, ServerResponse } from 'http';
|
|
3
|
-
import { serverManager
|
|
3
|
+
import { serverManager } from '@modern-js/server-core';
|
|
4
|
+
import type { ServerPlugin } from '@modern-js/server-core';
|
|
4
5
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
5
6
|
import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
|
|
6
7
|
import { ModernRouteInterface } from './libs/route';
|
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.
|
|
14
|
+
"version": "1.1.1",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -28,39 +28,41 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@modern-js/utils": "^1.
|
|
31
|
+
"@modern-js/utils": "^1.5.0",
|
|
32
32
|
"@babel/compat-data": "^7.17.0",
|
|
33
|
-
"@modern-js/server-core": "^1.
|
|
33
|
+
"@modern-js/server-core": "^1.3.1",
|
|
34
34
|
"axios": "^0.24.0",
|
|
35
35
|
"compare-versions": "^3.6.0",
|
|
36
|
+
"cookie": "^0.4.2",
|
|
36
37
|
"http-proxy-middleware": "^2.0.1",
|
|
37
38
|
"ignore-styles": "^5.0.1",
|
|
39
|
+
"lodash.clone": "^4.5.0",
|
|
40
|
+
"lru-cache": "^6.0.0",
|
|
41
|
+
"merge-deep": "^3.0.3",
|
|
38
42
|
"mime-types": "^2.1.32",
|
|
39
43
|
"path-to-regexp": "^6.2.0",
|
|
40
44
|
"serve-static": "^1.14.1",
|
|
41
|
-
"
|
|
42
|
-
"ua-parser-js": "^0.7.28",
|
|
43
|
-
"lodash.clone": "^4.5.0",
|
|
44
|
-
"cookie": "^0.4.2"
|
|
45
|
+
"ua-parser-js": "^0.7.28"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
|
-
"@modern-js/types": "^1.
|
|
48
|
-
"@modern-js/core": "1.
|
|
48
|
+
"@modern-js/types": "^1.5.0",
|
|
49
|
+
"@modern-js/core": "1.8.0",
|
|
49
50
|
"@scripts/jest-config": "0.0.0",
|
|
50
51
|
"@scripts/build": "0.0.0",
|
|
51
|
-
"
|
|
52
|
+
"@types/cookie": "^0.4.1",
|
|
53
|
+
"@types/jest": "^26.0.9",
|
|
54
|
+
"@types/lodash.clone": "^4",
|
|
52
55
|
"@types/lru-cache": "^5.1.1",
|
|
56
|
+
"@types/merge-deep": "^3.0.0",
|
|
53
57
|
"@types/mime-types": "^2.1.0",
|
|
58
|
+
"@types/node": "^14",
|
|
54
59
|
"@types/serve-static": "^1.13.10",
|
|
55
60
|
"@types/ua-parser-js": "^0.7.36",
|
|
56
|
-
"
|
|
57
|
-
"@types/cookie": "^0.4.1",
|
|
58
|
-
"typescript": "^4",
|
|
59
|
-
"@types/jest": "^26.0.9",
|
|
60
|
-
"@types/node": "^14",
|
|
61
|
-
"node-mocks-http": "^1.11.0",
|
|
61
|
+
"del-cli": "^4.0.1",
|
|
62
62
|
"jest": "^27",
|
|
63
|
-
"
|
|
63
|
+
"node-mocks-http": "^1.11.0",
|
|
64
|
+
"portfinder": "^1.0.28",
|
|
65
|
+
"typescript": "^4"
|
|
64
66
|
},
|
|
65
67
|
"sideEffects": false,
|
|
66
68
|
"modernConfig": {
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ModernServerOptions } from './type';
|
|
|
2
2
|
import { Server } from './server';
|
|
3
3
|
|
|
4
4
|
export { Server };
|
|
5
|
+
export type { ServerConfig } from '@modern-js/server-core';
|
|
5
6
|
export { ModernServer } from './server/modern-server';
|
|
6
7
|
export { createProxyHandler } from './libs/proxy';
|
|
7
8
|
export * from './type';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
|
|
3
|
+
import type { NormalizedConfig } from '@modern-js/core';
|
|
4
|
+
import type { ServerConfig } from '@modern-js/server-core';
|
|
5
|
+
import mergeDeep from 'merge-deep';
|
|
6
|
+
import { debug } from '../utils';
|
|
7
|
+
|
|
8
|
+
export const getServerConfigPath = (
|
|
9
|
+
distDirectory: string,
|
|
10
|
+
serverConfigFile: string = DEFAULT_SERVER_CONFIG,
|
|
11
|
+
) => {
|
|
12
|
+
const serverConfigPath = path.join(distDirectory, serverConfigFile);
|
|
13
|
+
return `${serverConfigPath}.js`;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const requireConfig = (serverConfigPath: string) => {
|
|
17
|
+
if (fs.pathExistsSync(serverConfigPath)) {
|
|
18
|
+
try {
|
|
19
|
+
return compatRequire(serverConfigPath);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
debug(error);
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 对配置进行合并,开发环境下,cliConfig 与 serverConfig 进行深合并
|
|
31
|
+
* 生产环境下,resolvedConfig 与 serverConfig 进行深合并
|
|
32
|
+
* resolvedConfigPath: 构建序列化后的 modern.config.js 文件路径
|
|
33
|
+
*/
|
|
34
|
+
export const loadConfig = ({
|
|
35
|
+
cliConfig,
|
|
36
|
+
serverConfig,
|
|
37
|
+
resolvedConfigPath,
|
|
38
|
+
}: {
|
|
39
|
+
cliConfig: NormalizedConfig;
|
|
40
|
+
serverConfig: ServerConfig;
|
|
41
|
+
resolvedConfigPath: string;
|
|
42
|
+
}) => {
|
|
43
|
+
let config = null;
|
|
44
|
+
if (process.env.NODE_ENV === 'production') {
|
|
45
|
+
const resolvedConfig = requireConfig(resolvedConfigPath);
|
|
46
|
+
// cli config has a higher priority,because it's an argument passed in.
|
|
47
|
+
config = mergeDeep(
|
|
48
|
+
{
|
|
49
|
+
...resolvedConfig,
|
|
50
|
+
plugins: [], // filter cli plugins
|
|
51
|
+
},
|
|
52
|
+
serverConfig,
|
|
53
|
+
cliConfig,
|
|
54
|
+
);
|
|
55
|
+
} else {
|
|
56
|
+
config = mergeDeep(
|
|
57
|
+
{
|
|
58
|
+
...cliConfig,
|
|
59
|
+
plugins: [],
|
|
60
|
+
},
|
|
61
|
+
serverConfig,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return config;
|
|
65
|
+
};
|
package/src/libs/proxy.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
2
2
|
import { NextFunction, BffProxyOptions } from '@modern-js/types';
|
|
3
3
|
import { formatProxyOptions } from '@modern-js/utils';
|
|
4
|
+
import { debug } from '../utils';
|
|
4
5
|
import { ModernServerContext } from './context';
|
|
5
6
|
|
|
6
7
|
export type { BffProxyOptions };
|
|
7
8
|
|
|
8
9
|
export const createProxyHandler = (proxyOptions?: BffProxyOptions) => {
|
|
10
|
+
debug('createProxyHandler', proxyOptions);
|
|
9
11
|
if (!proxyOptions) {
|
|
10
12
|
return null;
|
|
11
13
|
}
|
package/src/server/index.ts
CHANGED
|
@@ -5,8 +5,13 @@ import {
|
|
|
5
5
|
AppContext,
|
|
6
6
|
ConfigContext,
|
|
7
7
|
loadPlugins,
|
|
8
|
+
ServerConfig,
|
|
8
9
|
} from '@modern-js/server-core';
|
|
9
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
logger as defaultLogger,
|
|
12
|
+
SHARED_DIR,
|
|
13
|
+
OUTPUT_CONFIG_FILE,
|
|
14
|
+
} from '@modern-js/utils';
|
|
10
15
|
import type { UserConfig } from '@modern-js/core';
|
|
11
16
|
import { ISAppContext } from '@modern-js/types';
|
|
12
17
|
import {
|
|
@@ -16,6 +21,12 @@ import {
|
|
|
16
21
|
ModernServerInterface,
|
|
17
22
|
} from '../type';
|
|
18
23
|
import { metrics as defaultMetrics } from '../libs/metrics';
|
|
24
|
+
import {
|
|
25
|
+
loadConfig,
|
|
26
|
+
getServerConfigPath,
|
|
27
|
+
requireConfig,
|
|
28
|
+
} from '../libs/loadConfig';
|
|
29
|
+
import { debug } from '../utils';
|
|
19
30
|
import { createProdServer } from './modern-server-split';
|
|
20
31
|
|
|
21
32
|
export class Server {
|
|
@@ -29,31 +40,110 @@ export class Server {
|
|
|
29
40
|
|
|
30
41
|
private runner!: ServerHookRunner;
|
|
31
42
|
|
|
43
|
+
private serverConfig: ServerConfig;
|
|
44
|
+
|
|
32
45
|
constructor(options: ModernServerOptions) {
|
|
33
46
|
options.logger = options.logger || defaultLogger;
|
|
34
47
|
options.metrics = options.metrics || defaultMetrics;
|
|
35
48
|
|
|
36
49
|
this.options = options;
|
|
50
|
+
this.serverConfig = {};
|
|
37
51
|
}
|
|
38
52
|
|
|
53
|
+
/**
|
|
54
|
+
* 初始化顺序
|
|
55
|
+
* - 获取 server runtime config
|
|
56
|
+
* - 设置 context
|
|
57
|
+
* - 创建 hooksRunner
|
|
58
|
+
* - 合并插件,内置插件和 serverConfig 中配置的插件
|
|
59
|
+
* - 执行 config hook
|
|
60
|
+
* - 获取最终的配置
|
|
61
|
+
* - 设置配置到 context
|
|
62
|
+
* - 初始化 server
|
|
63
|
+
* - 执行 prepare hook
|
|
64
|
+
* - 执行 server init
|
|
65
|
+
*/
|
|
39
66
|
public async init() {
|
|
40
67
|
const { options } = this;
|
|
41
68
|
|
|
69
|
+
this.initServerConfig(options);
|
|
70
|
+
|
|
71
|
+
await this.injectContext(this.runner, options);
|
|
72
|
+
|
|
73
|
+
// initialize server runner
|
|
74
|
+
this.runner = await this.createHookRunner();
|
|
75
|
+
|
|
76
|
+
// init config and execute config hook
|
|
77
|
+
await this.initConfig(this.runner, options);
|
|
78
|
+
|
|
79
|
+
await this.injectContext(this.runner, options);
|
|
80
|
+
|
|
42
81
|
// initialize server
|
|
43
82
|
this.server = this.serverImpl(options);
|
|
44
83
|
|
|
84
|
+
await this.runPrepareHook(this.runner);
|
|
85
|
+
|
|
45
86
|
// create http-server
|
|
46
87
|
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
47
88
|
|
|
48
|
-
// initialize server runner
|
|
49
|
-
this.runner = await this.createHookRunner();
|
|
50
|
-
|
|
51
89
|
// runner can only be used after server init
|
|
52
90
|
await this.server.onInit(this.runner);
|
|
53
91
|
|
|
54
92
|
return this;
|
|
55
93
|
}
|
|
56
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Execute config hooks
|
|
97
|
+
* @param runner
|
|
98
|
+
* @param options
|
|
99
|
+
*/
|
|
100
|
+
private runConfigHook(runner: ServerHookRunner, serverConfig: ServerConfig) {
|
|
101
|
+
const newServerConfig = runner.config(serverConfig || {});
|
|
102
|
+
return newServerConfig;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private async runPrepareHook(runner: ServerHookRunner) {
|
|
106
|
+
runner.prepare();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private initServerConfig(options: ModernServerOptions) {
|
|
110
|
+
const { pwd, serverConfigFile } = options;
|
|
111
|
+
const distDirectory = path.join(pwd, options.config.output?.path || 'dist');
|
|
112
|
+
const serverConfigPath = getServerConfigPath(
|
|
113
|
+
distDirectory,
|
|
114
|
+
serverConfigFile,
|
|
115
|
+
);
|
|
116
|
+
const serverConfig = requireConfig(serverConfigPath);
|
|
117
|
+
this.serverConfig = serverConfig;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* merge cliConfig and serverConfig
|
|
123
|
+
*/
|
|
124
|
+
private async initConfig(
|
|
125
|
+
runner: ServerHookRunner,
|
|
126
|
+
options: ModernServerOptions,
|
|
127
|
+
) {
|
|
128
|
+
const { pwd, config } = options;
|
|
129
|
+
|
|
130
|
+
const { serverConfig } = this;
|
|
131
|
+
|
|
132
|
+
const finalServerConfig = this.runConfigHook(runner, serverConfig);
|
|
133
|
+
|
|
134
|
+
const resolvedConfigPath = path.join(
|
|
135
|
+
pwd,
|
|
136
|
+
config?.output?.path || 'dist',
|
|
137
|
+
OUTPUT_CONFIG_FILE,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
options.config = loadConfig({
|
|
141
|
+
cliConfig: config,
|
|
142
|
+
serverConfig: finalServerConfig,
|
|
143
|
+
resolvedConfigPath,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
57
147
|
public async close() {
|
|
58
148
|
await this.server.onClose();
|
|
59
149
|
await new Promise<void>(resolve =>
|
|
@@ -85,15 +175,32 @@ export class Server {
|
|
|
85
175
|
serverManager.clear();
|
|
86
176
|
|
|
87
177
|
const { options } = this;
|
|
178
|
+
// TODO: 确认下这里是不是可以不从 options 中取插件,而是从 config 中取和过滤
|
|
88
179
|
const { plugins = [], pwd, config } = options;
|
|
89
180
|
|
|
181
|
+
const serverPlugins = this.serverConfig.plugins || [];
|
|
182
|
+
|
|
90
183
|
// server app context for serve plugin
|
|
91
|
-
const loadedPlugins = loadPlugins(plugins, pwd);
|
|
184
|
+
const loadedPlugins = loadPlugins(plugins.concat(serverPlugins), pwd);
|
|
185
|
+
|
|
186
|
+
debug('plugins', config.plugins, loadedPlugins);
|
|
92
187
|
loadedPlugins.forEach(p => {
|
|
93
188
|
serverManager.usePlugin(p);
|
|
94
189
|
});
|
|
95
190
|
|
|
191
|
+
// create runner
|
|
192
|
+
const hooksRunner = await serverManager.init({});
|
|
193
|
+
|
|
194
|
+
return hooksRunner;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private async injectContext(
|
|
198
|
+
runner: ServerHookRunner,
|
|
199
|
+
options: ModernServerOptions,
|
|
200
|
+
) {
|
|
96
201
|
const appContext = this.initAppContext();
|
|
202
|
+
const { config, pwd } = options;
|
|
203
|
+
|
|
97
204
|
serverManager.run(() => {
|
|
98
205
|
ConfigContext.set(config as UserConfig);
|
|
99
206
|
AppContext.set({
|
|
@@ -101,8 +208,6 @@ export class Server {
|
|
|
101
208
|
distDirectory: path.join(pwd, config.output?.path || 'dist'),
|
|
102
209
|
});
|
|
103
210
|
});
|
|
104
|
-
|
|
105
|
-
return serverManager.init({});
|
|
106
211
|
}
|
|
107
212
|
|
|
108
213
|
private initAppContext(): ISAppContext {
|
|
@@ -33,6 +33,7 @@ import {
|
|
|
33
33
|
getStaticReg,
|
|
34
34
|
mergeExtension,
|
|
35
35
|
noop,
|
|
36
|
+
debug,
|
|
36
37
|
} from '../utils';
|
|
37
38
|
import * as reader from '../libs/render/reader';
|
|
38
39
|
import { createProxyHandler, BffProxyOptions } from '../libs/proxy';
|
|
@@ -120,6 +121,7 @@ export class ModernServer implements ModernServerInterface {
|
|
|
120
121
|
this.distDir = path.join(pwd, config.output?.path || 'dist');
|
|
121
122
|
this.workDir = this.distDir;
|
|
122
123
|
this.conf = config;
|
|
124
|
+
debug('server conf', this.conf);
|
|
123
125
|
this.logger = logger!;
|
|
124
126
|
this.metrics = metrics!;
|
|
125
127
|
this.router = new RouteMatchManager();
|
|
@@ -143,6 +145,7 @@ export class ModernServer implements ModernServerInterface {
|
|
|
143
145
|
next();
|
|
144
146
|
});
|
|
145
147
|
|
|
148
|
+
debug('final server conf', this.conf);
|
|
146
149
|
// proxy handler, each proxy has own handler
|
|
147
150
|
this.proxyHandler = createProxyHandler(conf.bff?.proxy as BffProxyOptions);
|
|
148
151
|
if (this.proxyHandler) {
|
|
@@ -279,6 +282,7 @@ export class ModernServer implements ModernServerInterface {
|
|
|
279
282
|
? ApiServerMode.frame
|
|
280
283
|
: ApiServerMode.func;
|
|
281
284
|
|
|
285
|
+
debug('exists api dir', mode);
|
|
282
286
|
// if use lambda/, mean framework style of writing, then discard user extension
|
|
283
287
|
const apiExtension = mergeExtension(pluginAPIExt);
|
|
284
288
|
this.frameAPIHandler = await this.prepareAPIHandler(mode, apiExtension);
|
|
@@ -306,7 +310,6 @@ export class ModernServer implements ModernServerInterface {
|
|
|
306
310
|
const { workDir, runner, conf } = this;
|
|
307
311
|
const { bff } = conf as ConfWithBFF;
|
|
308
312
|
const prefix = bff?.prefix || '/api';
|
|
309
|
-
|
|
310
313
|
return runner.prepareApiServer(
|
|
311
314
|
{
|
|
312
315
|
pwd: workDir,
|
|
@@ -379,7 +382,6 @@ export class ModernServer implements ModernServerInterface {
|
|
|
379
382
|
|
|
380
383
|
/* —————————————————————— private function —————————————————————— */
|
|
381
384
|
// handler route.json, include api / csr / ssr
|
|
382
|
-
// eslint-disable-next-line max-statements
|
|
383
385
|
private async routeHandler(context: ModernServerContext) {
|
|
384
386
|
const { req, res } = context;
|
|
385
387
|
|
|
@@ -460,7 +462,6 @@ export class ModernServer implements ModernServerInterface {
|
|
|
460
462
|
res.end(response);
|
|
461
463
|
}
|
|
462
464
|
|
|
463
|
-
// eslint-disable-next-line max-statements
|
|
464
465
|
private async injectMicroFE(
|
|
465
466
|
context: ModernServerContext,
|
|
466
467
|
templateAPI: ReturnType<typeof createTemplateAPI>,
|
package/src/type.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
2
|
import { IncomingMessage, Server, ServerResponse } from 'http';
|
|
3
|
-
import { serverManager
|
|
3
|
+
import { serverManager } from '@modern-js/server-core';
|
|
4
|
+
import type { ServerPlugin } from '@modern-js/server-core';
|
|
4
5
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
5
6
|
import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
|
|
6
7
|
import { ModernRouteInterface } from './libs/route';
|
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
2
2
|
import { compile } from 'path-to-regexp';
|
|
3
|
+
import { createDebugger } from '@modern-js/utils';
|
|
4
|
+
|
|
5
|
+
export const debug = createDebugger('prod-server') as any;
|
|
3
6
|
|
|
4
7
|
export const mergeExtension = (users: any[]) => {
|
|
5
8
|
const output: any[] = [];
|
package/tests/.eslintrc.js
DELETED
package/tests/context.test.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'events';
|
|
2
|
-
import { Readable } from 'stream';
|
|
3
|
-
import httpMocks from 'node-mocks-http';
|
|
4
|
-
import { createContext } from '../src/libs/context';
|
|
5
|
-
|
|
6
|
-
describe('test server context', () => {
|
|
7
|
-
test('should route api work correctly', () => {
|
|
8
|
-
const req = httpMocks.createRequest({
|
|
9
|
-
url: '/pathname?foo=baz',
|
|
10
|
-
headers: {
|
|
11
|
-
host: 'modernjs.com',
|
|
12
|
-
},
|
|
13
|
-
eventEmitter: Readable,
|
|
14
|
-
method: 'GET',
|
|
15
|
-
});
|
|
16
|
-
const res = httpMocks.createResponse({ eventEmitter: EventEmitter });
|
|
17
|
-
const context = createContext(req, res);
|
|
18
|
-
const {
|
|
19
|
-
method,
|
|
20
|
-
url,
|
|
21
|
-
origin,
|
|
22
|
-
host,
|
|
23
|
-
path,
|
|
24
|
-
href,
|
|
25
|
-
query,
|
|
26
|
-
querystring,
|
|
27
|
-
protocol,
|
|
28
|
-
params,
|
|
29
|
-
} = context;
|
|
30
|
-
|
|
31
|
-
expect(method).toBe('GET');
|
|
32
|
-
expect(url).toBe('/pathname?foo=baz');
|
|
33
|
-
expect(origin).toBe('http://modernjs.com');
|
|
34
|
-
expect(host).toBe('modernjs.com');
|
|
35
|
-
expect(path).toBe('/pathname');
|
|
36
|
-
expect(href).toBe('http://modernjs.com/pathname?foo=baz');
|
|
37
|
-
expect(query).toEqual({ foo: 'baz' });
|
|
38
|
-
expect(querystring).toBe('foo=baz');
|
|
39
|
-
expect(protocol).toBe('http');
|
|
40
|
-
expect(params).toEqual({});
|
|
41
|
-
|
|
42
|
-
expect(context.serverData).toEqual({});
|
|
43
|
-
context.setServerData('foo', {
|
|
44
|
-
name: 'foo',
|
|
45
|
-
});
|
|
46
|
-
expect(context.serverData).toEqual({
|
|
47
|
-
foo: {
|
|
48
|
-
name: 'foo',
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
console.info('index.js');
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "prod-server-pure",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"scripts": {
|
|
5
|
-
"reset": "del-cli node_modules",
|
|
6
|
-
"dev": "modern dev",
|
|
7
|
-
"build": "modern build",
|
|
8
|
-
"start": "modern start",
|
|
9
|
-
"new": "modern new",
|
|
10
|
-
"lint": "modern lint",
|
|
11
|
-
"deploy": "modern deploy"
|
|
12
|
-
},
|
|
13
|
-
"dependencies": {},
|
|
14
|
-
"devDependencies": {},
|
|
15
|
-
"modernConfig": {
|
|
16
|
-
"runtime": {
|
|
17
|
-
"router": true,
|
|
18
|
-
"state": true
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|