@modern-js/server 2.0.0-beta.0 → 2.0.0-beta.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/CHANGELOG.md +47 -0
- package/dist/js/modern/dev-tools/dev-middleware/dev-server-plugin.js +4 -9
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +2 -5
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +43 -50
- package/dist/js/modern/dev-tools/dev-middleware/index.js +14 -32
- package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +18 -43
- package/dist/js/modern/dev-tools/https/index.js +0 -2
- package/dist/js/modern/dev-tools/mock/getMockData.js +4 -23
- package/dist/js/modern/dev-tools/mock/index.js +0 -8
- package/dist/js/modern/dev-tools/register/index.js +2 -18
- package/dist/js/modern/dev-tools/watcher/dependency-tree.js +5 -15
- package/dist/js/modern/dev-tools/watcher/index.js +0 -18
- package/dist/js/modern/dev-tools/watcher/stats-cache.js +0 -16
- package/dist/js/modern/index.js +0 -1
- package/dist/js/modern/server/dev-server.js +36 -56
- package/dist/js/modern/server/index.js +0 -4
- package/dist/js/node/constants.js +0 -3
- package/dist/js/node/dev-tools/dev-middleware/dev-server-plugin.js +4 -11
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +2 -8
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +41 -53
- package/dist/js/node/dev-tools/dev-middleware/index.js +14 -38
- package/dist/js/node/dev-tools/dev-middleware/socket-server.js +18 -48
- package/dist/js/node/dev-tools/https/index.js +0 -7
- package/dist/js/node/dev-tools/mock/getMockData.js +4 -29
- package/dist/js/node/dev-tools/mock/index.js +0 -17
- package/dist/js/node/dev-tools/register/index.js +2 -27
- package/dist/js/node/dev-tools/watcher/dependency-tree.js +5 -19
- package/dist/js/node/dev-tools/watcher/index.js +0 -32
- package/dist/js/node/dev-tools/watcher/stats-cache.js +0 -23
- package/dist/js/node/index.js +0 -4
- package/dist/js/node/server/dev-server.js +36 -75
- package/dist/js/node/server/index.js +0 -7
- package/dist/types/dev-tools/mock/getMockData.d.ts +0 -2
- package/dist/types/dev-tools/watcher/dependency-tree.d.ts +0 -2
- package/dist/types/index.d.ts +0 -2
- package/package.json +11 -11
|
@@ -4,26 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _events = require("events");
|
|
9
|
-
|
|
10
8
|
var _webpackDevMiddleware = _interopRequireDefault(require("@modern-js/utils/webpack-dev-middleware"));
|
|
11
|
-
|
|
12
9
|
var _devServerPlugin = _interopRequireDefault(require("./dev-server-plugin"));
|
|
13
|
-
|
|
14
10
|
var _socketServer = _interopRequireDefault(require("./socket-server"));
|
|
15
|
-
|
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
12
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
19
|
-
|
|
20
13
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
21
|
-
|
|
22
14
|
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; }
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
const noop = () => {
|
|
16
|
+
// noop
|
|
25
17
|
};
|
|
26
|
-
|
|
27
18
|
class DevMiddleware extends _events.EventEmitter {
|
|
28
19
|
constructor({
|
|
29
20
|
compiler,
|
|
@@ -31,48 +22,43 @@ class DevMiddleware extends _events.EventEmitter {
|
|
|
31
22
|
devMiddleware
|
|
32
23
|
}) {
|
|
33
24
|
super();
|
|
34
|
-
|
|
35
25
|
_defineProperty(this, "middleware", void 0);
|
|
36
|
-
|
|
37
26
|
_defineProperty(this, "compiler", void 0);
|
|
38
|
-
|
|
39
27
|
_defineProperty(this, "devOptions", void 0);
|
|
40
|
-
|
|
41
28
|
_defineProperty(this, "socketServer", void 0);
|
|
42
|
-
|
|
43
29
|
this.compiler = compiler;
|
|
44
|
-
this.devOptions = dev;
|
|
30
|
+
this.devOptions = dev;
|
|
45
31
|
|
|
46
|
-
|
|
32
|
+
// init socket server
|
|
33
|
+
this.socketServer = new _socketServer.default(dev);
|
|
47
34
|
|
|
35
|
+
// Todo: should remove after abstract dev middleware
|
|
48
36
|
if (this.compiler) {
|
|
49
37
|
// setup compiler in server, also add dev-middleware to handler static file in memory
|
|
50
38
|
// set up plugin to each compiler
|
|
51
|
-
this.setupDevServerPlugin();
|
|
52
|
-
|
|
53
|
-
this.setupHooks();
|
|
54
|
-
|
|
39
|
+
this.setupDevServerPlugin();
|
|
40
|
+
// register hooks for each compilation, update socket stats if recompiled
|
|
41
|
+
this.setupHooks();
|
|
42
|
+
// start dev middleware
|
|
55
43
|
this.middleware = this.setupDevMiddleware(devMiddleware);
|
|
56
44
|
}
|
|
57
45
|
}
|
|
58
|
-
|
|
59
46
|
init(app) {
|
|
60
47
|
app.on('listening', () => {
|
|
61
48
|
this.socketServer.prepare(app);
|
|
62
49
|
});
|
|
63
50
|
app.on('close', async () => {
|
|
64
51
|
var _this$middleware;
|
|
65
|
-
|
|
66
52
|
(_this$middleware = this.middleware) === null || _this$middleware === void 0 ? void 0 : _this$middleware.close(noop);
|
|
67
53
|
this.socketServer.close();
|
|
68
54
|
});
|
|
69
55
|
}
|
|
70
|
-
|
|
71
56
|
setupDevServerPlugin() {
|
|
72
57
|
const {
|
|
73
58
|
devOptions
|
|
74
|
-
} = this;
|
|
59
|
+
} = this;
|
|
75
60
|
|
|
61
|
+
// apply dev server to client compiler, add hmr client to entry.
|
|
76
62
|
if (this.compiler.compilers) {
|
|
77
63
|
this.compiler.compilers.forEach(target => {
|
|
78
64
|
if (this.isClientCompiler(target)) {
|
|
@@ -83,21 +69,17 @@ class DevMiddleware extends _events.EventEmitter {
|
|
|
83
69
|
new _devServerPlugin.default(devOptions).apply(this.compiler);
|
|
84
70
|
}
|
|
85
71
|
}
|
|
86
|
-
|
|
87
72
|
sockWrite(type, data) {
|
|
88
73
|
this.socketServer.sockWrite(type, data);
|
|
89
74
|
}
|
|
90
|
-
|
|
91
75
|
setupHooks() {
|
|
92
76
|
const invalidPlugin = () => {
|
|
93
77
|
this.socketServer.sockWrite('invalid');
|
|
94
78
|
};
|
|
95
|
-
|
|
96
79
|
const addHooks = compiler => {
|
|
97
80
|
if (compiler.name === 'server') {
|
|
98
81
|
return;
|
|
99
82
|
}
|
|
100
|
-
|
|
101
83
|
const {
|
|
102
84
|
compile,
|
|
103
85
|
invalid,
|
|
@@ -110,14 +92,12 @@ class DevMiddleware extends _events.EventEmitter {
|
|
|
110
92
|
this.emit('change', stats);
|
|
111
93
|
});
|
|
112
94
|
};
|
|
113
|
-
|
|
114
95
|
if (this.compiler.compilers) {
|
|
115
96
|
this.compiler.compilers.forEach(addHooks);
|
|
116
97
|
} else {
|
|
117
98
|
addHooks(this.compiler);
|
|
118
99
|
}
|
|
119
100
|
}
|
|
120
|
-
|
|
121
101
|
setupDevMiddleware(devMiddleware = _webpackDevMiddleware.default) {
|
|
122
102
|
const {
|
|
123
103
|
devOptions
|
|
@@ -128,23 +108,19 @@ class DevMiddleware extends _events.EventEmitter {
|
|
|
128
108
|
}, devOptions.devMiddleware));
|
|
129
109
|
return middleware;
|
|
130
110
|
}
|
|
131
|
-
|
|
132
111
|
isClientCompiler(compiler) {
|
|
133
112
|
const {
|
|
134
113
|
target
|
|
135
|
-
} = compiler.options;
|
|
114
|
+
} = compiler.options;
|
|
136
115
|
|
|
116
|
+
// if target not contains `node`, it's a client compiler
|
|
137
117
|
if (target) {
|
|
138
118
|
if (Array.isArray(target)) {
|
|
139
119
|
return !target.includes('node');
|
|
140
120
|
}
|
|
141
|
-
|
|
142
121
|
return target !== 'node';
|
|
143
122
|
}
|
|
144
|
-
|
|
145
123
|
return compiler.name === 'client';
|
|
146
124
|
}
|
|
147
|
-
|
|
148
125
|
}
|
|
149
|
-
|
|
150
126
|
exports.default = DevMiddleware;
|
|
@@ -4,47 +4,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _ws = _interopRequireDefault(require("ws"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
10
|
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; }
|
|
15
|
-
|
|
16
11
|
class SocketServer {
|
|
17
12
|
constructor(options) {
|
|
18
13
|
_defineProperty(this, "wsServer", void 0);
|
|
19
|
-
|
|
20
14
|
_defineProperty(this, "sockets", []);
|
|
21
|
-
|
|
22
15
|
_defineProperty(this, "options", void 0);
|
|
23
|
-
|
|
24
16
|
_defineProperty(this, "app", void 0);
|
|
25
|
-
|
|
26
17
|
_defineProperty(this, "stats", void 0);
|
|
27
|
-
|
|
28
18
|
_defineProperty(this, "timer", null);
|
|
29
|
-
|
|
30
19
|
this.options = options;
|
|
31
|
-
}
|
|
32
|
-
|
|
20
|
+
}
|
|
33
21
|
|
|
22
|
+
// create socket, install socket handler, bind socket event
|
|
34
23
|
prepare(app) {
|
|
35
24
|
var _this$options$client;
|
|
36
|
-
|
|
37
25
|
this.app = app;
|
|
38
26
|
this.wsServer = new _ws.default.Server({
|
|
39
27
|
noServer: true,
|
|
40
28
|
path: (_this$options$client = this.options.client) === null || _this$options$client === void 0 ? void 0 : _this$options$client.path
|
|
41
|
-
});
|
|
29
|
+
});
|
|
42
30
|
|
|
31
|
+
// listen upgrade event to handle socket
|
|
43
32
|
this.app.on('upgrade', (req, sock, head) => {
|
|
44
33
|
if (!this.wsServer.shouldHandle(req)) {
|
|
45
34
|
return;
|
|
46
35
|
}
|
|
47
|
-
|
|
48
36
|
this.wsServer.handleUpgrade(req, sock, head, connection => {
|
|
49
37
|
this.wsServer.emit('connection', connection, req);
|
|
50
38
|
});
|
|
@@ -56,12 +44,12 @@ class SocketServer {
|
|
|
56
44
|
this.timer = setInterval(() => {
|
|
57
45
|
this.wsServer.clients.forEach(socket => {
|
|
58
46
|
const extWs = socket;
|
|
59
|
-
|
|
60
47
|
if (!extWs.isAlive) {
|
|
61
48
|
extWs.terminate();
|
|
62
49
|
} else {
|
|
63
50
|
extWs.isAlive = false;
|
|
64
|
-
extWs.ping(() => {
|
|
51
|
+
extWs.ping(() => {
|
|
52
|
+
// empty
|
|
65
53
|
});
|
|
66
54
|
}
|
|
67
55
|
});
|
|
@@ -70,13 +58,12 @@ class SocketServer {
|
|
|
70
58
|
this.onConnect(socket);
|
|
71
59
|
});
|
|
72
60
|
}
|
|
73
|
-
|
|
74
61
|
updateStats(stats) {
|
|
75
62
|
this.stats = stats;
|
|
76
63
|
this.sendStats();
|
|
77
|
-
}
|
|
78
|
-
|
|
64
|
+
}
|
|
79
65
|
|
|
66
|
+
// write message to each socket
|
|
80
67
|
sockWrite(type, data) {
|
|
81
68
|
this.sockets.forEach(socket => {
|
|
82
69
|
this.send(socket, JSON.stringify({
|
|
@@ -85,67 +72,56 @@ class SocketServer {
|
|
|
85
72
|
}));
|
|
86
73
|
});
|
|
87
74
|
}
|
|
88
|
-
|
|
89
75
|
singleWrite(socket, type, data) {
|
|
90
76
|
this.send(socket, JSON.stringify({
|
|
91
77
|
type,
|
|
92
78
|
data
|
|
93
79
|
}));
|
|
94
80
|
}
|
|
95
|
-
|
|
96
81
|
close() {
|
|
97
82
|
this.sockets.forEach(socket => {
|
|
98
83
|
socket.close();
|
|
99
84
|
});
|
|
100
|
-
|
|
101
85
|
if (this.timer) {
|
|
102
86
|
clearInterval(this.timer);
|
|
103
87
|
this.timer = null;
|
|
104
88
|
}
|
|
105
89
|
}
|
|
106
|
-
|
|
107
90
|
onConnect(socket) {
|
|
108
91
|
const connection = socket;
|
|
109
92
|
connection.isAlive = true;
|
|
110
93
|
connection.on('pong', () => {
|
|
111
94
|
connection.isAlive = true;
|
|
112
95
|
});
|
|
113
|
-
|
|
114
96
|
if (!connection) {
|
|
115
97
|
return;
|
|
116
98
|
}
|
|
117
|
-
|
|
118
99
|
this.sockets.push(connection);
|
|
119
100
|
connection.on('close', () => {
|
|
120
101
|
const idx = this.sockets.indexOf(connection);
|
|
121
|
-
|
|
122
102
|
if (idx >= 0) {
|
|
123
103
|
this.sockets.splice(idx, 1);
|
|
124
104
|
}
|
|
125
105
|
});
|
|
126
|
-
|
|
127
106
|
if (this.options.hot || this.options.hot === 'only') {
|
|
128
107
|
this.singleWrite(connection, 'hot');
|
|
129
108
|
}
|
|
130
|
-
|
|
131
109
|
if (this.options.liveReload) {
|
|
132
110
|
this.singleWrite(connection, 'liveReload');
|
|
133
|
-
}
|
|
134
|
-
|
|
111
|
+
}
|
|
135
112
|
|
|
113
|
+
// send first stats to active client sock if stats exist
|
|
136
114
|
if (this.stats) {
|
|
137
115
|
this.sendStats(true);
|
|
138
116
|
}
|
|
139
|
-
}
|
|
140
|
-
|
|
117
|
+
}
|
|
141
118
|
|
|
119
|
+
// get standard stats
|
|
142
120
|
getStats() {
|
|
143
121
|
const curStats = this.stats;
|
|
144
|
-
|
|
145
122
|
if (!curStats) {
|
|
146
123
|
return null;
|
|
147
124
|
}
|
|
148
|
-
|
|
149
125
|
const defaultStats = {
|
|
150
126
|
all: false,
|
|
151
127
|
hash: true,
|
|
@@ -155,24 +131,21 @@ class SocketServer {
|
|
|
155
131
|
errorDetails: false
|
|
156
132
|
};
|
|
157
133
|
return curStats.toJson(defaultStats);
|
|
158
|
-
}
|
|
159
|
-
|
|
134
|
+
}
|
|
160
135
|
|
|
136
|
+
// determine what message should send by stats
|
|
161
137
|
sendStats(force = false) {
|
|
162
|
-
const stats = this.getStats();
|
|
138
|
+
const stats = this.getStats();
|
|
163
139
|
|
|
140
|
+
// this should never happened
|
|
164
141
|
if (!stats) {
|
|
165
142
|
return null;
|
|
166
143
|
}
|
|
167
|
-
|
|
168
144
|
const shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(asset => !asset.emitted);
|
|
169
|
-
|
|
170
145
|
if (shouldEmit) {
|
|
171
146
|
return this.sockWrite('still-ok');
|
|
172
147
|
}
|
|
173
|
-
|
|
174
148
|
this.sockWrite('hash', stats.hash);
|
|
175
|
-
|
|
176
149
|
if (stats.errors && stats.errors.length > 0) {
|
|
177
150
|
return this.sockWrite('errors', stats.errors);
|
|
178
151
|
} else if (stats.warnings && stats.warnings.length > 0) {
|
|
@@ -180,17 +153,14 @@ class SocketServer {
|
|
|
180
153
|
} else {
|
|
181
154
|
return this.sockWrite('ok');
|
|
182
155
|
}
|
|
183
|
-
}
|
|
184
|
-
|
|
156
|
+
}
|
|
185
157
|
|
|
158
|
+
// send message to connecting socket
|
|
186
159
|
send(connection, message) {
|
|
187
160
|
if (connection.readyState !== 1) {
|
|
188
161
|
return;
|
|
189
162
|
}
|
|
190
|
-
|
|
191
163
|
connection.send(message);
|
|
192
164
|
}
|
|
193
|
-
|
|
194
165
|
}
|
|
195
|
-
|
|
196
166
|
exports.default = SocketServer;
|
|
@@ -4,22 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.genHttpsOptions = void 0;
|
|
7
|
-
|
|
8
7
|
var devcert = _interopRequireWildcard(require("devcert"));
|
|
9
|
-
|
|
10
8
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
|
|
12
9
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
-
|
|
14
10
|
const genHttpsOptions = async userOptions => {
|
|
15
11
|
const httpsOptions = typeof userOptions === 'boolean' ? {} : userOptions;
|
|
16
|
-
|
|
17
12
|
if (!httpsOptions.key || !httpsOptions.cert) {
|
|
18
13
|
const selfsign = await devcert.certificateFor(['localhost']);
|
|
19
14
|
return selfsign;
|
|
20
15
|
}
|
|
21
|
-
|
|
22
16
|
return httpsOptions;
|
|
23
17
|
};
|
|
24
|
-
|
|
25
18
|
exports.genHttpsOptions = genHttpsOptions;
|
|
@@ -4,22 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getMatched = exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _utils = require("@modern-js/utils");
|
|
9
|
-
|
|
10
8
|
var _pathToRegexp = require("path-to-regexp");
|
|
11
|
-
|
|
12
9
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
10
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
-
|
|
16
11
|
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; }
|
|
17
|
-
|
|
18
12
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
-
const VALID_METHODS = ['get', 'post', 'put', 'delete', 'patch'];
|
|
20
|
-
|
|
13
|
+
const VALID_METHODS = ['get', 'post', 'put', 'delete', 'patch'];
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
15
|
const BODY_PARSED_METHODS = ['post', 'put', 'delete', 'patch'];
|
|
22
|
-
|
|
23
16
|
const createFunctionDataHandler = (method, handler) => async (context, next) => {
|
|
24
17
|
const {
|
|
25
18
|
req,
|
|
@@ -27,7 +20,6 @@ const createFunctionDataHandler = (method, handler) => async (context, next) =>
|
|
|
27
20
|
} = context;
|
|
28
21
|
return handler(req, res, next);
|
|
29
22
|
};
|
|
30
|
-
|
|
31
23
|
const createStaticDataHandler = (method, handler) => context => {
|
|
32
24
|
const {
|
|
33
25
|
res
|
|
@@ -35,19 +27,14 @@ const createStaticDataHandler = (method, handler) => context => {
|
|
|
35
27
|
res.setHeader('Content-Type', 'application/json');
|
|
36
28
|
res.end(JSON.stringify(handler));
|
|
37
29
|
};
|
|
38
|
-
|
|
39
30
|
const allowTypes = ['object', 'function'];
|
|
40
|
-
|
|
41
31
|
const normalizeConfig = mockConfig => Object.keys(mockConfig).reduce((acc, key) => {
|
|
42
32
|
const handler = mockConfig[key];
|
|
43
33
|
const type = typeof handler;
|
|
44
|
-
|
|
45
34
|
if (!allowTypes.includes(type)) {
|
|
46
35
|
throw new Error(`mock value of ${key} should be object or function, but got ${type}`);
|
|
47
36
|
}
|
|
48
|
-
|
|
49
37
|
const meta = parseKey(key);
|
|
50
|
-
|
|
51
38
|
if (type === 'object') {
|
|
52
39
|
acc.push(_objectSpread(_objectSpread({}, meta), {}, {
|
|
53
40
|
handler: createStaticDataHandler(meta.method, handler)
|
|
@@ -57,44 +44,35 @@ const normalizeConfig = mockConfig => Object.keys(mockConfig).reduce((acc, key)
|
|
|
57
44
|
handler: createFunctionDataHandler(meta.method, handler)
|
|
58
45
|
}));
|
|
59
46
|
}
|
|
60
|
-
|
|
61
47
|
return acc;
|
|
62
48
|
}, []);
|
|
63
|
-
|
|
64
49
|
const _blank = ' ';
|
|
65
|
-
|
|
66
50
|
const parseKey = key => {
|
|
67
51
|
// 'Method /pathname' | '/pathname'
|
|
68
52
|
const splitted = key.split(_blank).filter(Boolean);
|
|
69
|
-
|
|
70
53
|
if (splitted.length > 1) {
|
|
71
54
|
const [method, pathname] = splitted;
|
|
72
55
|
return {
|
|
73
56
|
method: method.toLowerCase(),
|
|
74
57
|
path: pathname
|
|
75
58
|
};
|
|
76
|
-
}
|
|
77
|
-
|
|
59
|
+
}
|
|
78
60
|
|
|
61
|
+
// default return get method
|
|
79
62
|
return {
|
|
80
63
|
method: 'get',
|
|
81
64
|
path: key
|
|
82
65
|
};
|
|
83
66
|
};
|
|
84
|
-
|
|
85
67
|
var _default = filepath => {
|
|
86
68
|
const mockModule = (0, _utils.compatRequire)(filepath);
|
|
87
|
-
|
|
88
69
|
if (!mockModule) {
|
|
89
70
|
throw new Error(`Mock file ${filepath} parsed failed!`);
|
|
90
71
|
}
|
|
91
|
-
|
|
92
72
|
const data = normalizeConfig(mockModule);
|
|
93
73
|
return data;
|
|
94
74
|
};
|
|
95
|
-
|
|
96
75
|
exports.default = _default;
|
|
97
|
-
|
|
98
76
|
const getMatched = (context, mockApiList) => {
|
|
99
77
|
const {
|
|
100
78
|
path: targetPathname,
|
|
@@ -105,17 +83,14 @@ const getMatched = (context, mockApiList) => {
|
|
|
105
83
|
method,
|
|
106
84
|
path: pathname
|
|
107
85
|
} = mockApi;
|
|
108
|
-
|
|
109
86
|
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
110
87
|
return (0, _pathToRegexp.match)(pathname, {
|
|
111
88
|
encode: encodeURI,
|
|
112
89
|
decode: decodeURIComponent
|
|
113
90
|
})(targetPathname);
|
|
114
91
|
}
|
|
115
|
-
|
|
116
92
|
return false;
|
|
117
93
|
});
|
|
118
94
|
return matched;
|
|
119
95
|
};
|
|
120
|
-
|
|
121
96
|
exports.getMatched = getMatched;
|
|
@@ -4,59 +4,42 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.createMockHandler = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _prodServer = require("@modern-js/prod-server");
|
|
13
|
-
|
|
14
10
|
var _getMockData = _interopRequireWildcard(require("./getMockData"));
|
|
15
|
-
|
|
16
11
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
-
|
|
18
12
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
-
|
|
20
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
14
|
const createMockHandler = ({
|
|
23
15
|
pwd
|
|
24
16
|
}) => {
|
|
25
17
|
const exts = ['.ts', '.js'];
|
|
26
18
|
let filepath = '';
|
|
27
|
-
|
|
28
19
|
for (const ext of exts) {
|
|
29
20
|
const maybeMatch = _path.default.join(pwd, `${_prodServer.AGGRED_DIR.mock}/index${ext}`);
|
|
30
|
-
|
|
31
21
|
if (_utils.fs.existsSync(maybeMatch)) {
|
|
32
22
|
filepath = maybeMatch;
|
|
33
23
|
break;
|
|
34
24
|
}
|
|
35
25
|
}
|
|
36
|
-
|
|
37
26
|
if (!filepath) {
|
|
38
27
|
return null;
|
|
39
28
|
}
|
|
40
|
-
|
|
41
29
|
const apiList = (0, _getMockData.default)(filepath);
|
|
42
|
-
|
|
43
30
|
if (!apiList || apiList.length === 0) {
|
|
44
31
|
return null;
|
|
45
32
|
}
|
|
46
|
-
|
|
47
33
|
return async (context, next) => {
|
|
48
34
|
const {
|
|
49
35
|
res
|
|
50
36
|
} = context;
|
|
51
37
|
const matched = (0, _getMockData.getMatched)(context, apiList);
|
|
52
|
-
|
|
53
38
|
if (!matched) {
|
|
54
39
|
return next();
|
|
55
40
|
}
|
|
56
|
-
|
|
57
41
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
58
42
|
return matched.handler(context, next);
|
|
59
43
|
};
|
|
60
44
|
};
|
|
61
|
-
|
|
62
45
|
exports.createMockHandler = createMockHandler;
|
|
@@ -4,57 +4,38 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.enableRegister = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _serverUtils = require("@modern-js/server-utils");
|
|
11
|
-
|
|
12
9
|
var _utils = require("@modern-js/utils");
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
11
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
-
|
|
18
12
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
|
-
|
|
20
13
|
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; }
|
|
21
|
-
|
|
22
14
|
const debug = (0, _utils.createDebugger)('server');
|
|
23
|
-
|
|
24
15
|
const checkDep = (depName, paths) => {
|
|
25
16
|
let packagePath = '';
|
|
26
|
-
|
|
27
17
|
try {
|
|
28
18
|
packagePath = require.resolve(depName, {
|
|
29
19
|
paths
|
|
30
20
|
});
|
|
31
21
|
} catch (error) {}
|
|
32
|
-
|
|
33
22
|
return Boolean(packagePath);
|
|
34
23
|
};
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
const enableRegister = (projectRoot, config
|
|
25
|
+
// eslint-disable-next-line consistent-return
|
|
37
26
|
) => {
|
|
38
27
|
const registerDirs = ['./api', './server', './config/mock', './shared'];
|
|
39
28
|
const TS_CONFIG_FILENAME = `tsconfig.json`;
|
|
40
|
-
|
|
41
29
|
const tsconfigPath = _path.default.resolve(projectRoot, TS_CONFIG_FILENAME);
|
|
42
|
-
|
|
43
30
|
const isTsProject = _utils.fs.existsSync(tsconfigPath);
|
|
44
|
-
|
|
45
31
|
const existTsNode = checkDep('ts-node', [projectRoot]);
|
|
46
32
|
const existTsConfigPaths = checkDep('tsconfig-paths', [projectRoot]);
|
|
47
|
-
|
|
48
33
|
if (isTsProject && existTsNode && existTsConfigPaths) {
|
|
49
34
|
var _config$output;
|
|
50
|
-
|
|
51
35
|
debug('use ts-node');
|
|
52
36
|
const distPath = (config === null || config === void 0 ? void 0 : (_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist';
|
|
53
|
-
|
|
54
37
|
const tsNode = require('ts-node');
|
|
55
|
-
|
|
56
38
|
const tsConfigPaths = require('tsconfig-paths');
|
|
57
|
-
|
|
58
39
|
const {
|
|
59
40
|
alias
|
|
60
41
|
} = config.source;
|
|
@@ -68,15 +49,12 @@ const enableRegister = (projectRoot, config // eslint-disable-next-line consiste
|
|
|
68
49
|
} = aliasConfig;
|
|
69
50
|
const tsPaths = Object.keys(paths).reduce((o, key) => {
|
|
70
51
|
let tsPath = paths[key];
|
|
71
|
-
|
|
72
52
|
if (typeof tsPath === 'string' && _path.default.isAbsolute(tsPath)) {
|
|
73
53
|
tsPath = _path.default.relative(absoluteBaseUrl, tsPath);
|
|
74
54
|
}
|
|
75
|
-
|
|
76
55
|
if (typeof tsPath === 'string') {
|
|
77
56
|
tsPath = [tsPath];
|
|
78
57
|
}
|
|
79
|
-
|
|
80
58
|
return _objectSpread(_objectSpread({}, o), {}, {
|
|
81
59
|
[`${key}`]: tsPath
|
|
82
60
|
});
|
|
@@ -94,7 +72,6 @@ const enableRegister = (projectRoot, config // eslint-disable-next-line consiste
|
|
|
94
72
|
});
|
|
95
73
|
} else {
|
|
96
74
|
var _config$tools, _config$server;
|
|
97
|
-
|
|
98
75
|
debug('use @babel/register');
|
|
99
76
|
const babelConfig = (0, _serverUtils.resolveBabelConfig)(projectRoot, _objectSpread(_objectSpread({}, config.source), {}, {
|
|
100
77
|
babelConfig: (_config$tools = config.tools) === null || _config$tools === void 0 ? void 0 : _config$tools.babel,
|
|
@@ -113,7 +90,6 @@ const enableRegister = (projectRoot, config // eslint-disable-next-line consiste
|
|
|
113
90
|
if (filePath.includes(`node_modules${_path.default.sep}.modern-js`)) {
|
|
114
91
|
return true;
|
|
115
92
|
}
|
|
116
|
-
|
|
117
93
|
return registerDirs.some(registerDir => filePath.startsWith(_path.default.join(projectRoot, registerDir)));
|
|
118
94
|
}],
|
|
119
95
|
extensions: ['.js', '.ts'],
|
|
@@ -123,5 +99,4 @@ const enableRegister = (projectRoot, config // eslint-disable-next-line consiste
|
|
|
123
99
|
}));
|
|
124
100
|
}
|
|
125
101
|
};
|
|
126
|
-
|
|
127
102
|
exports.enableRegister = enableRegister;
|