@modern-js/server 2.15.1-alpha.0 → 2.17.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 +25 -0
- package/dist/cjs/constants.js +12 -28
- package/dist/cjs/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +14 -32
- package/dist/cjs/dev-tools/dev-middleware/hmr-client/index.js +24 -42
- package/dist/cjs/dev-tools/dev-middleware/index.js +45 -50
- package/dist/cjs/dev-tools/dev-middleware/socketServer.js +48 -46
- package/dist/cjs/dev-tools/https/index.js +21 -38
- package/dist/cjs/dev-tools/mock/getMockData.js +33 -33
- package/dist/cjs/dev-tools/mock/index.js +67 -47
- package/dist/cjs/dev-tools/register/index.js +59 -70
- package/dist/cjs/dev-tools/watcher/dependencyTree.js +52 -55
- package/dist/cjs/dev-tools/watcher/index.js +52 -53
- package/dist/cjs/dev-tools/watcher/statsCache.js +37 -47
- package/dist/cjs/index.js +16 -28
- package/dist/cjs/server/devServer.js +163 -109
- package/dist/cjs/server/index.js +11 -29
- package/dist/cjs/types.js +4 -15
- package/dist/esm/constants.js +19 -20
- package/dist/esm/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +43 -44
- package/dist/esm/dev-tools/dev-middleware/hmr-client/index.js +148 -157
- package/dist/esm/dev-tools/dev-middleware/index.js +307 -291
- package/dist/esm/dev-tools/dev-middleware/socketServer.js +216 -202
- package/dist/esm/dev-tools/https/index.js +189 -182
- package/dist/esm/dev-tools/mock/getMockData.js +297 -278
- package/dist/esm/dev-tools/mock/index.js +198 -192
- package/dist/esm/dev-tools/register/index.js +141 -141
- package/dist/esm/dev-tools/watcher/dependencyTree.js +153 -138
- package/dist/esm/dev-tools/watcher/index.js +187 -175
- package/dist/esm/dev-tools/watcher/statsCache.js +118 -116
- package/dist/esm/index.js +9 -8
- package/dist/esm/server/devServer.js +803 -766
- package/dist/esm/server/index.js +80 -76
- package/dist/esm/types.js +1 -1
- package/dist/esm-node/constants.js +4 -5
- package/dist/esm-node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +2 -11
- package/dist/esm-node/dev-tools/dev-middleware/hmr-client/index.js +125 -139
- package/dist/esm-node/dev-tools/dev-middleware/index.js +31 -17
- package/dist/esm-node/dev-tools/dev-middleware/socketServer.js +33 -12
- package/dist/esm-node/dev-tools/https/index.js +13 -15
- package/dist/esm-node/dev-tools/mock/getMockData.js +20 -12
- package/dist/esm-node/dev-tools/mock/index.js +7 -7
- package/dist/esm-node/dev-tools/register/index.js +38 -29
- package/dist/esm-node/dev-tools/watcher/dependencyTree.js +32 -21
- package/dist/esm-node/dev-tools/watcher/index.js +31 -18
- package/dist/esm-node/dev-tools/watcher/statsCache.js +18 -8
- package/dist/esm-node/index.js +2 -5
- package/dist/esm-node/server/devServer.js +87 -62
- package/dist/esm-node/server/index.js +1 -4
- package/dist/esm-node/types.js +1 -0
- package/package.json +13 -9
- package/dist/js/modern/constants.js +0 -19
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -41
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +0 -143
- package/dist/js/modern/dev-tools/dev-middleware/index.js +0 -94
- package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +0 -132
- package/dist/js/modern/dev-tools/https/index.js +0 -49
- package/dist/js/modern/dev-tools/mock/getMockData.js +0 -114
- package/dist/js/modern/dev-tools/mock/index.js +0 -54
- package/dist/js/modern/dev-tools/register/index.js +0 -111
- package/dist/js/modern/dev-tools/watcher/dependency-tree.js +0 -59
- package/dist/js/modern/dev-tools/watcher/index.js +0 -106
- package/dist/js/modern/dev-tools/watcher/stats-cache.js +0 -58
- package/dist/js/modern/index.js +0 -12
- package/dist/js/modern/server/dev-server.js +0 -293
- package/dist/js/modern/server/index.js +0 -16
- package/dist/js/modern/types.js +0 -0
- package/dist/js/node/constants.js +0 -42
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -65
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +0 -152
- package/dist/js/node/dev-tools/dev-middleware/index.js +0 -119
- package/dist/js/node/dev-tools/dev-middleware/socket-server.js +0 -159
- package/dist/js/node/dev-tools/https/index.js +0 -72
- package/dist/js/node/dev-tools/mock/getMockData.js +0 -135
- package/dist/js/node/dev-tools/mock/index.js +0 -83
- package/dist/js/node/dev-tools/register/index.js +0 -138
- package/dist/js/node/dev-tools/watcher/dependency-tree.js +0 -89
- package/dist/js/node/dev-tools/watcher/index.js +0 -135
- package/dist/js/node/dev-tools/watcher/stats-cache.js +0 -87
- package/dist/js/node/index.js +0 -35
- package/dist/js/node/server/dev-server.js +0 -310
- package/dist/js/node/server/index.js +0 -39
- package/dist/js/node/types.js +0 -15
- package/dist/js/treeshaking/constants.js +0 -21
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -51
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js +0 -157
- package/dist/js/treeshaking/dev-tools/dev-middleware/index.js +0 -322
- package/dist/js/treeshaking/dev-tools/dev-middleware/socket-server.js +0 -209
- package/dist/js/treeshaking/dev-tools/https/index.js +0 -193
- package/dist/js/treeshaking/dev-tools/mock/getMockData.js +0 -327
- package/dist/js/treeshaking/dev-tools/mock/index.js +0 -191
- package/dist/js/treeshaking/dev-tools/register/index.js +0 -153
- package/dist/js/treeshaking/dev-tools/watcher/dependency-tree.js +0 -150
- package/dist/js/treeshaking/dev-tools/watcher/index.js +0 -200
- package/dist/js/treeshaking/dev-tools/watcher/stats-cache.js +0 -128
- package/dist/js/treeshaking/index.js +0 -9
- package/dist/js/treeshaking/server/dev-server.js +0 -799
- package/dist/js/treeshaking/server/index.js +0 -92
- package/dist/js/treeshaking/types.js +0 -1
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import ws from "ws";
|
|
2
|
-
import { logger } from "@modern-js/utils";
|
|
3
|
-
class SocketServer {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
this.sockets = [];
|
|
6
|
-
this.timer = null;
|
|
7
|
-
this.options = options;
|
|
8
|
-
}
|
|
9
|
-
prepare(app) {
|
|
10
|
-
var _a;
|
|
11
|
-
this.app = app;
|
|
12
|
-
this.wsServer = new ws.Server({
|
|
13
|
-
noServer: true,
|
|
14
|
-
path: (_a = this.options.client) == null ? void 0 : _a.path
|
|
15
|
-
});
|
|
16
|
-
this.app.on("upgrade", (req, sock, head) => {
|
|
17
|
-
if (!this.wsServer.shouldHandle(req)) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
this.wsServer.handleUpgrade(req, sock, head, (connection) => {
|
|
21
|
-
this.wsServer.emit("connection", connection, req);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
this.wsServer.on("error", (err) => {
|
|
25
|
-
logger.error(err);
|
|
26
|
-
});
|
|
27
|
-
this.timer = setInterval(() => {
|
|
28
|
-
this.wsServer.clients.forEach((socket) => {
|
|
29
|
-
const extWs = socket;
|
|
30
|
-
if (!extWs.isAlive) {
|
|
31
|
-
extWs.terminate();
|
|
32
|
-
} else {
|
|
33
|
-
extWs.isAlive = false;
|
|
34
|
-
extWs.ping(() => {
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}, 3e4);
|
|
39
|
-
this.wsServer.on("connection", (socket) => {
|
|
40
|
-
this.onConnect(socket);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
updateStats(stats) {
|
|
44
|
-
this.stats = stats;
|
|
45
|
-
this.sendStats();
|
|
46
|
-
}
|
|
47
|
-
sockWrite(type, data) {
|
|
48
|
-
this.sockets.forEach((socket) => {
|
|
49
|
-
this.send(socket, JSON.stringify({ type, data }));
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
singleWrite(socket, type, data) {
|
|
53
|
-
this.send(socket, JSON.stringify({ type, data }));
|
|
54
|
-
}
|
|
55
|
-
close() {
|
|
56
|
-
this.sockets.forEach((socket) => {
|
|
57
|
-
socket.close();
|
|
58
|
-
});
|
|
59
|
-
if (this.timer) {
|
|
60
|
-
clearInterval(this.timer);
|
|
61
|
-
this.timer = null;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
onConnect(socket) {
|
|
65
|
-
const connection = socket;
|
|
66
|
-
connection.isAlive = true;
|
|
67
|
-
connection.on("pong", () => {
|
|
68
|
-
connection.isAlive = true;
|
|
69
|
-
});
|
|
70
|
-
if (!connection) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
this.sockets.push(connection);
|
|
74
|
-
connection.on("close", () => {
|
|
75
|
-
const idx = this.sockets.indexOf(connection);
|
|
76
|
-
if (idx >= 0) {
|
|
77
|
-
this.sockets.splice(idx, 1);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
if (this.options.hot || this.options.hot === "only") {
|
|
81
|
-
this.singleWrite(connection, "hot");
|
|
82
|
-
}
|
|
83
|
-
if (this.options.liveReload) {
|
|
84
|
-
this.singleWrite(connection, "liveReload");
|
|
85
|
-
}
|
|
86
|
-
if (this.stats) {
|
|
87
|
-
this.sendStats(true);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
getStats() {
|
|
91
|
-
const curStats = this.stats;
|
|
92
|
-
if (!curStats) {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
const defaultStats = {
|
|
96
|
-
all: false,
|
|
97
|
-
hash: true,
|
|
98
|
-
assets: true,
|
|
99
|
-
warnings: true,
|
|
100
|
-
errors: true,
|
|
101
|
-
errorDetails: false
|
|
102
|
-
};
|
|
103
|
-
return curStats.toJson(defaultStats);
|
|
104
|
-
}
|
|
105
|
-
sendStats(force = false) {
|
|
106
|
-
const stats = this.getStats();
|
|
107
|
-
if (!stats) {
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
const shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every((asset) => !asset.emitted);
|
|
111
|
-
if (shouldEmit) {
|
|
112
|
-
return this.sockWrite("still-ok");
|
|
113
|
-
}
|
|
114
|
-
this.sockWrite("hash", stats.hash);
|
|
115
|
-
if (stats.errors && stats.errors.length > 0) {
|
|
116
|
-
return this.sockWrite("errors", stats.errors);
|
|
117
|
-
} else if (stats.warnings && stats.warnings.length > 0) {
|
|
118
|
-
return this.sockWrite("warnings", stats.warnings);
|
|
119
|
-
} else {
|
|
120
|
-
return this.sockWrite("ok");
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
send(connection, message) {
|
|
124
|
-
if (connection.readyState !== 1) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
connection.send(message);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
export {
|
|
131
|
-
SocketServer as default
|
|
132
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import { chalk, getPackageManager, logger } from "@modern-js/utils";
|
|
22
|
-
const genHttpsOptions = (userOptions, pwd) => __async(void 0, null, function* () {
|
|
23
|
-
const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
|
|
24
|
-
if (!httpsOptions.key || !httpsOptions.cert) {
|
|
25
|
-
let devcertPath;
|
|
26
|
-
try {
|
|
27
|
-
devcertPath = require.resolve("devcert", { paths: [pwd, __dirname] });
|
|
28
|
-
} catch (err) {
|
|
29
|
-
const packageManager = yield getPackageManager(pwd);
|
|
30
|
-
const command = chalk.yellow.bold(
|
|
31
|
-
`${packageManager} install devcert@1.2.2 -D`
|
|
32
|
-
);
|
|
33
|
-
logger.error(
|
|
34
|
-
`You have enabled "dev.https" option, but the "devcert" package is not installed.`
|
|
35
|
-
);
|
|
36
|
-
logger.error(
|
|
37
|
-
`Please run ${command} to install manually, otherwise the https can not work.`
|
|
38
|
-
);
|
|
39
|
-
throw new Error('[https] "devcert" is not found.');
|
|
40
|
-
}
|
|
41
|
-
const devcert = require(devcertPath);
|
|
42
|
-
const selfsign = yield devcert.certificateFor(["localhost"]);
|
|
43
|
-
return selfsign;
|
|
44
|
-
}
|
|
45
|
-
return httpsOptions;
|
|
46
|
-
});
|
|
47
|
-
export {
|
|
48
|
-
genHttpsOptions
|
|
49
|
-
};
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __async = (__this, __arguments, generator) => {
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
var fulfilled = (value) => {
|
|
23
|
-
try {
|
|
24
|
-
step(generator.next(value));
|
|
25
|
-
} catch (e) {
|
|
26
|
-
reject(e);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var rejected = (value) => {
|
|
30
|
-
try {
|
|
31
|
-
step(generator.throw(value));
|
|
32
|
-
} catch (e) {
|
|
33
|
-
reject(e);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
import { compatRequire } from "@modern-js/utils";
|
|
41
|
-
import { match } from "path-to-regexp";
|
|
42
|
-
const VALID_METHODS = ["get", "post", "put", "delete", "patch"];
|
|
43
|
-
const BODY_PARSED_METHODS = ["post", "put", "delete", "patch"];
|
|
44
|
-
const createFunctionDataHandler = (method, handler) => (context, next) => __async(void 0, null, function* () {
|
|
45
|
-
const { req, res } = context;
|
|
46
|
-
return handler(req, res, next);
|
|
47
|
-
});
|
|
48
|
-
const createStaticDataHandler = (method, handler) => (context) => {
|
|
49
|
-
const { res } = context;
|
|
50
|
-
res.setHeader("Content-Type", "application/json");
|
|
51
|
-
res.end(JSON.stringify(handler));
|
|
52
|
-
};
|
|
53
|
-
const allowTypes = ["object", "function"];
|
|
54
|
-
const normalizeConfig = (mockConfig) => Object.keys(mockConfig).reduce((acc, key) => {
|
|
55
|
-
const handler = mockConfig[key];
|
|
56
|
-
const type = typeof handler;
|
|
57
|
-
if (!allowTypes.includes(type)) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`mock value of ${key} should be object or function, but got ${type}`
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
const meta = parseKey(key);
|
|
63
|
-
if (type === "object") {
|
|
64
|
-
acc.push(__spreadProps(__spreadValues({}, meta), {
|
|
65
|
-
handler: createStaticDataHandler(meta.method, handler)
|
|
66
|
-
}));
|
|
67
|
-
} else {
|
|
68
|
-
acc.push(__spreadProps(__spreadValues({}, meta), {
|
|
69
|
-
handler: createFunctionDataHandler(meta.method, handler)
|
|
70
|
-
}));
|
|
71
|
-
}
|
|
72
|
-
return acc;
|
|
73
|
-
}, []);
|
|
74
|
-
const _blank = " ";
|
|
75
|
-
const parseKey = (key) => {
|
|
76
|
-
const splitted = key.split(_blank).filter(Boolean);
|
|
77
|
-
if (splitted.length > 1) {
|
|
78
|
-
const [method, pathname] = splitted;
|
|
79
|
-
return {
|
|
80
|
-
method: method.toLowerCase(),
|
|
81
|
-
path: pathname
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
return {
|
|
85
|
-
method: "get",
|
|
86
|
-
path: key
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
var getMockData_default = (filepath) => {
|
|
90
|
-
const mockModule = compatRequire(filepath);
|
|
91
|
-
if (!mockModule) {
|
|
92
|
-
throw new Error(`Mock file ${filepath} parsed failed!`);
|
|
93
|
-
}
|
|
94
|
-
const data = normalizeConfig(mockModule);
|
|
95
|
-
return data;
|
|
96
|
-
};
|
|
97
|
-
const getMatched = (context, mockApiList) => {
|
|
98
|
-
const { path: targetPathname, method: targetMethod } = context;
|
|
99
|
-
const matched = mockApiList.find((mockApi) => {
|
|
100
|
-
const { method, path: pathname } = mockApi;
|
|
101
|
-
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
102
|
-
return match(pathname, {
|
|
103
|
-
encode: encodeURI,
|
|
104
|
-
decode: decodeURIComponent
|
|
105
|
-
})(targetPathname);
|
|
106
|
-
}
|
|
107
|
-
return false;
|
|
108
|
-
});
|
|
109
|
-
return matched;
|
|
110
|
-
};
|
|
111
|
-
export {
|
|
112
|
-
getMockData_default as default,
|
|
113
|
-
getMatched
|
|
114
|
-
};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import path from "path";
|
|
22
|
-
import { fs } from "@modern-js/utils";
|
|
23
|
-
import { AGGRED_DIR } from "@modern-js/prod-server";
|
|
24
|
-
import getMockData, { getMatched } from "./getMockData";
|
|
25
|
-
const createMockHandler = ({ pwd }) => {
|
|
26
|
-
const exts = [".ts", ".js"];
|
|
27
|
-
let filepath = "";
|
|
28
|
-
for (const ext of exts) {
|
|
29
|
-
const maybeMatch = path.join(pwd, `${AGGRED_DIR.mock}/index${ext}`);
|
|
30
|
-
if (fs.existsSync(maybeMatch)) {
|
|
31
|
-
filepath = maybeMatch;
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (!filepath) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
const apiList = getMockData(filepath);
|
|
39
|
-
if (!apiList || apiList.length === 0) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
return (context, next) => __async(void 0, null, function* () {
|
|
43
|
-
const { res } = context;
|
|
44
|
-
const matched = getMatched(context, apiList);
|
|
45
|
-
if (!matched) {
|
|
46
|
-
return next();
|
|
47
|
-
}
|
|
48
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
49
|
-
return matched.handler(context, next);
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
export {
|
|
53
|
-
createMockHandler
|
|
54
|
-
};
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
import path from "path";
|
|
21
|
-
import { resolveBabelConfig } from "@modern-js/server-utils";
|
|
22
|
-
import { fs, getAliasConfig, createDebugger } from "@modern-js/utils";
|
|
23
|
-
const debug = createDebugger("server");
|
|
24
|
-
const checkDep = (depName, paths) => {
|
|
25
|
-
let packagePath = "";
|
|
26
|
-
try {
|
|
27
|
-
packagePath = require.resolve(depName, {
|
|
28
|
-
paths
|
|
29
|
-
});
|
|
30
|
-
} catch (error) {
|
|
31
|
-
}
|
|
32
|
-
return Boolean(packagePath);
|
|
33
|
-
};
|
|
34
|
-
const enableRegister = (projectRoot, config) => {
|
|
35
|
-
var _a, _b, _c;
|
|
36
|
-
const registerDirs = ["./api", "./server", "./config/mock", "./shared"];
|
|
37
|
-
const TS_CONFIG_FILENAME = `tsconfig.json`;
|
|
38
|
-
const tsconfigPath = path.resolve(projectRoot, TS_CONFIG_FILENAME);
|
|
39
|
-
const isTsProject = fs.existsSync(tsconfigPath);
|
|
40
|
-
const existTsNode = checkDep("ts-node", [projectRoot]);
|
|
41
|
-
const existTsConfigPaths = checkDep("tsconfig-paths", [projectRoot]);
|
|
42
|
-
if (isTsProject && existTsNode && existTsConfigPaths) {
|
|
43
|
-
debug("use ts-node");
|
|
44
|
-
const distPath = ((_a = config.output.distPath) == null ? void 0 : _a.root) || "dist";
|
|
45
|
-
const tsNode = require("ts-node");
|
|
46
|
-
const tsConfigPaths = require("tsconfig-paths");
|
|
47
|
-
const { alias } = config.source;
|
|
48
|
-
const aliasConfig = getAliasConfig(alias, {
|
|
49
|
-
appDirectory: projectRoot,
|
|
50
|
-
tsconfigPath
|
|
51
|
-
});
|
|
52
|
-
const { paths = {}, absoluteBaseUrl = "./" } = aliasConfig;
|
|
53
|
-
const tsPaths = Object.keys(paths).reduce((o, key) => {
|
|
54
|
-
let tsPath = paths[key];
|
|
55
|
-
if (typeof tsPath === "string" && path.isAbsolute(tsPath)) {
|
|
56
|
-
tsPath = path.relative(absoluteBaseUrl, tsPath);
|
|
57
|
-
}
|
|
58
|
-
if (typeof tsPath === "string") {
|
|
59
|
-
tsPath = [tsPath];
|
|
60
|
-
}
|
|
61
|
-
return __spreadProps(__spreadValues({}, o), {
|
|
62
|
-
[`${key}`]: tsPath
|
|
63
|
-
});
|
|
64
|
-
}, {});
|
|
65
|
-
tsConfigPaths.register({
|
|
66
|
-
baseUrl: absoluteBaseUrl || "./",
|
|
67
|
-
paths: tsPaths
|
|
68
|
-
});
|
|
69
|
-
tsNode.register({
|
|
70
|
-
project: tsconfigPath,
|
|
71
|
-
files: true,
|
|
72
|
-
transpileOnly: true,
|
|
73
|
-
ignore: ["(?:^|/)node_modules/", `(?:^|/)${distPath}/`]
|
|
74
|
-
});
|
|
75
|
-
} else {
|
|
76
|
-
debug("use @babel/register");
|
|
77
|
-
const babelConfig = resolveBabelConfig(
|
|
78
|
-
projectRoot,
|
|
79
|
-
__spreadProps(__spreadValues({}, config.source), {
|
|
80
|
-
babelConfig: (_b = config.tools) == null ? void 0 : _b.babel,
|
|
81
|
-
server: {
|
|
82
|
-
compiler: (_c = config.server) == null ? void 0 : _c.compiler
|
|
83
|
-
}
|
|
84
|
-
}),
|
|
85
|
-
{
|
|
86
|
-
tsconfigPath,
|
|
87
|
-
syntax: "es6+",
|
|
88
|
-
type: "commonjs"
|
|
89
|
-
}
|
|
90
|
-
);
|
|
91
|
-
return require("@babel/register")(__spreadProps(__spreadValues({}, babelConfig), {
|
|
92
|
-
only: [
|
|
93
|
-
function(filePath) {
|
|
94
|
-
if (filePath.includes(`node_modules${path.sep}.modern-js`)) {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
return registerDirs.some(
|
|
98
|
-
(registerDir) => filePath.startsWith(path.join(projectRoot, registerDir))
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
],
|
|
102
|
-
extensions: [".js", ".ts"],
|
|
103
|
-
babelrc: false,
|
|
104
|
-
configFile: false,
|
|
105
|
-
root: projectRoot
|
|
106
|
-
}));
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
export {
|
|
110
|
-
enableRegister
|
|
111
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import minimatch from "minimatch";
|
|
2
|
-
const defaultIgnores = [
|
|
3
|
-
"**/bower_components/**",
|
|
4
|
-
"**/coverage/**",
|
|
5
|
-
"**/node_modules/**",
|
|
6
|
-
"**/.*/**",
|
|
7
|
-
"**/*.d.ts",
|
|
8
|
-
"**/*.log"
|
|
9
|
-
];
|
|
10
|
-
class DependencyTree {
|
|
11
|
-
constructor() {
|
|
12
|
-
this.tree = /* @__PURE__ */ new Map();
|
|
13
|
-
this.ignore = [...defaultIgnores];
|
|
14
|
-
}
|
|
15
|
-
getNode(path) {
|
|
16
|
-
return this.tree.get(path);
|
|
17
|
-
}
|
|
18
|
-
update(cache) {
|
|
19
|
-
this.tree.clear();
|
|
20
|
-
Object.keys(cache).forEach((path) => {
|
|
21
|
-
if (!this.shouldIgnore(path)) {
|
|
22
|
-
const module = cache[path];
|
|
23
|
-
this.tree.set(module.filename, {
|
|
24
|
-
module,
|
|
25
|
-
parent: /* @__PURE__ */ new Set(),
|
|
26
|
-
children: /* @__PURE__ */ new Set()
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
for (const treeNode of this.tree.values()) {
|
|
31
|
-
const { parent } = treeNode.module;
|
|
32
|
-
const { children } = treeNode.module;
|
|
33
|
-
if (parent && !this.shouldIgnore(parent.filename)) {
|
|
34
|
-
const parentTreeNode = this.tree.get(parent.filename);
|
|
35
|
-
if (parentTreeNode) {
|
|
36
|
-
treeNode.parent.add(parentTreeNode);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
children.forEach((child) => {
|
|
40
|
-
if (!this.shouldIgnore(child.filename)) {
|
|
41
|
-
const childTreeNode = this.tree.get(child.filename);
|
|
42
|
-
if (childTreeNode) {
|
|
43
|
-
treeNode.children.add(childTreeNode);
|
|
44
|
-
childTreeNode.parent.add(treeNode);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
shouldIgnore(path) {
|
|
51
|
-
return !path || Boolean(
|
|
52
|
-
this.ignore.find((rule) => minimatch.match([path], rule).length > 0)
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
export {
|
|
57
|
-
DependencyTree,
|
|
58
|
-
defaultIgnores
|
|
59
|
-
};
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __spreadValues = (a, b) => {
|
|
7
|
-
for (var prop in b || (b = {}))
|
|
8
|
-
if (__hasOwnProp.call(b, prop))
|
|
9
|
-
__defNormalProp(a, prop, b[prop]);
|
|
10
|
-
if (__getOwnPropSymbols)
|
|
11
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
-
if (__propIsEnum.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
}
|
|
15
|
-
return a;
|
|
16
|
-
};
|
|
17
|
-
import path from "path";
|
|
18
|
-
import { fs, chokidar } from "@modern-js/utils";
|
|
19
|
-
import { DependencyTree } from "./dependency-tree";
|
|
20
|
-
import { StatsCache } from "./stats-cache";
|
|
21
|
-
const defaultWatchOptions = {
|
|
22
|
-
ignoreInitial: true,
|
|
23
|
-
ignored: /api\/typings\/.*/
|
|
24
|
-
};
|
|
25
|
-
const getWatchedFiles = (watcher) => {
|
|
26
|
-
const watched = watcher.getWatched();
|
|
27
|
-
const files = [];
|
|
28
|
-
Object.keys(watched).forEach((dir) => {
|
|
29
|
-
watched[dir].forEach((fileName) => {
|
|
30
|
-
files.push(path.join(dir, fileName));
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
return files;
|
|
34
|
-
};
|
|
35
|
-
const mergeWatchOptions = (options) => {
|
|
36
|
-
const watchOptions = __spreadValues({}, options);
|
|
37
|
-
if (watchOptions) {
|
|
38
|
-
const { ignored } = watchOptions;
|
|
39
|
-
const finalIgnored = ignored ? [
|
|
40
|
-
defaultWatchOptions.ignored,
|
|
41
|
-
...Array.isArray(ignored) ? ignored : [ignored]
|
|
42
|
-
] : ignored;
|
|
43
|
-
if (finalIgnored) {
|
|
44
|
-
watchOptions.ignored = finalIgnored;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const finalWatchOptions = __spreadValues(__spreadValues({}, defaultWatchOptions), watchOptions);
|
|
48
|
-
return finalWatchOptions;
|
|
49
|
-
};
|
|
50
|
-
class Watcher {
|
|
51
|
-
constructor() {
|
|
52
|
-
this.dependencyTree = null;
|
|
53
|
-
}
|
|
54
|
-
listen(files, options, callback) {
|
|
55
|
-
const watched = files.filter(Boolean);
|
|
56
|
-
const filenames = watched.map((filename) => filename.replace(/\\/g, "/"));
|
|
57
|
-
const cache = new StatsCache();
|
|
58
|
-
const watcher = chokidar.watch(filenames, options);
|
|
59
|
-
watcher.on("ready", () => {
|
|
60
|
-
cache.add(getWatchedFiles(watcher));
|
|
61
|
-
});
|
|
62
|
-
watcher.on("change", (changed) => {
|
|
63
|
-
if (!fs.existsSync(changed) || cache.isDiff(changed)) {
|
|
64
|
-
cache.refresh(changed);
|
|
65
|
-
callback(changed, "change");
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
watcher.on("add", (changed) => {
|
|
69
|
-
if (!cache.has(changed)) {
|
|
70
|
-
cache.add([changed]);
|
|
71
|
-
callback(changed, "add");
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
watcher.on("unlink", (changed) => {
|
|
75
|
-
cache.del(changed);
|
|
76
|
-
callback(changed, "unlink");
|
|
77
|
-
});
|
|
78
|
-
this.watcher = watcher;
|
|
79
|
-
}
|
|
80
|
-
createDepTree() {
|
|
81
|
-
this.dependencyTree = new DependencyTree();
|
|
82
|
-
}
|
|
83
|
-
updateDepTree() {
|
|
84
|
-
var _a;
|
|
85
|
-
(_a = this.dependencyTree) == null ? void 0 : _a.update(require.cache);
|
|
86
|
-
}
|
|
87
|
-
cleanDepCache(filepath) {
|
|
88
|
-
var _a;
|
|
89
|
-
const node = (_a = this.dependencyTree) == null ? void 0 : _a.getNode(filepath);
|
|
90
|
-
if (node && require.cache[filepath]) {
|
|
91
|
-
delete require.cache[filepath];
|
|
92
|
-
for (const parentNode of node.parent.values()) {
|
|
93
|
-
this.cleanDepCache(parentNode.module.filename);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
close() {
|
|
98
|
-
return this.watcher.close();
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
export {
|
|
102
|
-
Watcher as default,
|
|
103
|
-
defaultWatchOptions,
|
|
104
|
-
getWatchedFiles,
|
|
105
|
-
mergeWatchOptions
|
|
106
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import crypto from "crypto";
|
|
3
|
-
class StatsCache {
|
|
4
|
-
constructor() {
|
|
5
|
-
this.cachedHash = {};
|
|
6
|
-
this.cachedSize = {};
|
|
7
|
-
}
|
|
8
|
-
add(files) {
|
|
9
|
-
const { cachedHash, cachedSize } = this;
|
|
10
|
-
for (const filename of files) {
|
|
11
|
-
if (fs.existsSync(filename)) {
|
|
12
|
-
const stats = fs.statSync(filename);
|
|
13
|
-
if (stats.isFile() && !cachedHash[filename]) {
|
|
14
|
-
cachedHash[filename] = this.hash(stats, filename);
|
|
15
|
-
cachedSize[filename] = stats.size;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
refresh(filename) {
|
|
21
|
-
const { cachedHash, cachedSize } = this;
|
|
22
|
-
if (fs.existsSync(filename)) {
|
|
23
|
-
const stats = fs.statSync(filename);
|
|
24
|
-
if (stats.isFile()) {
|
|
25
|
-
cachedHash[filename] = this.hash(stats, filename);
|
|
26
|
-
cachedSize[filename] = stats.size;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
del(filename) {
|
|
31
|
-
if (this.cachedHash[filename]) {
|
|
32
|
-
delete this.cachedHash[filename];
|
|
33
|
-
delete this.cachedSize[filename];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
isDiff(filename) {
|
|
37
|
-
const { cachedHash, cachedSize } = this;
|
|
38
|
-
const stats = fs.statSync(filename);
|
|
39
|
-
const hash = cachedHash[filename];
|
|
40
|
-
const size = cachedSize[filename];
|
|
41
|
-
if (stats.size !== size) {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
if (this.hash(stats, filename) !== hash) {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
has(filename) {
|
|
50
|
-
return Boolean(this.cachedHash[filename]);
|
|
51
|
-
}
|
|
52
|
-
hash(stats, filename) {
|
|
53
|
-
return crypto.createHash("md5").update(fs.readFileSync(filename)).digest("hex");
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
export {
|
|
57
|
-
StatsCache
|
|
58
|
-
};
|
package/dist/js/modern/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { DevServer as Server } from "./server";
|
|
2
|
-
var src_default = (options) => {
|
|
3
|
-
if (options == null) {
|
|
4
|
-
throw new Error("can not start server without options");
|
|
5
|
-
}
|
|
6
|
-
const server = new Server(options);
|
|
7
|
-
return server.init();
|
|
8
|
-
};
|
|
9
|
-
export {
|
|
10
|
-
Server,
|
|
11
|
-
src_default as default
|
|
12
|
-
};
|