@modern-js/server 1.3.2 → 1.4.2
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 +49 -0
- package/dist/js/modern/dev-tools/babel/register.js +1 -0
- package/dist/js/modern/dev-tools/dev-server-plugin.js +1 -2
- package/dist/js/modern/dev-tools/mock/getMockData.js +24 -1
- package/dist/js/modern/dev-tools/mock/index.js +1 -26
- package/dist/js/modern/dev-tools/socket-server.js +65 -46
- package/dist/js/modern/dev-tools/watcher/index.js +3 -4
- package/dist/js/modern/dev-tools/watcher/stats-cache.js +32 -20
- package/dist/js/modern/libs/context/context.js +6 -0
- package/dist/js/modern/libs/hook-api/route.js +6 -4
- package/dist/js/modern/libs/render/index.js +1 -0
- package/dist/js/modern/libs/render/reader.js +1 -2
- package/dist/js/modern/libs/render/ssr.js +7 -2
- package/dist/js/modern/libs/route/index.js +0 -1
- package/dist/js/modern/libs/route/matcher.js +15 -3
- package/dist/js/modern/libs/route/route.js +1 -0
- package/dist/js/modern/server/dev-server/dev-server.js +7 -0
- package/dist/js/modern/server/index.js +5 -4
- package/dist/js/modern/server/modern-server-split.js +1 -1
- package/dist/js/modern/server/modern-server.js +13 -34
- package/dist/js/modern/utils.js +39 -0
- package/dist/js/node/dev-tools/babel/register.js +1 -0
- package/dist/js/node/dev-tools/dev-server-plugin.js +1 -2
- package/dist/js/node/dev-tools/mock/getMockData.js +29 -2
- package/dist/js/node/dev-tools/mock/index.js +5 -26
- package/dist/js/node/dev-tools/socket-server.js +65 -46
- package/dist/js/node/dev-tools/watcher/index.js +6 -2
- package/dist/js/node/dev-tools/watcher/stats-cache.js +33 -20
- package/dist/js/node/libs/context/context.js +6 -0
- package/dist/js/node/libs/hook-api/route.js +6 -4
- package/dist/js/node/libs/render/index.js +1 -0
- package/dist/js/node/libs/render/reader.js +2 -1
- package/dist/js/node/libs/render/ssr.js +8 -2
- package/dist/js/node/libs/route/index.js +0 -1
- package/dist/js/node/libs/route/matcher.js +16 -3
- package/dist/js/node/libs/route/route.js +1 -0
- package/dist/js/node/server/dev-server/dev-server.js +7 -0
- package/dist/js/node/server/index.js +9 -6
- package/dist/js/node/server/modern-server-split.js +1 -1
- package/dist/js/node/server/modern-server.js +12 -33
- package/dist/js/node/utils.js +51 -2
- package/dist/types/dev-tools/mock/getMockData.d.ts +2 -1
- package/dist/types/dev-tools/socket-server.d.ts +4 -1
- package/dist/types/dev-tools/watcher/index.d.ts +2 -1
- package/dist/types/dev-tools/watcher/stats-cache.d.ts +3 -2
- package/dist/types/libs/context/context.d.ts +2 -0
- package/dist/types/libs/hook-api/route.d.ts +3 -2
- package/dist/types/libs/render/reader.d.ts +13 -0
- package/dist/types/libs/render/ssr.d.ts +1 -0
- package/dist/types/libs/route/matcher.d.ts +1 -1
- package/dist/types/libs/route/route.d.ts +1 -0
- package/dist/types/server/dev-server/dev-server-split.d.ts +3 -3
- package/dist/types/server/modern-server-split.d.ts +3 -3
- package/dist/types/server/modern-server.d.ts +1 -2
- package/dist/types/type.d.ts +5 -3
- package/dist/types/utils.d.ts +5 -1
- package/package.json +9 -8
- package/tests/context.test.ts +12 -1
- package/tests/dev.test.ts +316 -5
- package/tests/fixtures/mock/exist/config/mock/index.ts +11 -0
- package/tests/fixtures/mock/zero/config/mock/index.ts +1 -0
- package/tests/fixtures/reader/index.ts +3 -0
- package/tests/fixtures/route-spec/dynamic.json +13 -0
- package/tests/fixtures/ssr/bundle.js +5 -0
- package/tests/fixtures/static-dir/bar.html +11 -0
- package/tests/fixtures/static-dir/baz/index.html +11 -0
- package/tests/fixtures/static-dir/foo/index.html +11 -0
- package/tests/fixtures/watch/a.ts +3 -0
- package/tests/fixtures/watch/index.ts +5 -0
- package/tests/fixtures/watch/stats.txt +1 -0
- package/tests/hook.test.ts +1 -1
- package/tests/render.test.ts +102 -0
- package/tests/route.test.ts +26 -3
- package/tests/utils.test.ts +35 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,54 @@
|
|
|
1
1
|
# @modern-js/server
|
|
2
2
|
|
|
3
|
+
## 1.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 735b2a81: prevent ssr compiler to send socket message
|
|
8
|
+
- Updated dependencies [b376c8d6]
|
|
9
|
+
- Updated dependencies [e62c4efd]
|
|
10
|
+
- Updated dependencies [e2a8233f]
|
|
11
|
+
- @modern-js/core@1.4.2
|
|
12
|
+
|
|
13
|
+
## 1.4.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 2cfc4235: support cssPath/jsPath/mediaPath in production mode
|
|
18
|
+
- 8d55e234: fix: catch api error
|
|
19
|
+
- Updated dependencies [53aca274]
|
|
20
|
+
- Updated dependencies [78279953]
|
|
21
|
+
- Updated dependencies [e116ace5]
|
|
22
|
+
- Updated dependencies [4d72edea]
|
|
23
|
+
- @modern-js/core@1.4.1
|
|
24
|
+
- @modern-js/utils@1.3.1
|
|
25
|
+
|
|
26
|
+
## 1.4.0
|
|
27
|
+
|
|
28
|
+
### Minor Changes
|
|
29
|
+
|
|
30
|
+
- ec4dbffb: feat: support as a pure api service
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- 816fd721: support more server context
|
|
35
|
+
- d9cc5ea9: support resatrt options transfer
|
|
36
|
+
- bfbea9a7: support multi base url and dynamic base url
|
|
37
|
+
- d099e5c5: fix error when modify modern.config.js
|
|
38
|
+
- 24f616ca: feat: support custom meta info
|
|
39
|
+
- 272cab15: refactor server plugin manager
|
|
40
|
+
- Updated dependencies [d9cc5ea9]
|
|
41
|
+
- Updated dependencies [bd819a8d]
|
|
42
|
+
- Updated dependencies [ec4dbffb]
|
|
43
|
+
- Updated dependencies [d099e5c5]
|
|
44
|
+
- Updated dependencies [bada2879]
|
|
45
|
+
- Updated dependencies [24f616ca]
|
|
46
|
+
- Updated dependencies [bd819a8d]
|
|
47
|
+
- Updated dependencies [272cab15]
|
|
48
|
+
- @modern-js/core@1.4.0
|
|
49
|
+
- @modern-js/utils@1.3.0
|
|
50
|
+
- @modern-js/server-core@1.2.2
|
|
51
|
+
|
|
3
52
|
## 1.3.2
|
|
4
53
|
|
|
5
54
|
### Patch Changes
|
|
@@ -18,6 +18,7 @@ export const enableRegister = (projectRoot, config) => {
|
|
|
18
18
|
return require('@babel/register')(_objectSpread(_objectSpread({}, babelConfig), {}, {
|
|
19
19
|
only: [function (filePath) {
|
|
20
20
|
// TODO: wait params
|
|
21
|
+
// FIXME: 删除hardcode,根据 AppContext 中的 metaName 设置路径
|
|
21
22
|
if (filePath.includes(`node_modules${path.sep}.modern-js`)) {
|
|
22
23
|
return true;
|
|
23
24
|
}
|
|
@@ -14,8 +14,7 @@ export default class DevServerPlugin {
|
|
|
14
14
|
} = this;
|
|
15
15
|
const host = `&host=${options.client.host || 'localhost'}`;
|
|
16
16
|
const path = `&path=${options.client.path}`;
|
|
17
|
-
const port = `&port=${options.client.port}`;
|
|
18
|
-
|
|
17
|
+
const port = `&port=${options.client.port}`;
|
|
19
18
|
const clientEntry = `${require.resolve('@modern-js/hmr-client')}?${host}${path}${port}`;
|
|
20
19
|
|
|
21
20
|
const hotEntry = require.resolve('webpack/hot/dev-server');
|
|
@@ -5,6 +5,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
|
|
7
7
|
import { compatRequire } from '@modern-js/utils';
|
|
8
|
+
import { match } from 'path-to-regexp';
|
|
8
9
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
9
10
|
const VALID_METHODS = ['get', 'post', 'put', 'delete', 'patch']; // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10
11
|
|
|
@@ -81,4 +82,26 @@ export default (filepath => {
|
|
|
81
82
|
|
|
82
83
|
const data = normalizeConfig(mockModule);
|
|
83
84
|
return data;
|
|
84
|
-
});
|
|
85
|
+
});
|
|
86
|
+
export const getMatched = (context, mockApiList) => {
|
|
87
|
+
const {
|
|
88
|
+
path: targetPathname,
|
|
89
|
+
method: targetMethod
|
|
90
|
+
} = context;
|
|
91
|
+
const matched = mockApiList.find(mockApi => {
|
|
92
|
+
const {
|
|
93
|
+
method,
|
|
94
|
+
path: pathname
|
|
95
|
+
} = mockApi;
|
|
96
|
+
|
|
97
|
+
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
98
|
+
return match(pathname, {
|
|
99
|
+
encode: encodeURI,
|
|
100
|
+
decode: decodeURIComponent
|
|
101
|
+
})(targetPathname);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return false;
|
|
105
|
+
});
|
|
106
|
+
return matched;
|
|
107
|
+
};
|
|
@@ -1,32 +1,7 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { fs } from '@modern-js/utils';
|
|
3
|
-
import { match } from 'path-to-regexp';
|
|
4
3
|
import { AGGRED_DIR } from "../../constants";
|
|
5
|
-
import getMockData from "./getMockData";
|
|
6
|
-
|
|
7
|
-
const getMatched = (context, mockApiList) => {
|
|
8
|
-
const {
|
|
9
|
-
path: targetPathname,
|
|
10
|
-
method: targetMethod
|
|
11
|
-
} = context;
|
|
12
|
-
const matched = mockApiList.find(mockApi => {
|
|
13
|
-
const {
|
|
14
|
-
method,
|
|
15
|
-
path: pathname
|
|
16
|
-
} = mockApi;
|
|
17
|
-
|
|
18
|
-
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
19
|
-
return match(pathname, {
|
|
20
|
-
encode: encodeURI,
|
|
21
|
-
decode: decodeURIComponent
|
|
22
|
-
})(targetPathname);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return false;
|
|
26
|
-
});
|
|
27
|
-
return matched;
|
|
28
|
-
};
|
|
29
|
-
|
|
4
|
+
import getMockData, { getMatched } from "./getMockData";
|
|
30
5
|
export const createMockHandler = ({
|
|
31
6
|
pwd
|
|
32
7
|
}) => {
|
|
@@ -8,6 +8,7 @@ export default class SocketServer {
|
|
|
8
8
|
this.options = void 0;
|
|
9
9
|
this.app = void 0;
|
|
10
10
|
this.stats = void 0;
|
|
11
|
+
this.timer = null;
|
|
11
12
|
this.options = options;
|
|
12
13
|
} // create socket, install socket handler, bind socket event
|
|
13
14
|
|
|
@@ -32,7 +33,7 @@ export default class SocketServer {
|
|
|
32
33
|
// only dev server, use default logger
|
|
33
34
|
logger.error(err);
|
|
34
35
|
});
|
|
35
|
-
setInterval(() => {
|
|
36
|
+
this.timer = setInterval(() => {
|
|
36
37
|
this.wsServer.clients.forEach(socket => {
|
|
37
38
|
const extWs = socket;
|
|
38
39
|
|
|
@@ -45,49 +46,7 @@ export default class SocketServer {
|
|
|
45
46
|
});
|
|
46
47
|
}, 30000);
|
|
47
48
|
this.wsServer.on('connection', socket => {
|
|
48
|
-
|
|
49
|
-
connection.isAlive = true;
|
|
50
|
-
connection.on('pong', () => {
|
|
51
|
-
connection.isAlive = true;
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
if (!connection) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
this.sockets.push(connection);
|
|
59
|
-
connection.on('close', () => {
|
|
60
|
-
const idx = this.sockets.indexOf(connection);
|
|
61
|
-
|
|
62
|
-
if (idx >= 0) {
|
|
63
|
-
this.sockets.splice(idx, 1);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
if (this.options.client.logging) {
|
|
68
|
-
this.sockWrite('logging', this.options.client.logging);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (this.options.hot || this.options.hot === 'only') {
|
|
72
|
-
this.sockWrite('hot');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (this.options.liveReload) {
|
|
76
|
-
this.sockWrite('liveReload');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (this.options.client.progress) {
|
|
80
|
-
this.sockWrite('progress', this.options.client.progress);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (this.options.client.overlay) {
|
|
84
|
-
this.sockWrite('overlay', this.options.client.overlay);
|
|
85
|
-
} // send first stats to active client sock if stats exist
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (this.stats) {
|
|
89
|
-
this.sendStats(true);
|
|
90
|
-
}
|
|
49
|
+
this.onConnect(socket);
|
|
91
50
|
});
|
|
92
51
|
}
|
|
93
52
|
|
|
@@ -106,8 +65,68 @@ export default class SocketServer {
|
|
|
106
65
|
});
|
|
107
66
|
}
|
|
108
67
|
|
|
109
|
-
|
|
110
|
-
|
|
68
|
+
singleWrite(socket, type, data) {
|
|
69
|
+
this.send(socket, JSON.stringify({
|
|
70
|
+
type,
|
|
71
|
+
data
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
close() {
|
|
76
|
+
this.sockets.forEach(socket => {
|
|
77
|
+
socket.close();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (this.timer) {
|
|
81
|
+
clearInterval(this.timer);
|
|
82
|
+
this.timer = null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
onConnect(socket) {
|
|
87
|
+
const connection = socket;
|
|
88
|
+
connection.isAlive = true;
|
|
89
|
+
connection.on('pong', () => {
|
|
90
|
+
connection.isAlive = true;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (!connection) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.sockets.push(connection);
|
|
98
|
+
connection.on('close', () => {
|
|
99
|
+
const idx = this.sockets.indexOf(connection);
|
|
100
|
+
|
|
101
|
+
if (idx >= 0) {
|
|
102
|
+
this.sockets.splice(idx, 1);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (this.options.client.logging) {
|
|
107
|
+
this.singleWrite(connection, 'logging', this.options.client.logging);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (this.options.hot || this.options.hot === 'only') {
|
|
111
|
+
this.singleWrite(connection, 'hot');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (this.options.liveReload) {
|
|
115
|
+
this.singleWrite(connection, 'liveReload');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (this.options.client.progress) {
|
|
119
|
+
this.singleWrite(connection, 'progress', this.options.client.progress);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (this.options.client.overlay) {
|
|
123
|
+
this.singleWrite(connection, 'overlay', this.options.client.overlay);
|
|
124
|
+
} // send first stats to active client sock if stats exist
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
if (this.stats) {
|
|
128
|
+
this.sendStats(true);
|
|
129
|
+
}
|
|
111
130
|
} // get standard stats
|
|
112
131
|
|
|
113
132
|
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
import path from 'path';
|
|
1
2
|
import chokidar from 'chokidar';
|
|
2
3
|
import { DependencyTree } from "./dependency-tree";
|
|
3
4
|
import { StatsCache } from "./stats-cache";
|
|
4
|
-
|
|
5
|
-
const getWatchedFiles = watcher => {
|
|
5
|
+
export const getWatchedFiles = watcher => {
|
|
6
6
|
const watched = watcher.getWatched();
|
|
7
7
|
const files = [];
|
|
8
8
|
Object.keys(watched).forEach(dir => {
|
|
9
9
|
watched[dir].forEach(fileName => {
|
|
10
|
-
files.push(
|
|
10
|
+
files.push(path.join(dir, fileName));
|
|
11
11
|
});
|
|
12
12
|
});
|
|
13
13
|
return files;
|
|
14
14
|
};
|
|
15
|
-
|
|
16
15
|
export default class Watcher {
|
|
17
16
|
constructor() {
|
|
18
17
|
this.dependencyTree = null;
|
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
+
import crypto from 'crypto';
|
|
2
3
|
export class StatsCache {
|
|
3
4
|
constructor() {
|
|
4
|
-
this.
|
|
5
|
+
this.cachedHash = {};
|
|
6
|
+
this.cachedSize = {};
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
add(files) {
|
|
8
10
|
const {
|
|
9
|
-
|
|
11
|
+
cachedHash,
|
|
12
|
+
cachedSize
|
|
10
13
|
} = this;
|
|
11
14
|
|
|
12
15
|
for (const filename of files) {
|
|
13
16
|
if (fs.existsSync(filename)) {
|
|
14
|
-
const
|
|
17
|
+
const stats = fs.statSync(filename);
|
|
15
18
|
|
|
16
|
-
if (
|
|
17
|
-
|
|
19
|
+
if (stats.isFile() && !cachedHash[filename]) {
|
|
20
|
+
cachedHash[filename] = this.hash(stats, filename);
|
|
21
|
+
cachedSize[filename] = stats.size;
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
}
|
|
@@ -22,32 +26,41 @@ export class StatsCache {
|
|
|
22
26
|
|
|
23
27
|
refresh(filename) {
|
|
24
28
|
const {
|
|
25
|
-
|
|
29
|
+
cachedHash,
|
|
30
|
+
cachedSize
|
|
26
31
|
} = this;
|
|
27
32
|
|
|
28
33
|
if (fs.existsSync(filename)) {
|
|
29
|
-
const
|
|
34
|
+
const stats = fs.statSync(filename);
|
|
30
35
|
|
|
31
|
-
if (
|
|
32
|
-
|
|
36
|
+
if (stats.isFile()) {
|
|
37
|
+
cachedHash[filename] = this.hash(stats, filename);
|
|
38
|
+
cachedSize[filename] = stats.size;
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
del(filename) {
|
|
38
|
-
if (this.
|
|
39
|
-
delete this.
|
|
44
|
+
if (this.cachedHash[filename]) {
|
|
45
|
+
delete this.cachedHash[filename];
|
|
46
|
+
delete this.cachedSize[filename];
|
|
40
47
|
}
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
isDiff(filename) {
|
|
44
51
|
const {
|
|
45
|
-
|
|
52
|
+
cachedHash,
|
|
53
|
+
cachedSize
|
|
46
54
|
} = this;
|
|
47
|
-
const
|
|
48
|
-
const
|
|
55
|
+
const stats = fs.statSync(filename);
|
|
56
|
+
const hash = cachedHash[filename];
|
|
57
|
+
const size = cachedSize[filename];
|
|
49
58
|
|
|
50
|
-
if (
|
|
59
|
+
if (stats.size !== size) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (this.hash(stats, filename) !== hash) {
|
|
51
64
|
return true;
|
|
52
65
|
}
|
|
53
66
|
|
|
@@ -55,12 +68,11 @@ export class StatsCache {
|
|
|
55
68
|
}
|
|
56
69
|
|
|
57
70
|
has(filename) {
|
|
58
|
-
return Boolean(this.
|
|
59
|
-
}
|
|
60
|
-
|
|
71
|
+
return Boolean(this.cachedHash[filename]);
|
|
72
|
+
}
|
|
61
73
|
|
|
62
|
-
|
|
63
|
-
return
|
|
74
|
+
hash(stats, filename) {
|
|
75
|
+
return crypto.createHash('md5').update(fs.readFileSync(filename)).digest('hex');
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
}
|
|
@@ -19,10 +19,12 @@ export class ModernServerContext {
|
|
|
19
19
|
this.params = {};
|
|
20
20
|
this.logger = void 0;
|
|
21
21
|
this.metrics = void 0;
|
|
22
|
+
this.serverData = void 0;
|
|
22
23
|
this.req = req;
|
|
23
24
|
this.res = res;
|
|
24
25
|
this.logger = req.logger;
|
|
25
26
|
this.metrics = req.metrics;
|
|
27
|
+
this.serverData = {};
|
|
26
28
|
this.bind();
|
|
27
29
|
}
|
|
28
30
|
|
|
@@ -41,6 +43,10 @@ export class ModernServerContext {
|
|
|
41
43
|
this.params = params;
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
setServerData(key, value) {
|
|
47
|
+
this.serverData[key] = value;
|
|
48
|
+
}
|
|
49
|
+
|
|
44
50
|
getReqHeader(key) {
|
|
45
51
|
const {
|
|
46
52
|
req
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
class RouteAPI {
|
|
2
|
-
constructor(matched, router) {
|
|
2
|
+
constructor(matched, router, url) {
|
|
3
3
|
this.router = void 0;
|
|
4
4
|
this.current = void 0;
|
|
5
|
+
this.url = void 0;
|
|
5
6
|
this.current = matched;
|
|
6
7
|
this.router = router;
|
|
8
|
+
this.url = url;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
cur() {
|
|
10
|
-
return this.current.generate();
|
|
12
|
+
return this.current.generate(this.url);
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
get(entryName) {
|
|
@@ -15,7 +17,7 @@ class RouteAPI {
|
|
|
15
17
|
router
|
|
16
18
|
} = this;
|
|
17
19
|
const matched = router.matchEntry(entryName);
|
|
18
|
-
return matched ? matched.generate() : null;
|
|
20
|
+
return matched ? matched.generate(this.url) : null;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
use(entryName) {
|
|
@@ -34,4 +36,4 @@ class RouteAPI {
|
|
|
34
36
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
export const createRouteAPI = (matched, router) => new RouteAPI(matched, router);
|
|
39
|
+
export const createRouteAPI = (matched, router, url) => new RouteAPI(matched, router, url);
|
|
@@ -14,7 +14,7 @@ const createCacheItem = async (filepath, mtime) => {
|
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
class LruReader {
|
|
17
|
+
export class LruReader {
|
|
18
18
|
// private timer?: NodeJS.Timeout;
|
|
19
19
|
constructor() {
|
|
20
20
|
this.cache = void 0;
|
|
@@ -96,7 +96,6 @@ class LruReader {
|
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
}
|
|
99
|
-
|
|
100
99
|
const reader = new LruReader();
|
|
101
100
|
export const readFile = async filepath => {
|
|
102
101
|
const file = await reader.read(filepath);
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils';
|
|
3
3
|
import mime from 'mime-types';
|
|
4
|
+
import cookie from 'cookie';
|
|
4
5
|
import cache from "./cache";
|
|
5
6
|
export const render = async (ctx, renderOptions, runner) => {
|
|
6
7
|
const {
|
|
8
|
+
urlPath,
|
|
7
9
|
bundle,
|
|
8
10
|
distDir,
|
|
9
11
|
template,
|
|
@@ -13,11 +15,14 @@ export const render = async (ctx, renderOptions, runner) => {
|
|
|
13
15
|
const bundleJS = path.join(distDir, bundle);
|
|
14
16
|
const context = {
|
|
15
17
|
request: {
|
|
18
|
+
baseUrl: urlPath,
|
|
16
19
|
params: ctx.params,
|
|
17
20
|
pathname: ctx.path,
|
|
21
|
+
host: ctx.host,
|
|
18
22
|
query: ctx.query,
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
url: ctx.href,
|
|
24
|
+
cookieMap: cookie.parse(ctx.headers.cookie || ''),
|
|
25
|
+
headers: ctx.headers
|
|
21
26
|
},
|
|
22
27
|
redirection: {},
|
|
23
28
|
template,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { removeTailSlash } from '@modern-js/utils';
|
|
2
2
|
import { match, pathToRegexp } from 'path-to-regexp';
|
|
3
|
+
import { toPath } from "../../utils";
|
|
3
4
|
import { ModernRoute } from "./route"; // eslint-disable-next-line no-useless-escape
|
|
4
5
|
|
|
5
6
|
const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
|
|
@@ -14,8 +15,16 @@ export class RouteMatcher {
|
|
|
14
15
|
} // generate modern route object
|
|
15
16
|
|
|
16
17
|
|
|
17
|
-
generate() {
|
|
18
|
-
|
|
18
|
+
generate(url) {
|
|
19
|
+
const route = new ModernRoute(this.spec);
|
|
20
|
+
|
|
21
|
+
if (this.urlPath) {
|
|
22
|
+
const params = this.parseURLParams(url);
|
|
23
|
+
route.urlPath = toPath(route.urlPath, params);
|
|
24
|
+
route.params = params;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return route;
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
parseURLParams(pathname) {
|
|
@@ -73,9 +82,12 @@ export class RouteMatcher {
|
|
|
73
82
|
|
|
74
83
|
if (useReg) {
|
|
75
84
|
this.urlMatcher = match(urlPath, {
|
|
85
|
+
end: false,
|
|
76
86
|
decode: decodeURIComponent
|
|
77
87
|
});
|
|
78
|
-
this.urlReg = pathToRegexp(urlPath
|
|
88
|
+
this.urlReg = pathToRegexp(urlPath, [], {
|
|
89
|
+
end: false
|
|
90
|
+
});
|
|
79
91
|
}
|
|
80
92
|
}
|
|
81
93
|
|
|
@@ -118,6 +118,8 @@ export class ModernDevServer extends ModernServer {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
async close() {
|
|
121
|
+
var _this$socketServer2;
|
|
122
|
+
|
|
121
123
|
super.close();
|
|
122
124
|
await this.watcher.close();
|
|
123
125
|
await new Promise(resolve => {
|
|
@@ -129,6 +131,7 @@ export class ModernDevServer extends ModernServer {
|
|
|
129
131
|
resolve();
|
|
130
132
|
}
|
|
131
133
|
});
|
|
134
|
+
(_this$socketServer2 = this.socketServer) === null || _this$socketServer2 === void 0 ? void 0 : _this$socketServer2.close();
|
|
132
135
|
}
|
|
133
136
|
|
|
134
137
|
async createHTTPServer(handler) {
|
|
@@ -180,6 +183,10 @@ export class ModernDevServer extends ModernServer {
|
|
|
180
183
|
};
|
|
181
184
|
|
|
182
185
|
const addHooks = compiler => {
|
|
186
|
+
if (compiler.name === 'server') {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
183
190
|
const {
|
|
184
191
|
compile,
|
|
185
192
|
invalid,
|
|
@@ -5,9 +5,9 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
|
|
7
7
|
import path from 'path';
|
|
8
|
-
import { serverManager } from '@modern-js/server-
|
|
9
|
-
import { logger as defaultLogger } from '@modern-js/utils';
|
|
10
|
-
import {
|
|
8
|
+
import { serverManager, AppContext, ConfigContext } from '@modern-js/server-core';
|
|
9
|
+
import { compatRequire, logger as defaultLogger } from '@modern-js/utils';
|
|
10
|
+
import { initAppContext, initAppDir, loadUserConfig } from '@modern-js/core';
|
|
11
11
|
import { metrics as defaultMetrics } from "../libs/metrics";
|
|
12
12
|
import { ModernServer } from "./modern-server";
|
|
13
13
|
import { ModernAPIServer, ModernSSRServer, ModernWebServer } from "./modern-server-split";
|
|
@@ -115,8 +115,9 @@ export class Server {
|
|
|
115
115
|
const {
|
|
116
116
|
options
|
|
117
117
|
} = this;
|
|
118
|
+
serverManager.clear();
|
|
118
119
|
(_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
|
|
119
|
-
serverManager.usePlugin(p);
|
|
120
|
+
serverManager.usePlugin(compatRequire(p.pluginPath));
|
|
120
121
|
});
|
|
121
122
|
const appContext = await this.initAppContext();
|
|
122
123
|
serverManager.run(() => {
|