@modern-js/server 2.48.5 → 2.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/createDevServer.js +95 -0
- package/dist/cjs/{server/index.js → helpers/devOptions.js} +11 -28
- package/dist/cjs/helpers/fileReader.js +51 -0
- package/dist/cjs/helpers/index.js +107 -0
- package/dist/cjs/{server/workerSSRRender.js → helpers/repack.js} +27 -21
- package/dist/cjs/helpers/utils.js +29 -0
- package/dist/cjs/index.js +3 -13
- package/dist/esm/createDevServer.js +99 -0
- package/dist/esm/helpers/devOptions.js +10 -0
- package/dist/esm/helpers/fileReader.js +45 -0
- package/dist/esm/helpers/index.js +119 -0
- package/dist/esm/helpers/repack.js +31 -0
- package/dist/esm/helpers/utils.js +5 -0
- package/dist/esm/index.js +2 -12
- package/dist/esm-node/createDevServer.js +61 -0
- package/dist/esm-node/helpers/devOptions.js +10 -0
- package/dist/esm-node/helpers/fileReader.js +27 -0
- package/dist/esm-node/helpers/index.js +69 -0
- package/dist/esm-node/helpers/repack.js +27 -0
- package/dist/esm-node/helpers/utils.js +5 -0
- package/dist/esm-node/index.js +2 -11
- package/dist/types/createDevServer.d.ts +6 -0
- package/dist/types/{constants.d.ts → helpers/constants.d.ts} +1 -1
- package/dist/types/helpers/devOptions.d.ts +4 -0
- package/dist/types/helpers/fileReader.d.ts +2 -0
- package/dist/types/helpers/index.d.ts +14 -0
- package/dist/types/helpers/repack.d.ts +3 -0
- package/dist/types/helpers/utils.d.ts +1 -0
- package/dist/types/index.d.ts +2 -6
- package/dist/types/types.d.ts +4 -11
- package/package.json +10 -11
- package/dist/cjs/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -61
- package/dist/cjs/dev-tools/dev-middleware/hmr-client/index.js +0 -159
- package/dist/cjs/dev-tools/dev-middleware/index.js +0 -90
- package/dist/cjs/dev-tools/dev-middleware/socketServer.js +0 -173
- package/dist/cjs/dev-tools/https/index.js +0 -54
- package/dist/cjs/dev-tools/mock/getMockData.js +0 -104
- package/dist/cjs/dev-tools/mock/index.js +0 -85
- package/dist/cjs/dev-tools/register/index.js +0 -144
- package/dist/cjs/server/devServer.js +0 -267
- package/dist/cjs/server/devServerOld.js +0 -346
- package/dist/esm/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -54
- package/dist/esm/dev-tools/dev-middleware/hmr-client/index.js +0 -162
- package/dist/esm/dev-tools/dev-middleware/index.js +0 -82
- package/dist/esm/dev-tools/dev-middleware/socketServer.js +0 -149
- package/dist/esm/dev-tools/https/index.js +0 -74
- package/dist/esm/dev-tools/mock/getMockData.js +0 -102
- package/dist/esm/dev-tools/mock/index.js +0 -88
- package/dist/esm/dev-tools/register/index.js +0 -109
- package/dist/esm/server/devServer.js +0 -449
- package/dist/esm/server/devServerOld.js +0 -519
- package/dist/esm/server/index.js +0 -45
- package/dist/esm/server/workerSSRRender.js +0 -43
- package/dist/esm-node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -36
- package/dist/esm-node/dev-tools/dev-middleware/hmr-client/index.js +0 -145
- package/dist/esm-node/dev-tools/dev-middleware/index.js +0 -60
- package/dist/esm-node/dev-tools/dev-middleware/socketServer.js +0 -143
- package/dist/esm-node/dev-tools/https/index.js +0 -30
- package/dist/esm-node/dev-tools/mock/getMockData.js +0 -80
- package/dist/esm-node/dev-tools/mock/index.js +0 -51
- package/dist/esm-node/dev-tools/register/index.js +0 -110
- package/dist/esm-node/server/devServer.js +0 -233
- package/dist/esm-node/server/devServerOld.js +0 -312
- package/dist/esm-node/server/index.js +0 -26
- package/dist/esm-node/server/workerSSRRender.js +0 -21
- package/dist/types/dev-tools/dev-middleware/hmr-client/createSocketUrl.d.ts +0 -7
- package/dist/types/dev-tools/dev-middleware/hmr-client/index.d.ts +0 -1
- package/dist/types/dev-tools/dev-middleware/index.d.ts +0 -21
- package/dist/types/dev-tools/dev-middleware/socketServer.d.ts +0 -25
- package/dist/types/dev-tools/https/index.d.ts +0 -7
- package/dist/types/dev-tools/mock/getMockData.d.ts +0 -18
- package/dist/types/dev-tools/mock/index.d.ts +0 -4
- package/dist/types/dev-tools/register/index.d.ts +0 -2
- package/dist/types/server/devServer.d.ts +0 -38
- package/dist/types/server/devServerOld.d.ts +0 -37
- package/dist/types/server/index.d.ts +0 -8
- package/dist/types/server/workerSSRRender.d.ts +0 -10
- /package/dist/cjs/{constants.js → helpers/constants.js} +0 -0
- /package/dist/esm/{constants.js → helpers/constants.js} +0 -0
- /package/dist/esm-node/{constants.js → helpers/constants.js} +0 -0
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
-
};
|
|
5
|
-
import stripAnsi from "@modern-js/utils/strip-ansi";
|
|
6
|
-
import { formatWebpackMessages } from "@modern-js/utils/universal/format-webpack";
|
|
7
|
-
import { createSocketUrl } from "./createSocketUrl";
|
|
8
|
-
var require_hmr_client = __commonJS({
|
|
9
|
-
"src/dev-tools/dev-middleware/hmr-client/index.ts"(exports, module) {
|
|
10
|
-
const hadRuntimeError = false;
|
|
11
|
-
const socketUrl = createSocketUrl(__resourceQuery);
|
|
12
|
-
const connection = new WebSocket(socketUrl);
|
|
13
|
-
connection.onopen = function() {
|
|
14
|
-
if (typeof console !== "undefined" && typeof console.info === "function") {
|
|
15
|
-
console.info("[HMR] connected.");
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
connection.onclose = function() {
|
|
19
|
-
if (typeof console !== "undefined" && typeof console.info === "function") {
|
|
20
|
-
console.info("[HMR] disconnected. Refresh the page if necessary.");
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
let isFirstCompilation = true;
|
|
24
|
-
let mostRecentCompilationHash = null;
|
|
25
|
-
let hasCompileErrors = false;
|
|
26
|
-
function clearOutdatedErrors() {
|
|
27
|
-
if (typeof console !== "undefined" && typeof console.clear === "function") {
|
|
28
|
-
if (hasCompileErrors) {
|
|
29
|
-
console.clear();
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function handleSuccess() {
|
|
34
|
-
clearOutdatedErrors();
|
|
35
|
-
const isHotUpdate = !isFirstCompilation;
|
|
36
|
-
isFirstCompilation = false;
|
|
37
|
-
hasCompileErrors = false;
|
|
38
|
-
if (isHotUpdate) {
|
|
39
|
-
tryApplyUpdates();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function handleWarnings(warnings) {
|
|
43
|
-
clearOutdatedErrors();
|
|
44
|
-
const isHotUpdate = !isFirstCompilation;
|
|
45
|
-
isFirstCompilation = false;
|
|
46
|
-
hasCompileErrors = false;
|
|
47
|
-
function printWarnings() {
|
|
48
|
-
const formatted = formatWebpackMessages({
|
|
49
|
-
warnings,
|
|
50
|
-
errors: []
|
|
51
|
-
});
|
|
52
|
-
if (typeof console !== "undefined" && typeof console.warn === "function") {
|
|
53
|
-
for (let i = 0; i < formatted.warnings.length; i++) {
|
|
54
|
-
if (i === 5) {
|
|
55
|
-
console.warn("There were more warnings in other files.\nYou can find a complete log in the terminal.");
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
console.warn(stripAnsi(formatted.warnings[i]));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
printWarnings();
|
|
63
|
-
if (isHotUpdate) {
|
|
64
|
-
tryApplyUpdates();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function handleErrors(errors) {
|
|
68
|
-
clearOutdatedErrors();
|
|
69
|
-
isFirstCompilation = false;
|
|
70
|
-
hasCompileErrors = true;
|
|
71
|
-
const formatted = formatWebpackMessages({
|
|
72
|
-
errors,
|
|
73
|
-
warnings: []
|
|
74
|
-
});
|
|
75
|
-
if (typeof console !== "undefined" && typeof console.error === "function") {
|
|
76
|
-
for (const error of formatted.errors) {
|
|
77
|
-
console.error(stripAnsi(error));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function handleAvailableHash(hash) {
|
|
82
|
-
mostRecentCompilationHash = hash;
|
|
83
|
-
}
|
|
84
|
-
connection.onmessage = function(e) {
|
|
85
|
-
const message = JSON.parse(e.data);
|
|
86
|
-
switch (message.type) {
|
|
87
|
-
case "hash":
|
|
88
|
-
handleAvailableHash(message.data);
|
|
89
|
-
break;
|
|
90
|
-
case "still-ok":
|
|
91
|
-
case "ok":
|
|
92
|
-
handleSuccess();
|
|
93
|
-
break;
|
|
94
|
-
case "content-changed":
|
|
95
|
-
window.location.reload();
|
|
96
|
-
break;
|
|
97
|
-
case "warnings":
|
|
98
|
-
handleWarnings(message.data);
|
|
99
|
-
break;
|
|
100
|
-
case "errors":
|
|
101
|
-
handleErrors(message.data);
|
|
102
|
-
break;
|
|
103
|
-
default:
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
function isUpdateAvailable() {
|
|
107
|
-
return mostRecentCompilationHash !== __webpack_hash__;
|
|
108
|
-
}
|
|
109
|
-
function canApplyUpdates() {
|
|
110
|
-
return module.hot.status() === "idle";
|
|
111
|
-
}
|
|
112
|
-
function tryApplyUpdates() {
|
|
113
|
-
if (!module.hot) {
|
|
114
|
-
window.location.reload();
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
if (!isUpdateAvailable() || !canApplyUpdates()) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
function handleApplyUpdates(err, updatedModules) {
|
|
121
|
-
const wantsForcedReload = err || !updatedModules || hadRuntimeError;
|
|
122
|
-
if (wantsForcedReload) {
|
|
123
|
-
window.location.reload();
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
if (isUpdateAvailable()) {
|
|
127
|
-
tryApplyUpdates();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const result = module.hot.check(
|
|
131
|
-
/* autoApply */
|
|
132
|
-
true,
|
|
133
|
-
handleApplyUpdates
|
|
134
|
-
);
|
|
135
|
-
if (result === null || result === void 0 ? void 0 : result.then) {
|
|
136
|
-
result.then((updatedModules) => {
|
|
137
|
-
handleApplyUpdates(null, updatedModules);
|
|
138
|
-
}, (err) => {
|
|
139
|
-
handleApplyUpdates(err, null);
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
export default require_hmr_client();
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "events";
|
|
2
|
-
import SocketServer from "./socketServer";
|
|
3
|
-
const noop = () => {
|
|
4
|
-
};
|
|
5
|
-
function getHMRClientPath(client) {
|
|
6
|
-
const protocol = (client === null || client === void 0 ? void 0 : client.protocol) ? `&protocol=${client.protocol}` : "";
|
|
7
|
-
const host = (client === null || client === void 0 ? void 0 : client.host) ? `&host=${client.host}` : "";
|
|
8
|
-
const path = (client === null || client === void 0 ? void 0 : client.path) ? `&path=${client.path}` : "";
|
|
9
|
-
const port = (client === null || client === void 0 ? void 0 : client.port) ? `&port=${client.port}` : "";
|
|
10
|
-
const clientEntry = `${require.resolve("@modern-js/server/hmr-client")}?${host}${path}${port}${protocol}`;
|
|
11
|
-
return clientEntry;
|
|
12
|
-
}
|
|
13
|
-
class DevMiddleware extends EventEmitter {
|
|
14
|
-
init(app) {
|
|
15
|
-
app.on("listening", () => {
|
|
16
|
-
this.socketServer.prepare(app);
|
|
17
|
-
});
|
|
18
|
-
app.on("close", async () => {
|
|
19
|
-
var _this_middleware;
|
|
20
|
-
(_this_middleware = this.middleware) === null || _this_middleware === void 0 ? void 0 : _this_middleware.close(noop);
|
|
21
|
-
this.socketServer.close();
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
sockWrite(type, data) {
|
|
25
|
-
this.socketServer.sockWrite(type, data);
|
|
26
|
-
}
|
|
27
|
-
setupDevMiddleware(devMiddleware) {
|
|
28
|
-
const { devOptions } = this;
|
|
29
|
-
const callbacks = {
|
|
30
|
-
onInvalid: () => {
|
|
31
|
-
this.socketServer.sockWrite("invalid");
|
|
32
|
-
},
|
|
33
|
-
onDone: (stats) => {
|
|
34
|
-
this.socketServer.updateStats(stats);
|
|
35
|
-
this.emit("change", stats);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
const enableHMR = this.devOptions.hot || this.devOptions.liveReload;
|
|
39
|
-
const middleware = devMiddleware({
|
|
40
|
-
headers: devOptions.headers,
|
|
41
|
-
stats: false,
|
|
42
|
-
callbacks,
|
|
43
|
-
hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0,
|
|
44
|
-
serverSideRender: true,
|
|
45
|
-
...devOptions.devMiddleware
|
|
46
|
-
});
|
|
47
|
-
return middleware;
|
|
48
|
-
}
|
|
49
|
-
constructor({ dev, devMiddleware }) {
|
|
50
|
-
super();
|
|
51
|
-
this.devOptions = dev;
|
|
52
|
-
this.socketServer = new SocketServer(dev);
|
|
53
|
-
if (devMiddleware) {
|
|
54
|
-
this.middleware = this.setupDevMiddleware(devMiddleware);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
export {
|
|
59
|
-
DevMiddleware as default
|
|
60
|
-
};
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import ws from "ws";
|
|
2
|
-
import { logger } from "@modern-js/utils";
|
|
3
|
-
class SocketServer {
|
|
4
|
-
// create socket, install socket handler, bind socket event
|
|
5
|
-
prepare(app) {
|
|
6
|
-
var _this_options_client;
|
|
7
|
-
this.app = app;
|
|
8
|
-
this.wsServer = new ws.Server({
|
|
9
|
-
noServer: true,
|
|
10
|
-
path: (_this_options_client = this.options.client) === null || _this_options_client === void 0 ? void 0 : _this_options_client.path
|
|
11
|
-
});
|
|
12
|
-
this.app.on("upgrade", (req, sock, head) => {
|
|
13
|
-
if (!this.wsServer.shouldHandle(req)) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
this.wsServer.handleUpgrade(req, sock, head, (connection) => {
|
|
17
|
-
this.wsServer.emit("connection", connection, req);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
this.wsServer.on("error", (err) => {
|
|
21
|
-
logger.error(err);
|
|
22
|
-
});
|
|
23
|
-
this.timer = setInterval(() => {
|
|
24
|
-
this.wsServer.clients.forEach((socket) => {
|
|
25
|
-
const extWs = socket;
|
|
26
|
-
if (!extWs.isAlive) {
|
|
27
|
-
extWs.terminate();
|
|
28
|
-
} else {
|
|
29
|
-
extWs.isAlive = false;
|
|
30
|
-
extWs.ping(() => {
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}, 3e4);
|
|
35
|
-
this.wsServer.on("connection", (socket) => {
|
|
36
|
-
this.onConnect(socket);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
updateStats(stats) {
|
|
40
|
-
this.stats = stats;
|
|
41
|
-
this.sendStats();
|
|
42
|
-
}
|
|
43
|
-
// write message to each socket
|
|
44
|
-
sockWrite(type, data) {
|
|
45
|
-
this.sockets.forEach((socket) => {
|
|
46
|
-
this.send(socket, JSON.stringify({
|
|
47
|
-
type,
|
|
48
|
-
data
|
|
49
|
-
}));
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
singleWrite(socket, type, data) {
|
|
53
|
-
this.send(socket, JSON.stringify({
|
|
54
|
-
type,
|
|
55
|
-
data
|
|
56
|
-
}));
|
|
57
|
-
}
|
|
58
|
-
close() {
|
|
59
|
-
this.sockets.forEach((socket) => {
|
|
60
|
-
socket.close();
|
|
61
|
-
});
|
|
62
|
-
if (this.timer) {
|
|
63
|
-
clearInterval(this.timer);
|
|
64
|
-
this.timer = null;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
onConnect(socket) {
|
|
68
|
-
const connection = socket;
|
|
69
|
-
connection.isAlive = true;
|
|
70
|
-
connection.on("pong", () => {
|
|
71
|
-
connection.isAlive = true;
|
|
72
|
-
});
|
|
73
|
-
if (!connection) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
this.sockets.push(connection);
|
|
77
|
-
connection.on("close", () => {
|
|
78
|
-
const idx = this.sockets.indexOf(connection);
|
|
79
|
-
if (idx >= 0) {
|
|
80
|
-
this.sockets.splice(idx, 1);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
if (this.options.hot) {
|
|
84
|
-
this.singleWrite(connection, "hot");
|
|
85
|
-
}
|
|
86
|
-
if (this.options.liveReload) {
|
|
87
|
-
this.singleWrite(connection, "liveReload");
|
|
88
|
-
}
|
|
89
|
-
if (this.stats) {
|
|
90
|
-
this.sendStats(true);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
// get standard stats
|
|
94
|
-
getStats() {
|
|
95
|
-
const curStats = this.stats;
|
|
96
|
-
if (!curStats) {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
const defaultStats = {
|
|
100
|
-
all: false,
|
|
101
|
-
hash: true,
|
|
102
|
-
assets: true,
|
|
103
|
-
warnings: true,
|
|
104
|
-
errors: true,
|
|
105
|
-
errorDetails: false
|
|
106
|
-
};
|
|
107
|
-
return curStats.toJson(defaultStats);
|
|
108
|
-
}
|
|
109
|
-
// determine what message should send by stats
|
|
110
|
-
sendStats(force = false) {
|
|
111
|
-
const stats = this.getStats();
|
|
112
|
-
if (!stats) {
|
|
113
|
-
return null;
|
|
114
|
-
}
|
|
115
|
-
const shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every((asset) => !asset.emitted);
|
|
116
|
-
if (shouldEmit) {
|
|
117
|
-
return this.sockWrite("still-ok");
|
|
118
|
-
}
|
|
119
|
-
this.sockWrite("hash", stats.hash);
|
|
120
|
-
if (stats.errors && stats.errors.length > 0) {
|
|
121
|
-
return this.sockWrite("errors", stats.errors);
|
|
122
|
-
} else if (stats.warnings && stats.warnings.length > 0) {
|
|
123
|
-
return this.sockWrite("warnings", stats.warnings);
|
|
124
|
-
} else {
|
|
125
|
-
return this.sockWrite("ok");
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
// send message to connecting socket
|
|
129
|
-
send(connection, message) {
|
|
130
|
-
if (connection.readyState !== 1) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
connection.send(message);
|
|
134
|
-
}
|
|
135
|
-
constructor(options) {
|
|
136
|
-
this.sockets = [];
|
|
137
|
-
this.timer = null;
|
|
138
|
-
this.options = options;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
export {
|
|
142
|
-
SocketServer as default
|
|
143
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { chalk, getPackageManager, logger } from "@modern-js/utils";
|
|
2
|
-
const genHttpsOptions = async (userOptions, pwd) => {
|
|
3
|
-
const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
|
|
4
|
-
if (!httpsOptions.key || !httpsOptions.cert) {
|
|
5
|
-
let devcertPath;
|
|
6
|
-
try {
|
|
7
|
-
devcertPath = require.resolve("devcert", {
|
|
8
|
-
paths: [
|
|
9
|
-
pwd,
|
|
10
|
-
__dirname
|
|
11
|
-
]
|
|
12
|
-
});
|
|
13
|
-
} catch (err) {
|
|
14
|
-
const packageManager = await getPackageManager(pwd);
|
|
15
|
-
const command = chalk.yellow.bold(`${packageManager} add devcert@1.2.2 -D`);
|
|
16
|
-
logger.error(`You have enabled "dev.https" option, but the "devcert" package is not installed.`);
|
|
17
|
-
logger.error(`Please run ${command} to install manually, otherwise the https can not work.`);
|
|
18
|
-
throw new Error('[https] "devcert" is not found.');
|
|
19
|
-
}
|
|
20
|
-
const devcert = require(devcertPath);
|
|
21
|
-
const selfsign = await devcert.certificateFor([
|
|
22
|
-
"localhost"
|
|
23
|
-
]);
|
|
24
|
-
return selfsign;
|
|
25
|
-
}
|
|
26
|
-
return httpsOptions;
|
|
27
|
-
};
|
|
28
|
-
export {
|
|
29
|
-
genHttpsOptions
|
|
30
|
-
};
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { match } from "path-to-regexp";
|
|
2
|
-
const VALID_METHODS = [
|
|
3
|
-
"get",
|
|
4
|
-
"post",
|
|
5
|
-
"put",
|
|
6
|
-
"delete",
|
|
7
|
-
"patch"
|
|
8
|
-
];
|
|
9
|
-
const BODY_PARSED_METHODS = [
|
|
10
|
-
"post",
|
|
11
|
-
"put",
|
|
12
|
-
"delete",
|
|
13
|
-
"patch"
|
|
14
|
-
];
|
|
15
|
-
const createFunctionDataHandler = (method, handler) => async (context, next) => {
|
|
16
|
-
const { req, res } = context;
|
|
17
|
-
return handler(req, res, next);
|
|
18
|
-
};
|
|
19
|
-
const createStaticDataHandler = (method, handler) => (context) => {
|
|
20
|
-
const { res } = context;
|
|
21
|
-
res.setHeader("Content-Type", "application/json");
|
|
22
|
-
res.end(JSON.stringify(handler));
|
|
23
|
-
};
|
|
24
|
-
const allowTypes = [
|
|
25
|
-
"object",
|
|
26
|
-
"function"
|
|
27
|
-
];
|
|
28
|
-
var getMockData_default = (mockConfig) => Object.keys(mockConfig).reduce((acc, key) => {
|
|
29
|
-
const handler = mockConfig[key];
|
|
30
|
-
const type = typeof handler;
|
|
31
|
-
if (!allowTypes.includes(type)) {
|
|
32
|
-
throw new Error(`mock value of ${key} should be object or function, but got ${type}`);
|
|
33
|
-
}
|
|
34
|
-
const meta = parseKey(key);
|
|
35
|
-
if (type === "object") {
|
|
36
|
-
acc.push({
|
|
37
|
-
...meta,
|
|
38
|
-
handler: createStaticDataHandler(meta.method, handler)
|
|
39
|
-
});
|
|
40
|
-
} else {
|
|
41
|
-
acc.push({
|
|
42
|
-
...meta,
|
|
43
|
-
handler: createFunctionDataHandler(meta.method, handler)
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
return acc;
|
|
47
|
-
}, []);
|
|
48
|
-
const _blank = " ";
|
|
49
|
-
const parseKey = (key) => {
|
|
50
|
-
const splitted = key.split(_blank).filter(Boolean);
|
|
51
|
-
if (splitted.length > 1) {
|
|
52
|
-
const [method, pathname] = splitted;
|
|
53
|
-
return {
|
|
54
|
-
method: method.toLowerCase(),
|
|
55
|
-
path: pathname
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
method: "get",
|
|
60
|
-
path: key
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
const getMatched = (context, mockApiList) => {
|
|
64
|
-
const { path: targetPathname, method: targetMethod } = context;
|
|
65
|
-
const matched = mockApiList.find((mockApi) => {
|
|
66
|
-
const { method, path: pathname } = mockApi;
|
|
67
|
-
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
68
|
-
return match(pathname, {
|
|
69
|
-
encode: encodeURI,
|
|
70
|
-
decode: decodeURIComponent
|
|
71
|
-
})(targetPathname);
|
|
72
|
-
}
|
|
73
|
-
return false;
|
|
74
|
-
});
|
|
75
|
-
return matched;
|
|
76
|
-
};
|
|
77
|
-
export {
|
|
78
|
-
getMockData_default as default,
|
|
79
|
-
getMatched
|
|
80
|
-
};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { compatRequire, fs } from "@modern-js/utils";
|
|
3
|
-
import { AGGRED_DIR } from "@modern-js/prod-server";
|
|
4
|
-
import getMockData, { getMatched } from "./getMockData";
|
|
5
|
-
const createMockHandler = ({ pwd }) => {
|
|
6
|
-
const exts = [
|
|
7
|
-
".ts",
|
|
8
|
-
".js"
|
|
9
|
-
];
|
|
10
|
-
let filepath = "";
|
|
11
|
-
for (const ext of exts) {
|
|
12
|
-
const maybeMatch = path.join(pwd, `${AGGRED_DIR.mock}/index${ext}`);
|
|
13
|
-
if (fs.existsSync(maybeMatch)) {
|
|
14
|
-
filepath = maybeMatch;
|
|
15
|
-
break;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
if (!filepath) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
const mod = compatRequire(filepath, false);
|
|
22
|
-
const { default: mockModule = mod, config } = mod;
|
|
23
|
-
if ((config === null || config === void 0 ? void 0 : config.enable) === false) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
if (!mockModule) {
|
|
27
|
-
throw new Error(`Mock file ${filepath} parsed failed!`);
|
|
28
|
-
}
|
|
29
|
-
const apiList = getMockData(mockModule);
|
|
30
|
-
if (!apiList || apiList.length === 0) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
return async (context, next) => {
|
|
34
|
-
if (typeof (config === null || config === void 0 ? void 0 : config.enable) === "function") {
|
|
35
|
-
const enableMock = config.enable(context.req, context.res);
|
|
36
|
-
if (!enableMock) {
|
|
37
|
-
return next();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const { res } = context;
|
|
41
|
-
const matched = getMatched(context, apiList);
|
|
42
|
-
if (!matched) {
|
|
43
|
-
return next();
|
|
44
|
-
}
|
|
45
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
46
|
-
return matched.handler(context, next);
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
export {
|
|
50
|
-
createMockHandler
|
|
51
|
-
};
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { resolveBabelConfig } from "@modern-js/server-utils";
|
|
3
|
-
import { fs, getAliasConfig, createDebugger, readTsConfigByFile } from "@modern-js/utils";
|
|
4
|
-
const debug = createDebugger("server");
|
|
5
|
-
const checkDep = (depName, paths) => {
|
|
6
|
-
let packagePath = "";
|
|
7
|
-
try {
|
|
8
|
-
packagePath = require.resolve(depName, {
|
|
9
|
-
paths
|
|
10
|
-
});
|
|
11
|
-
} catch (error) {
|
|
12
|
-
}
|
|
13
|
-
return Boolean(packagePath);
|
|
14
|
-
};
|
|
15
|
-
const enableRegister = (projectRoot, config) => {
|
|
16
|
-
const registerDirs = [
|
|
17
|
-
"./api",
|
|
18
|
-
"./server",
|
|
19
|
-
"./config/mock",
|
|
20
|
-
"./shared"
|
|
21
|
-
];
|
|
22
|
-
const TS_CONFIG_FILENAME = `tsconfig.json`;
|
|
23
|
-
const tsconfigPath = path.resolve(projectRoot, TS_CONFIG_FILENAME);
|
|
24
|
-
const isTsProject = fs.existsSync(tsconfigPath);
|
|
25
|
-
const existTsNode = checkDep("ts-node", [
|
|
26
|
-
projectRoot
|
|
27
|
-
]);
|
|
28
|
-
const existTsConfigPaths = checkDep("tsconfig-paths", [
|
|
29
|
-
projectRoot
|
|
30
|
-
]);
|
|
31
|
-
if (isTsProject && existTsNode && existTsConfigPaths) {
|
|
32
|
-
var _config_output_distPath;
|
|
33
|
-
debug("use ts-node");
|
|
34
|
-
const distPath = ((_config_output_distPath = config.output.distPath) === null || _config_output_distPath === void 0 ? void 0 : _config_output_distPath.root) || "dist";
|
|
35
|
-
const tsNode = require("ts-node");
|
|
36
|
-
const tsConfigPaths = require("tsconfig-paths");
|
|
37
|
-
const { alias } = config.source;
|
|
38
|
-
const aliasConfig = getAliasConfig(alias, {
|
|
39
|
-
appDirectory: projectRoot,
|
|
40
|
-
tsconfigPath
|
|
41
|
-
});
|
|
42
|
-
const { paths = {}, absoluteBaseUrl = "./" } = aliasConfig;
|
|
43
|
-
const tsPaths = Object.keys(paths).reduce((o, key) => {
|
|
44
|
-
let tsPath = paths[key];
|
|
45
|
-
if (typeof tsPath === "string" && path.isAbsolute(tsPath)) {
|
|
46
|
-
tsPath = path.relative(absoluteBaseUrl, tsPath);
|
|
47
|
-
}
|
|
48
|
-
if (typeof tsPath === "string") {
|
|
49
|
-
tsPath = [
|
|
50
|
-
tsPath
|
|
51
|
-
];
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
...o,
|
|
55
|
-
[`${key}`]: tsPath
|
|
56
|
-
};
|
|
57
|
-
}, {});
|
|
58
|
-
tsConfigPaths.register({
|
|
59
|
-
baseUrl: absoluteBaseUrl || "./",
|
|
60
|
-
paths: tsPaths
|
|
61
|
-
});
|
|
62
|
-
const tsConfig = readTsConfigByFile(tsconfigPath);
|
|
63
|
-
const tsNodeOptions = tsConfig["ts-node"];
|
|
64
|
-
tsNode.register({
|
|
65
|
-
project: tsconfigPath,
|
|
66
|
-
scope: true,
|
|
67
|
-
// for env.d.ts, https://www.npmjs.com/package/ts-node#missing-types
|
|
68
|
-
files: true,
|
|
69
|
-
transpileOnly: true,
|
|
70
|
-
ignore: [
|
|
71
|
-
"(?:^|/)node_modules/",
|
|
72
|
-
`(?:^|/)${distPath}/`
|
|
73
|
-
],
|
|
74
|
-
...tsNodeOptions
|
|
75
|
-
});
|
|
76
|
-
} else {
|
|
77
|
-
var _config_tools, _config_server;
|
|
78
|
-
debug("use @babel/register");
|
|
79
|
-
const babelConfig = resolveBabelConfig(projectRoot, {
|
|
80
|
-
...config.source,
|
|
81
|
-
babelConfig: (_config_tools = config.tools) === null || _config_tools === void 0 ? void 0 : _config_tools.babel,
|
|
82
|
-
server: {
|
|
83
|
-
compiler: (_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.compiler
|
|
84
|
-
}
|
|
85
|
-
}, {
|
|
86
|
-
tsconfigPath
|
|
87
|
-
});
|
|
88
|
-
return require("@babel/register")({
|
|
89
|
-
...babelConfig,
|
|
90
|
-
only: [
|
|
91
|
-
function(filePath) {
|
|
92
|
-
if (filePath.includes(`node_modules${path.sep}.modern-js`)) {
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
return registerDirs.some((registerDir) => filePath.startsWith(path.join(projectRoot, registerDir)));
|
|
96
|
-
}
|
|
97
|
-
],
|
|
98
|
-
extensions: [
|
|
99
|
-
".js",
|
|
100
|
-
".ts"
|
|
101
|
-
],
|
|
102
|
-
babelrc: false,
|
|
103
|
-
configFile: false,
|
|
104
|
-
root: projectRoot
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
export {
|
|
109
|
-
enableRegister
|
|
110
|
-
};
|