@modern-js/server 1.2.1-rc.0 → 1.3.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -7
- package/dist/js/modern/dev-tools/mock/index.js +1 -1
- package/dist/js/modern/dev-tools/watcher/index.js +5 -5
- package/dist/js/modern/libs/render/cache/index.js +1 -1
- package/dist/js/modern/libs/render/index.js +1 -1
- package/dist/js/modern/libs/render/modern/index.js +3 -1
- package/dist/js/modern/server/dev-server/dev-server.js +15 -5
- package/dist/js/modern/server/index.js +2 -2
- package/dist/js/modern/server/modern-server.js +3 -2
- package/dist/js/modern/utils.js +23 -0
- package/dist/js/node/dev-tools/mock/index.js +2 -2
- package/dist/js/node/dev-tools/watcher/index.js +5 -5
- package/dist/js/node/libs/render/cache/index.js +2 -2
- package/dist/js/node/libs/render/index.js +2 -2
- package/dist/js/node/libs/render/modern/index.js +3 -3
- package/dist/js/node/server/dev-server/dev-server.js +17 -5
- package/dist/js/node/server/index.js +3 -3
- package/dist/js/node/server/modern-server.js +4 -3
- package/dist/js/node/utils.js +28 -2
- package/dist/types/dev-tools/watcher/index.d.ts +2 -1
- package/dist/types/libs/metrics.d.ts +1 -1
- package/dist/types/libs/render/index.d.ts +1 -2
- package/dist/types/libs/render/ssr.d.ts +1 -2
- package/dist/types/server/dev-server/dev-server-split.d.ts +3 -3
- package/dist/types/server/dev-server/dev-server.d.ts +1 -1
- package/dist/types/server/index.d.ts +1 -1
- package/dist/types/server/modern-server-split.d.ts +4 -4
- package/dist/types/server/modern-server.d.ts +10 -7
- package/dist/types/utils.d.ts +10 -1
- package/jest.config.js +9 -0
- package/package.json +21 -15
- package/src/dev-tools/mock/index.ts +1 -1
- package/src/dev-tools/socket-server.ts +1 -1
- package/src/dev-tools/watcher/index.ts +10 -6
- package/src/libs/metrics.ts +1 -1
- package/src/libs/render/cache/index.ts +1 -1
- package/src/libs/render/index.ts +2 -3
- package/src/libs/render/modern/index.ts +2 -1
- package/src/libs/render/ssr.ts +1 -2
- package/src/server/dev-server/dev-server-split.ts +3 -3
- package/src/server/dev-server/dev-server.ts +50 -34
- package/src/server/index.ts +3 -3
- package/src/server/modern-server-split.ts +4 -4
- package/src/server/modern-server.ts +23 -14
- package/src/utils.ts +28 -0
- package/tests/dev.test.ts +39 -0
- package/tests/fixtures/pure/tsconfig.json +1 -1
- package/tests/middleware.test.ts +12 -11
- package/tests/server.test.ts +32 -1
- package/tests/tsconfig.json +1 -3
- package/tests/utils.test.ts +31 -0
- package/tests/watcher.test.ts +96 -0
- package/tsconfig.json +1 -3
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.3.1-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",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
22
|
"node": {
|
|
23
|
+
"jsnext:source": "./src/index.ts",
|
|
23
24
|
"import": "./dist/js/modern/index.js",
|
|
24
25
|
"require": "./dist/js/node/index.js"
|
|
25
26
|
},
|
|
@@ -27,18 +28,18 @@
|
|
|
27
28
|
}
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@babel/core": "
|
|
31
|
+
"@babel/core": "7.16.7",
|
|
31
32
|
"@babel/compat-data": "^7.15.0",
|
|
32
33
|
"@babel/preset-env": "^7.15.0",
|
|
33
34
|
"@babel/preset-typescript": "^7.15.0",
|
|
34
35
|
"@babel/register": "^7.15.3",
|
|
35
36
|
"@babel/runtime": "^7",
|
|
36
|
-
"@modern-js/core": "^1.
|
|
37
|
-
"@modern-js/hmr-client": "^1.
|
|
38
|
-
"@modern-js/server-plugin": "^1.
|
|
39
|
-
"@modern-js/server-utils": "^1.
|
|
40
|
-
"@modern-js/bff-utils": "^1.
|
|
41
|
-
"@modern-js/utils": "^1.
|
|
37
|
+
"@modern-js/core": "^1.3.0",
|
|
38
|
+
"@modern-js/hmr-client": "^1.2.0",
|
|
39
|
+
"@modern-js/server-plugin": "^1.2.0",
|
|
40
|
+
"@modern-js/server-utils": "^1.2.0",
|
|
41
|
+
"@modern-js/bff-utils": "^1.2.0",
|
|
42
|
+
"@modern-js/utils": "^1.2.0",
|
|
42
43
|
"axios": "^0.21.4",
|
|
43
44
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
44
45
|
"chokidar": "^3.5.2",
|
|
@@ -58,12 +59,12 @@
|
|
|
58
59
|
"tsconfig-paths": "^3.11.0",
|
|
59
60
|
"ua-parser-js": "^0.7.28",
|
|
60
61
|
"webpack-dev-middleware": "^5.0.0",
|
|
61
|
-
"ws": "^8.2.0"
|
|
62
|
+
"ws": "^8.2.0",
|
|
63
|
+
"lodash.clone": "^4.5.0"
|
|
62
64
|
},
|
|
63
65
|
"devDependencies": {
|
|
64
|
-
"@
|
|
65
|
-
"@modern-js/
|
|
66
|
-
"@modern-js/types": "^1.1.5-rc.0",
|
|
66
|
+
"@scripts/build": "0.0.0",
|
|
67
|
+
"@modern-js/types": "^1.2.0",
|
|
67
68
|
"@types/jest": "^26",
|
|
68
69
|
"@types/lru-cache": "^5.1.1",
|
|
69
70
|
"@types/mime-types": "^2.1.0",
|
|
@@ -77,10 +78,14 @@
|
|
|
77
78
|
"@types/ua-parser-js": "^0.7.36",
|
|
78
79
|
"@types/webpack-dev-middleware": "^5.0.2",
|
|
79
80
|
"@types/ws": "^7.4.7",
|
|
81
|
+
"@types/lodash.clone": "^4",
|
|
80
82
|
"typescript": "^4",
|
|
81
83
|
"webpack": "^5.54.0",
|
|
82
84
|
"node-mocks-http": "^1.11.0",
|
|
83
|
-
"portfinder": "^1.0.28"
|
|
85
|
+
"portfinder": "^1.0.28",
|
|
86
|
+
"jest": "^27",
|
|
87
|
+
"@scripts/jest-config": "0.0.0",
|
|
88
|
+
"websocket": "^1"
|
|
84
89
|
},
|
|
85
90
|
"peerDependencies": {
|
|
86
91
|
"webpack": "^5.54.0"
|
|
@@ -93,13 +98,14 @@
|
|
|
93
98
|
},
|
|
94
99
|
"publishConfig": {
|
|
95
100
|
"registry": "https://registry.npmjs.org/",
|
|
96
|
-
"access": "public"
|
|
101
|
+
"access": "public",
|
|
102
|
+
"types": "./dist/types/index.d.ts"
|
|
97
103
|
},
|
|
98
104
|
"scripts": {
|
|
99
105
|
"new": "modern new",
|
|
100
106
|
"build": "modern build",
|
|
101
107
|
"dev": "modern build --watch",
|
|
102
|
-
"test": "
|
|
108
|
+
"test": "jest --passWithNoTests"
|
|
103
109
|
},
|
|
104
110
|
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
|
105
111
|
}
|
|
@@ -3,8 +3,8 @@ import { fs } from '@modern-js/utils';
|
|
|
3
3
|
import { match } from 'path-to-regexp';
|
|
4
4
|
import { NextFunction } from '../../type';
|
|
5
5
|
import { ModernServerContext } from '../../libs/context';
|
|
6
|
+
import { AGGRED_DIR } from '../../constants';
|
|
6
7
|
import getMockData, { MockApi } from './getMockData';
|
|
7
|
-
import { AGGRED_DIR } from '@/constants';
|
|
8
8
|
|
|
9
9
|
const getMatched = (context: ModernServerContext, mockApiList: MockApi[]) => {
|
|
10
10
|
const { path: targetPathname, method: targetMethod } = context;
|
|
@@ -4,7 +4,7 @@ import ws from 'ws';
|
|
|
4
4
|
import type { Stats } from 'webpack';
|
|
5
5
|
import { logger } from '@modern-js/utils';
|
|
6
6
|
import { DevServerOptions } from '../type';
|
|
7
|
-
import { noop } from '
|
|
7
|
+
import { noop } from '../utils';
|
|
8
8
|
|
|
9
9
|
interface ExtWebSocket extends ws {
|
|
10
10
|
isAlive: boolean;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import chokidar, { FSWatcher } from 'chokidar';
|
|
1
|
+
import chokidar, { FSWatcher, WatchOptions } from 'chokidar';
|
|
2
2
|
import { DependencyTree } from './dependency-tree';
|
|
3
3
|
import { StatsCache } from './stats-cache';
|
|
4
4
|
|
|
@@ -18,14 +18,18 @@ export default class Watcher {
|
|
|
18
18
|
|
|
19
19
|
private watcher!: FSWatcher;
|
|
20
20
|
|
|
21
|
-
public listen(
|
|
21
|
+
public listen(
|
|
22
|
+
files: string[],
|
|
23
|
+
options: WatchOptions,
|
|
24
|
+
callback: (changed: string) => void,
|
|
25
|
+
) {
|
|
22
26
|
const watched = files.filter(Boolean);
|
|
27
|
+
const filenames = watched.map(filename => filename.replace(/\\/g, '/'));
|
|
28
|
+
// eslint-disable-next-line no-console
|
|
29
|
+
console.log('watched files:', filenames);
|
|
23
30
|
|
|
24
31
|
const cache = new StatsCache();
|
|
25
|
-
const watcher = chokidar.watch(
|
|
26
|
-
// 初始化的时候不触发 add、addDir 事件
|
|
27
|
-
ignoreInitial: true,
|
|
28
|
-
});
|
|
32
|
+
const watcher = chokidar.watch(filenames, options);
|
|
29
33
|
|
|
30
34
|
watcher.on('ready', () => {
|
|
31
35
|
cache.add(getWatchedFiles(watcher));
|
package/src/libs/metrics.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ModernServerContext } from '../../context';
|
|
2
2
|
import { RenderFunction, SSRServerContext } from '../type';
|
|
3
|
+
import { ERROR_DIGEST } from '../../../constants';
|
|
3
4
|
import { createCache } from './spr';
|
|
4
5
|
import { namespaceHash, withCoalescedInvoke } from './util';
|
|
5
6
|
import { CacheContext } from './type';
|
|
6
|
-
import { ERROR_DIGEST } from '@/constants';
|
|
7
7
|
|
|
8
8
|
export default (renderFn: RenderFunction, ctx: ModernServerContext) => {
|
|
9
9
|
const sprCache = createCache();
|
package/src/libs/render/index.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { fs } from '@modern-js/utils';
|
|
3
3
|
import mime from 'mime-types';
|
|
4
|
-
import { RenderResult } from '../../type';
|
|
4
|
+
import { RenderResult, ServerHookRunner } from '../../type';
|
|
5
5
|
import { ModernRoute } from '../route';
|
|
6
6
|
import { ModernServerContext } from '../context';
|
|
7
|
+
import { ERROR_DIGEST } from '../../constants';
|
|
7
8
|
import { handleDirectory } from './static';
|
|
8
9
|
import { readFile } from './reader';
|
|
9
10
|
import * as ssr from './ssr';
|
|
10
11
|
import { supportModern, getModernEntry } from './modern';
|
|
11
|
-
import { ERROR_DIGEST } from '@/constants';
|
|
12
|
-
import { ServerHookRunner } from '@/type';
|
|
13
12
|
|
|
14
13
|
export const createRenderHandler = ({
|
|
15
14
|
distDir,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import Parser from 'ua-parser-js';
|
|
2
|
-
import nativeModules from '@babel/compat-data/native-modules';
|
|
3
2
|
import compareVersions from 'compare-versions';
|
|
4
3
|
import { ModernServerContext } from '../../context';
|
|
5
4
|
import { NativeModuleNameMap } from './browser-list';
|
|
6
5
|
|
|
6
|
+
const nativeModules = require('@babel/compat-data/native-modules');
|
|
7
|
+
|
|
7
8
|
export const supportModern = (context: ModernServerContext) => {
|
|
8
9
|
if (context.query.modern_es6) {
|
|
9
10
|
return true;
|
package/src/libs/render/ssr.ts
CHANGED
|
@@ -2,10 +2,9 @@ import path from 'path';
|
|
|
2
2
|
import { SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils';
|
|
3
3
|
import mime from 'mime-types';
|
|
4
4
|
import { ModernServerContext } from '../context';
|
|
5
|
-
import { RenderResult } from '../../type';
|
|
5
|
+
import { RenderResult, ServerHookRunner } from '../../type';
|
|
6
6
|
import cache from './cache';
|
|
7
7
|
import { SSRServerContext } from './type';
|
|
8
|
-
import { ServerHookRunner } from '@/type';
|
|
9
8
|
|
|
10
9
|
export const render = async (
|
|
11
10
|
ctx: ModernServerContext,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
+
import { mergeExtension } from '../../utils';
|
|
3
|
+
import { ModernRouteInterface } from '../../libs/route';
|
|
4
|
+
import { ApiServerMode } from '../../constants';
|
|
2
5
|
import { ModernDevServer } from './dev-server';
|
|
3
|
-
import { mergeExtension } from '@/utils';
|
|
4
|
-
import { ModernRouteInterface } from '@/libs/route';
|
|
5
|
-
import { ApiServerMode } from '@/constants';
|
|
6
6
|
|
|
7
7
|
export class ModernSSRDevServer extends ModernDevServer {
|
|
8
8
|
protected prepareAPIHandler(
|
|
@@ -15,23 +15,23 @@ import {
|
|
|
15
15
|
import type { MultiCompiler, Compiler } from 'webpack';
|
|
16
16
|
import webpackDevMiddleware from 'webpack-dev-middleware';
|
|
17
17
|
import { ModernServer } from '../modern-server';
|
|
18
|
-
import { createMockHandler } from '
|
|
19
|
-
import { createProxyHandler, ProxyOptions } from '
|
|
18
|
+
import { createMockHandler } from '../../dev-tools/mock';
|
|
19
|
+
import { createProxyHandler, ProxyOptions } from '../../libs/proxy';
|
|
20
20
|
import {
|
|
21
21
|
DevServerOptions,
|
|
22
22
|
ModernServerOptions,
|
|
23
23
|
NextFunction,
|
|
24
24
|
ServerHookRunner,
|
|
25
25
|
ReadyOptions,
|
|
26
|
-
} from '
|
|
27
|
-
import SocketServer from '
|
|
28
|
-
import DevServerPlugin from '
|
|
29
|
-
import { ModernServerContext } from '
|
|
30
|
-
import { createLaunchEditorHandler } from '
|
|
31
|
-
import { enableRegister } from '
|
|
32
|
-
import * as reader from '
|
|
33
|
-
import Watcher from '
|
|
34
|
-
import { AGGRED_DIR } from '
|
|
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';
|
|
34
|
+
import { AGGRED_DIR } from '../../constants';
|
|
35
35
|
|
|
36
36
|
const DEFAULT_DEV_OPTIONS: DevServerOptions = {
|
|
37
37
|
client: {
|
|
@@ -84,7 +84,6 @@ export class ModernDevServer extends ModernServer {
|
|
|
84
84
|
// Complete the preparation of services
|
|
85
85
|
public async init(runner: ServerHookRunner) {
|
|
86
86
|
const { conf, pwd, compiler } = this;
|
|
87
|
-
|
|
88
87
|
// mock handler
|
|
89
88
|
this.mockHandler = createMockHandler({ pwd });
|
|
90
89
|
this.addHandler((ctx: ModernServerContext, next: NextFunction) => {
|
|
@@ -150,9 +149,13 @@ export class ModernDevServer extends ModernServer {
|
|
|
150
149
|
super.close();
|
|
151
150
|
await this.watcher.close();
|
|
152
151
|
await new Promise<void>(resolve => {
|
|
153
|
-
this.devMiddleware
|
|
152
|
+
if (this.devMiddleware) {
|
|
153
|
+
this.devMiddleware.close(() => {
|
|
154
|
+
resolve();
|
|
155
|
+
});
|
|
156
|
+
} else {
|
|
154
157
|
resolve();
|
|
155
|
-
}
|
|
158
|
+
}
|
|
156
159
|
});
|
|
157
160
|
}
|
|
158
161
|
|
|
@@ -166,7 +169,7 @@ export class ModernDevServer extends ModernServer {
|
|
|
166
169
|
const { dev } = this;
|
|
167
170
|
const devHttpsOption = typeof dev === 'object' && dev.https;
|
|
168
171
|
if (devHttpsOption) {
|
|
169
|
-
const { genHttpsOptions } = require('
|
|
172
|
+
const { genHttpsOptions } = require('../../dev-tools/https');
|
|
170
173
|
const httpsOptions = await genHttpsOptions(devHttpsOption);
|
|
171
174
|
return createHttpsServer(httpsOptions, handler);
|
|
172
175
|
} else {
|
|
@@ -252,32 +255,45 @@ export class ModernDevServer extends ModernServer {
|
|
|
252
255
|
const { pwd } = this;
|
|
253
256
|
const { mock } = AGGRED_DIR;
|
|
254
257
|
const defaultWatched = [
|
|
255
|
-
`${
|
|
256
|
-
`${
|
|
257
|
-
`${
|
|
258
|
-
`${
|
|
258
|
+
`${mock}/**/*`,
|
|
259
|
+
`${SERVER_DIR}/**/*`,
|
|
260
|
+
`${API_DIR}/**`,
|
|
261
|
+
`${SHARED_DIR}/**/*`,
|
|
259
262
|
];
|
|
260
263
|
|
|
264
|
+
const defaultWatchedPaths = defaultWatched.map(p =>
|
|
265
|
+
path.normalize(path.join(pwd, p)),
|
|
266
|
+
);
|
|
267
|
+
const mockPath = path.normalize(path.join(pwd, mock));
|
|
268
|
+
|
|
261
269
|
const watcher = new Watcher();
|
|
262
270
|
watcher.createDepTree();
|
|
263
271
|
|
|
264
272
|
// 监听文件变动,如果有变动则给 client,也就是 start 启动的插件发消息
|
|
265
|
-
watcher.listen(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
273
|
+
watcher.listen(
|
|
274
|
+
defaultWatchedPaths,
|
|
275
|
+
{
|
|
276
|
+
// 初始化的时候不触发 add、addDir 事件
|
|
277
|
+
ignoreInitial: true,
|
|
278
|
+
ignored: /api\/typings\/.*/,
|
|
279
|
+
},
|
|
280
|
+
(filepath: string) => {
|
|
281
|
+
watcher.updateDepTree();
|
|
282
|
+
watcher.cleanDepCache(filepath);
|
|
283
|
+
|
|
284
|
+
this.runner.reset();
|
|
285
|
+
|
|
286
|
+
if (filepath.startsWith(mockPath)) {
|
|
287
|
+
this.mockHandler = createMockHandler({ pwd });
|
|
288
|
+
} else {
|
|
289
|
+
try {
|
|
290
|
+
this.prepareFrameHandler();
|
|
291
|
+
} catch (e) {
|
|
292
|
+
this.logger.error(e as Error);
|
|
293
|
+
}
|
|
278
294
|
}
|
|
279
|
-
}
|
|
280
|
-
|
|
295
|
+
},
|
|
296
|
+
);
|
|
281
297
|
|
|
282
298
|
this.watcher = watcher;
|
|
283
299
|
}
|
package/src/server/index.ts
CHANGED
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
ConfigContext,
|
|
11
11
|
UserConfig,
|
|
12
12
|
} from '@modern-js/core';
|
|
13
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '../type';
|
|
14
|
+
import { metrics as defaultMetrics } from '../libs/metrics';
|
|
13
15
|
import { ModernServer } from './modern-server';
|
|
14
16
|
import type { ModernDevServer } from './dev-server';
|
|
15
17
|
import {
|
|
@@ -17,8 +19,6 @@ import {
|
|
|
17
19
|
ModernSSRServer,
|
|
18
20
|
ModernWebServer,
|
|
19
21
|
} from './modern-server-split';
|
|
20
|
-
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from '@/type';
|
|
21
|
-
import { metrics as defaultMetrics } from '@/libs/metrics';
|
|
22
22
|
|
|
23
23
|
export class Server {
|
|
24
24
|
public options: ModernServerOptions;
|
|
@@ -68,7 +68,7 @@ export class Server {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
public listen(port = 8080, listener: any) {
|
|
71
|
-
this.app.listen(port, () => {
|
|
71
|
+
this.app.listen(process.env.PORT || port, () => {
|
|
72
72
|
if (listener) {
|
|
73
73
|
listener();
|
|
74
74
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
+
import { mergeExtension } from '../utils';
|
|
3
|
+
import { ModernRoute, ModernRouteInterface, RouteMatcher } from '../libs/route';
|
|
4
|
+
import { ApiServerMode } from '../constants';
|
|
5
|
+
import { ModernServerContext } from '../libs/context';
|
|
2
6
|
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
7
|
|
|
8
8
|
export class ModernSSRServer extends ModernServer {
|
|
9
9
|
// Todo should not invoke any route hook in modernSSRServer
|
|
@@ -4,10 +4,10 @@ import util from 'util';
|
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { fs, ROUTE_SPEC_FILE } from '@modern-js/utils';
|
|
6
6
|
import { Adapter, APIServerStartInput } from '@modern-js/server-plugin';
|
|
7
|
-
import { createMiddlewareCollecter } from '@modern-js/server-utils';
|
|
8
7
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
9
8
|
import mime from 'mime-types';
|
|
10
9
|
import axios from 'axios';
|
|
10
|
+
import clone from 'lodash.clone';
|
|
11
11
|
import {
|
|
12
12
|
ModernServerOptions,
|
|
13
13
|
NextFunction,
|
|
@@ -16,27 +16,32 @@ import {
|
|
|
16
16
|
Logger,
|
|
17
17
|
ReadyOptions,
|
|
18
18
|
ConfWithBFF,
|
|
19
|
-
} from '
|
|
19
|
+
} from '../type';
|
|
20
20
|
import {
|
|
21
21
|
RouteMatchManager,
|
|
22
22
|
ModernRouteInterface,
|
|
23
23
|
ModernRoute,
|
|
24
24
|
RouteMatcher,
|
|
25
|
-
} from '
|
|
26
|
-
import { createRenderHandler } from '
|
|
27
|
-
import { createStaticFileHandler } from '
|
|
28
|
-
import {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
} from '../libs/route';
|
|
26
|
+
import { createRenderHandler } from '../libs/render';
|
|
27
|
+
import { createStaticFileHandler } from '../libs/serve-file';
|
|
28
|
+
import {
|
|
29
|
+
createErrorDocument,
|
|
30
|
+
createMiddlewareCollecter,
|
|
31
|
+
mergeExtension,
|
|
32
|
+
noop,
|
|
33
|
+
} from '../utils';
|
|
34
|
+
import * as reader from '../libs/render/reader';
|
|
35
|
+
import { createProxyHandler, ProxyOptions } from '../libs/proxy';
|
|
36
|
+
import { createContext, ModernServerContext } from '../libs/context';
|
|
32
37
|
import {
|
|
33
38
|
AGGRED_DIR,
|
|
34
39
|
ApiServerMode,
|
|
35
40
|
ERROR_DIGEST,
|
|
36
41
|
ERROR_PAGE_TEXT,
|
|
37
|
-
} from '
|
|
38
|
-
import { createTemplateAPI } from '
|
|
39
|
-
import { createRouteAPI } from '
|
|
42
|
+
} from '../constants';
|
|
43
|
+
import { createTemplateAPI } from '../libs/hook-api/template';
|
|
44
|
+
import { createRouteAPI } from '../libs/hook-api/route';
|
|
40
45
|
|
|
41
46
|
type ModernServerHandler = (
|
|
42
47
|
context: ModernServerContext,
|
|
@@ -319,9 +324,13 @@ export class ModernServer {
|
|
|
319
324
|
|
|
320
325
|
protected async emitRouteHook(
|
|
321
326
|
eventName: 'beforeMatch' | 'afterMatch' | 'beforeRender' | 'afterRender',
|
|
322
|
-
input:
|
|
327
|
+
input: {
|
|
328
|
+
context: ModernServerContext;
|
|
329
|
+
[propsName: string]: any;
|
|
330
|
+
},
|
|
323
331
|
) {
|
|
324
|
-
|
|
332
|
+
input.context = clone(input.context);
|
|
333
|
+
return this.runner[eventName](input as any, { onLast: noop as any });
|
|
325
334
|
}
|
|
326
335
|
|
|
327
336
|
// warmup ssr function
|
package/src/utils.ts
CHANGED
|
@@ -49,3 +49,31 @@ export const createErrorDocument = (status: number, text: string) => {
|
|
|
49
49
|
</html>
|
|
50
50
|
`;
|
|
51
51
|
};
|
|
52
|
+
|
|
53
|
+
export type CollectMiddlewaresResult = {
|
|
54
|
+
web: any[];
|
|
55
|
+
api: any[];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const createMiddlewareCollecter = () => {
|
|
59
|
+
const webMiddlewares: any[] = [];
|
|
60
|
+
const apiMiddlewares: any[] = [];
|
|
61
|
+
|
|
62
|
+
const addWebMiddleware = (input: any) => {
|
|
63
|
+
webMiddlewares.push(input);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const addAPIMiddleware = (input: any) => {
|
|
67
|
+
apiMiddlewares.push(input);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const getMiddlewares = (): CollectMiddlewaresResult => ({
|
|
71
|
+
web: webMiddlewares,
|
|
72
|
+
api: apiMiddlewares,
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
getMiddlewares,
|
|
76
|
+
addWebMiddleware,
|
|
77
|
+
addAPIMiddleware,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import SocketServer from '../src/dev-tools/socket-server';
|
|
3
|
+
|
|
4
|
+
describe('test dev tools', () => {
|
|
5
|
+
test('should socket server work correctly', () => {
|
|
6
|
+
const socketServer: any = new SocketServer({
|
|
7
|
+
client: {
|
|
8
|
+
port: '8080',
|
|
9
|
+
overlay: false,
|
|
10
|
+
logging: 'error',
|
|
11
|
+
path: '/',
|
|
12
|
+
host: '127.0.0.1',
|
|
13
|
+
},
|
|
14
|
+
dev: {
|
|
15
|
+
writeToDisk: false,
|
|
16
|
+
},
|
|
17
|
+
watch: true,
|
|
18
|
+
hot: true,
|
|
19
|
+
liveReload: true,
|
|
20
|
+
});
|
|
21
|
+
const app = http
|
|
22
|
+
.createServer((req, res) => {
|
|
23
|
+
res.end();
|
|
24
|
+
})
|
|
25
|
+
.listen(8080);
|
|
26
|
+
|
|
27
|
+
socketServer.prepare(app);
|
|
28
|
+
expect(socketServer.app).toBe(app);
|
|
29
|
+
expect(socketServer.stats).toBeUndefined();
|
|
30
|
+
|
|
31
|
+
const mockStats = {
|
|
32
|
+
toJson: () => ({}),
|
|
33
|
+
};
|
|
34
|
+
socketServer.updateStats(mockStats);
|
|
35
|
+
expect(socketServer.stats).toBe(mockStats);
|
|
36
|
+
|
|
37
|
+
app.close();
|
|
38
|
+
});
|
|
39
|
+
});
|
package/tests/middleware.test.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable max-nested-callbacks */
|
|
2
1
|
import path from 'path';
|
|
3
2
|
import EventEmitter from 'events';
|
|
4
3
|
import { Readable } from 'stream';
|
|
@@ -66,12 +65,15 @@ describe('test middleware create factory', () => {
|
|
|
66
65
|
let sourceServerPort = 8080;
|
|
67
66
|
let sourceServer: Server | null = null;
|
|
68
67
|
beforeAll(async () => {
|
|
68
|
+
let done: any;
|
|
69
|
+
const promise = new Promise(resolve => (done = resolve));
|
|
69
70
|
sourceServerPort = await portfinder.getPortPromise();
|
|
70
71
|
sourceServer = createServer((req, res) => {
|
|
71
72
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
72
73
|
res.write(req.url?.slice(1));
|
|
73
74
|
res.end();
|
|
74
|
-
}).listen(sourceServerPort);
|
|
75
|
+
}).listen(sourceServerPort, done);
|
|
76
|
+
return promise;
|
|
75
77
|
});
|
|
76
78
|
|
|
77
79
|
afterAll(() => {
|
|
@@ -83,7 +85,7 @@ describe('test middleware create factory', () => {
|
|
|
83
85
|
test('should proxy correctly use simply options', async () => {
|
|
84
86
|
const port = await portfinder.getPortPromise();
|
|
85
87
|
const middlewares = createProxyHandler({
|
|
86
|
-
'/simple': `http://
|
|
88
|
+
'/simple': `http://localhost:${sourceServerPort}`,
|
|
87
89
|
});
|
|
88
90
|
const proxyHandler = middlewares![0];
|
|
89
91
|
|
|
@@ -95,7 +97,7 @@ describe('test middleware create factory', () => {
|
|
|
95
97
|
}).listen(port);
|
|
96
98
|
|
|
97
99
|
try {
|
|
98
|
-
const { data } = await axios.get(`http://
|
|
100
|
+
const { data } = await axios.get(`http://localhost:${port}/simple`);
|
|
99
101
|
expect(data).toBe('simple');
|
|
100
102
|
} finally {
|
|
101
103
|
server.close();
|
|
@@ -106,7 +108,7 @@ describe('test middleware create factory', () => {
|
|
|
106
108
|
const port = await portfinder.getPortPromise();
|
|
107
109
|
const middlewares = createProxyHandler({
|
|
108
110
|
'/simple-obj': {
|
|
109
|
-
target: `http://
|
|
111
|
+
target: `http://localhost:${sourceServerPort}`,
|
|
110
112
|
},
|
|
111
113
|
});
|
|
112
114
|
const proxyHandler = middlewares![0];
|
|
@@ -119,7 +121,7 @@ describe('test middleware create factory', () => {
|
|
|
119
121
|
}).listen(port);
|
|
120
122
|
|
|
121
123
|
try {
|
|
122
|
-
const { data } = await axios.get(`http://
|
|
124
|
+
const { data } = await axios.get(`http://localhost:${port}/simple-obj`);
|
|
123
125
|
expect(data).toBe('simple-obj');
|
|
124
126
|
} finally {
|
|
125
127
|
server.close();
|
|
@@ -130,7 +132,7 @@ describe('test middleware create factory', () => {
|
|
|
130
132
|
const port = await portfinder.getPortPromise();
|
|
131
133
|
const middlewares = createProxyHandler({
|
|
132
134
|
context: '/context',
|
|
133
|
-
target: `http://
|
|
135
|
+
target: `http://localhost:${sourceServerPort}`,
|
|
134
136
|
});
|
|
135
137
|
const proxyHandler = middlewares![0];
|
|
136
138
|
|
|
@@ -142,7 +144,7 @@ describe('test middleware create factory', () => {
|
|
|
142
144
|
}).listen(port);
|
|
143
145
|
|
|
144
146
|
try {
|
|
145
|
-
const { data } = await axios.get(`http://
|
|
147
|
+
const { data } = await axios.get(`http://localhost:${port}/context`);
|
|
146
148
|
expect(data).toBe('context');
|
|
147
149
|
} finally {
|
|
148
150
|
server.close();
|
|
@@ -154,7 +156,7 @@ describe('test middleware create factory', () => {
|
|
|
154
156
|
const middlewares = createProxyHandler([
|
|
155
157
|
{
|
|
156
158
|
context: '/array',
|
|
157
|
-
target: `http://
|
|
159
|
+
target: `http://localhost:${sourceServerPort}`,
|
|
158
160
|
},
|
|
159
161
|
]);
|
|
160
162
|
const proxyHandler = middlewares![0];
|
|
@@ -167,7 +169,7 @@ describe('test middleware create factory', () => {
|
|
|
167
169
|
}).listen(port);
|
|
168
170
|
|
|
169
171
|
try {
|
|
170
|
-
const { data } = await axios.get(`http://
|
|
172
|
+
const { data } = await axios.get(`http://localhost:${port}/array`);
|
|
171
173
|
expect(data).toBe('array');
|
|
172
174
|
} finally {
|
|
173
175
|
server.close();
|
|
@@ -175,4 +177,3 @@ describe('test middleware create factory', () => {
|
|
|
175
177
|
});
|
|
176
178
|
});
|
|
177
179
|
});
|
|
178
|
-
/* eslint-enable max-nested-callbacks */
|