@modern-js/server 2.4.0 → 2.5.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 +28 -0
- package/dist/{js/node → cjs}/constants.js +0 -0
- package/dist/{js/node → cjs}/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -0
- package/dist/{js/node → cjs}/dev-tools/dev-middleware/hmr-client/index.js +0 -0
- package/dist/{js/node → cjs}/dev-tools/dev-middleware/index.js +6 -39
- package/dist/{js/node → cjs}/dev-tools/dev-middleware/socket-server.js +0 -0
- package/dist/{js/node → cjs}/dev-tools/https/index.js +4 -24
- package/dist/{js/node → cjs}/dev-tools/mock/getMockData.js +9 -53
- package/dist/{js/node → cjs}/dev-tools/mock/index.js +19 -23
- package/dist/{js/node → cjs}/dev-tools/register/index.js +10 -23
- package/dist/{js/node → cjs}/dev-tools/watcher/dependency-tree.js +0 -0
- package/dist/{js/node → cjs}/dev-tools/watcher/index.js +7 -16
- package/dist/{js/node → cjs}/dev-tools/watcher/stats-cache.js +0 -0
- package/dist/{js/node → cjs}/index.js +0 -0
- package/dist/{js/node → cjs}/server/dev-server.js +92 -135
- package/dist/{js/node → cjs}/server/index.js +0 -0
- package/dist/{js/node → cjs}/types.js +0 -0
- package/dist/{js/treeshaking → esm}/constants.js +0 -0
- package/dist/{js/treeshaking → esm}/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -0
- package/dist/{js/treeshaking → esm}/dev-tools/dev-middleware/hmr-client/index.js +0 -0
- package/dist/{js/treeshaking → esm}/dev-tools/dev-middleware/index.js +2 -2
- package/dist/{js/treeshaking → esm}/dev-tools/dev-middleware/socket-server.js +2 -2
- package/dist/{js/treeshaking → esm}/dev-tools/https/index.js +0 -0
- package/dist/{js/treeshaking → esm}/dev-tools/mock/getMockData.js +1 -10
- package/dist/{js/treeshaking → esm}/dev-tools/mock/index.js +19 -3
- package/dist/{js/treeshaking → esm}/dev-tools/register/index.js +7 -6
- package/dist/{js/treeshaking → esm}/dev-tools/watcher/dependency-tree.js +0 -0
- package/dist/{js/treeshaking → esm}/dev-tools/watcher/index.js +4 -4
- package/dist/{js/treeshaking → esm}/dev-tools/watcher/stats-cache.js +3 -3
- package/dist/{js/treeshaking → esm}/index.js +0 -0
- package/dist/{js/treeshaking → esm}/server/dev-server.js +11 -10
- package/dist/{js/treeshaking → esm}/server/index.js +0 -0
- package/dist/{js/treeshaking → esm}/types.js +0 -0
- package/dist/{js/modern → esm-node}/constants.js +0 -0
- package/dist/{js/modern → esm-node}/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -0
- package/dist/{js/modern → esm-node}/dev-tools/dev-middleware/hmr-client/index.js +0 -0
- package/dist/{js/modern → esm-node}/dev-tools/dev-middleware/index.js +6 -41
- package/dist/{js/modern → esm-node}/dev-tools/dev-middleware/socket-server.js +0 -0
- package/dist/{js/modern → esm-node}/dev-tools/https/index.js +4 -24
- package/dist/esm-node/dev-tools/mock/getMockData.js +68 -0
- package/dist/{js/modern → esm-node}/dev-tools/mock/index.js +20 -24
- package/dist/{js/modern → esm-node}/dev-tools/register/index.js +10 -25
- package/dist/{js/modern → esm-node}/dev-tools/watcher/dependency-tree.js +0 -0
- package/dist/{js/modern → esm-node}/dev-tools/watcher/index.js +7 -18
- package/dist/{js/modern → esm-node}/dev-tools/watcher/stats-cache.js +0 -0
- package/dist/{js/modern → esm-node}/index.js +0 -0
- package/dist/esm-node/server/dev-server.js +247 -0
- package/dist/{js/modern → esm-node}/server/index.js +0 -0
- package/dist/{js/modern → esm-node}/types.js +0 -0
- package/dist/types/dev-tools/mock/getMockData.d.ts +1 -1
- package/package.json +13 -14
- package/dist/js/modern/dev-tools/mock/getMockData.js +0 -114
- package/dist/js/modern/server/dev-server.js +0 -293
|
@@ -1,39 +1,3 @@
|
|
|
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
|
-
var __async = (__this, __arguments, generator) => {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
var fulfilled = (value) => {
|
|
20
|
-
try {
|
|
21
|
-
step(generator.next(value));
|
|
22
|
-
} catch (e) {
|
|
23
|
-
reject(e);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var rejected = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.throw(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
1
|
import { EventEmitter } from "events";
|
|
38
2
|
import SocketServer from "./socket-server";
|
|
39
3
|
const noop = () => {
|
|
@@ -59,11 +23,11 @@ class DevMiddleware extends EventEmitter {
|
|
|
59
23
|
app.on("listening", () => {
|
|
60
24
|
this.socketServer.prepare(app);
|
|
61
25
|
});
|
|
62
|
-
app.on("close", () =>
|
|
26
|
+
app.on("close", async () => {
|
|
63
27
|
var _a;
|
|
64
28
|
(_a = this.middleware) == null ? void 0 : _a.close(noop);
|
|
65
29
|
this.socketServer.close();
|
|
66
|
-
})
|
|
30
|
+
});
|
|
67
31
|
}
|
|
68
32
|
sockWrite(type, data) {
|
|
69
33
|
this.socketServer.sockWrite(type, data);
|
|
@@ -80,12 +44,13 @@ class DevMiddleware extends EventEmitter {
|
|
|
80
44
|
}
|
|
81
45
|
};
|
|
82
46
|
const enableHMR = this.devOptions.hot || this.devOptions.liveReload;
|
|
83
|
-
const middleware = devMiddleware(
|
|
47
|
+
const middleware = devMiddleware({
|
|
84
48
|
headers: devOptions.headers,
|
|
85
49
|
stats: false,
|
|
86
50
|
callbacks,
|
|
87
|
-
hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0
|
|
88
|
-
|
|
51
|
+
hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0,
|
|
52
|
+
...devOptions.devMiddleware
|
|
53
|
+
});
|
|
89
54
|
return middleware;
|
|
90
55
|
}
|
|
91
56
|
}
|
|
File without changes
|
|
@@ -1,32 +1,12 @@
|
|
|
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
1
|
import { chalk, getPackageManager, logger } from "@modern-js/utils";
|
|
22
|
-
const genHttpsOptions = (userOptions, pwd) =>
|
|
2
|
+
const genHttpsOptions = async (userOptions, pwd) => {
|
|
23
3
|
const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
|
|
24
4
|
if (!httpsOptions.key || !httpsOptions.cert) {
|
|
25
5
|
let devcertPath;
|
|
26
6
|
try {
|
|
27
7
|
devcertPath = require.resolve("devcert", { paths: [pwd, __dirname] });
|
|
28
8
|
} catch (err) {
|
|
29
|
-
const packageManager =
|
|
9
|
+
const packageManager = await getPackageManager(pwd);
|
|
30
10
|
const command = chalk.yellow.bold(
|
|
31
11
|
`${packageManager} install devcert@1.2.2 -D`
|
|
32
12
|
);
|
|
@@ -39,11 +19,11 @@ const genHttpsOptions = (userOptions, pwd) => __async(void 0, null, function* ()
|
|
|
39
19
|
throw new Error('[https] "devcert" is not found.');
|
|
40
20
|
}
|
|
41
21
|
const devcert = require(devcertPath);
|
|
42
|
-
const selfsign =
|
|
22
|
+
const selfsign = await devcert.certificateFor(["localhost"]);
|
|
43
23
|
return selfsign;
|
|
44
24
|
}
|
|
45
25
|
return httpsOptions;
|
|
46
|
-
}
|
|
26
|
+
};
|
|
47
27
|
export {
|
|
48
28
|
genHttpsOptions
|
|
49
29
|
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
const VALID_METHODS = ["get", "post", "put", "delete", "patch"];
|
|
3
|
+
const BODY_PARSED_METHODS = ["post", "put", "delete", "patch"];
|
|
4
|
+
const createFunctionDataHandler = (method, handler) => async (context, next) => {
|
|
5
|
+
const { req, res } = context;
|
|
6
|
+
return handler(req, res, next);
|
|
7
|
+
};
|
|
8
|
+
const createStaticDataHandler = (method, handler) => (context) => {
|
|
9
|
+
const { res } = context;
|
|
10
|
+
res.setHeader("Content-Type", "application/json");
|
|
11
|
+
res.end(JSON.stringify(handler));
|
|
12
|
+
};
|
|
13
|
+
const allowTypes = ["object", "function"];
|
|
14
|
+
var getMockData_default = (mockConfig) => Object.keys(mockConfig).reduce((acc, key) => {
|
|
15
|
+
const handler = mockConfig[key];
|
|
16
|
+
const type = typeof handler;
|
|
17
|
+
if (!allowTypes.includes(type)) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`mock value of ${key} should be object or function, but got ${type}`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
const meta = parseKey(key);
|
|
23
|
+
if (type === "object") {
|
|
24
|
+
acc.push({
|
|
25
|
+
...meta,
|
|
26
|
+
handler: createStaticDataHandler(meta.method, handler)
|
|
27
|
+
});
|
|
28
|
+
} else {
|
|
29
|
+
acc.push({
|
|
30
|
+
...meta,
|
|
31
|
+
handler: createFunctionDataHandler(meta.method, handler)
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return acc;
|
|
35
|
+
}, []);
|
|
36
|
+
const _blank = " ";
|
|
37
|
+
const parseKey = (key) => {
|
|
38
|
+
const splitted = key.split(_blank).filter(Boolean);
|
|
39
|
+
if (splitted.length > 1) {
|
|
40
|
+
const [method, pathname] = splitted;
|
|
41
|
+
return {
|
|
42
|
+
method: method.toLowerCase(),
|
|
43
|
+
path: pathname
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
method: "get",
|
|
48
|
+
path: key
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const getMatched = (context, mockApiList) => {
|
|
52
|
+
const { path: targetPathname, method: targetMethod } = context;
|
|
53
|
+
const matched = mockApiList.find((mockApi) => {
|
|
54
|
+
const { method, path: pathname } = mockApi;
|
|
55
|
+
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
56
|
+
return match(pathname, {
|
|
57
|
+
encode: encodeURI,
|
|
58
|
+
decode: decodeURIComponent
|
|
59
|
+
})(targetPathname);
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
});
|
|
63
|
+
return matched;
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
getMockData_default as default,
|
|
67
|
+
getMatched
|
|
68
|
+
};
|
|
@@ -1,25 +1,5 @@
|
|
|
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
1
|
import path from "path";
|
|
22
|
-
import { fs } from "@modern-js/utils";
|
|
2
|
+
import { compatRequire, fs } from "@modern-js/utils";
|
|
23
3
|
import { AGGRED_DIR } from "@modern-js/prod-server";
|
|
24
4
|
import getMockData, { getMatched } from "./getMockData";
|
|
25
5
|
const createMockHandler = ({ pwd }) => {
|
|
@@ -35,11 +15,27 @@ const createMockHandler = ({ pwd }) => {
|
|
|
35
15
|
if (!filepath) {
|
|
36
16
|
return null;
|
|
37
17
|
}
|
|
38
|
-
const
|
|
18
|
+
const {
|
|
19
|
+
default: mockModule,
|
|
20
|
+
config
|
|
21
|
+
} = compatRequire(filepath, false);
|
|
22
|
+
if ((config == null ? void 0 : config.enable) === false) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (!mockModule) {
|
|
26
|
+
throw new Error(`Mock file ${filepath} parsed failed!`);
|
|
27
|
+
}
|
|
28
|
+
const apiList = getMockData(mockModule);
|
|
39
29
|
if (!apiList || apiList.length === 0) {
|
|
40
30
|
return null;
|
|
41
31
|
}
|
|
42
|
-
return (context, next) =>
|
|
32
|
+
return async (context, next) => {
|
|
33
|
+
if (typeof (config == null ? void 0 : config.enable) === "function") {
|
|
34
|
+
const enableMock = config.enable(context.req, context.res);
|
|
35
|
+
if (!enableMock) {
|
|
36
|
+
return next();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
43
39
|
const { res } = context;
|
|
44
40
|
const matched = getMatched(context, apiList);
|
|
45
41
|
if (!matched) {
|
|
@@ -47,7 +43,7 @@ const createMockHandler = ({ pwd }) => {
|
|
|
47
43
|
}
|
|
48
44
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
49
45
|
return matched.handler(context, next);
|
|
50
|
-
}
|
|
46
|
+
};
|
|
51
47
|
};
|
|
52
48
|
export {
|
|
53
49
|
createMockHandler
|
|
@@ -1,22 +1,3 @@
|
|
|
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
1
|
import path from "path";
|
|
21
2
|
import { resolveBabelConfig } from "@modern-js/server-utils";
|
|
22
3
|
import { fs, getAliasConfig, createDebugger } from "@modern-js/utils";
|
|
@@ -58,9 +39,10 @@ const enableRegister = (projectRoot, config) => {
|
|
|
58
39
|
if (typeof tsPath === "string") {
|
|
59
40
|
tsPath = [tsPath];
|
|
60
41
|
}
|
|
61
|
-
return
|
|
42
|
+
return {
|
|
43
|
+
...o,
|
|
62
44
|
[`${key}`]: tsPath
|
|
63
|
-
}
|
|
45
|
+
};
|
|
64
46
|
}, {});
|
|
65
47
|
tsConfigPaths.register({
|
|
66
48
|
baseUrl: absoluteBaseUrl || "./",
|
|
@@ -68,6 +50,7 @@ const enableRegister = (projectRoot, config) => {
|
|
|
68
50
|
});
|
|
69
51
|
tsNode.register({
|
|
70
52
|
project: tsconfigPath,
|
|
53
|
+
scope: true,
|
|
71
54
|
files: true,
|
|
72
55
|
transpileOnly: true,
|
|
73
56
|
ignore: ["(?:^|/)node_modules/", `(?:^|/)${distPath}/`]
|
|
@@ -76,19 +59,21 @@ const enableRegister = (projectRoot, config) => {
|
|
|
76
59
|
debug("use @babel/register");
|
|
77
60
|
const babelConfig = resolveBabelConfig(
|
|
78
61
|
projectRoot,
|
|
79
|
-
|
|
62
|
+
{
|
|
63
|
+
...config.source,
|
|
80
64
|
babelConfig: (_b = config.tools) == null ? void 0 : _b.babel,
|
|
81
65
|
server: {
|
|
82
66
|
compiler: (_c = config.server) == null ? void 0 : _c.compiler
|
|
83
67
|
}
|
|
84
|
-
}
|
|
68
|
+
},
|
|
85
69
|
{
|
|
86
70
|
tsconfigPath,
|
|
87
71
|
syntax: "es6+",
|
|
88
72
|
type: "commonjs"
|
|
89
73
|
}
|
|
90
74
|
);
|
|
91
|
-
return require("@babel/register")(
|
|
75
|
+
return require("@babel/register")({
|
|
76
|
+
...babelConfig,
|
|
92
77
|
only: [
|
|
93
78
|
function(filePath) {
|
|
94
79
|
if (filePath.includes(`node_modules${path.sep}.modern-js`)) {
|
|
@@ -103,7 +88,7 @@ const enableRegister = (projectRoot, config) => {
|
|
|
103
88
|
babelrc: false,
|
|
104
89
|
configFile: false,
|
|
105
90
|
root: projectRoot
|
|
106
|
-
})
|
|
91
|
+
});
|
|
107
92
|
}
|
|
108
93
|
};
|
|
109
94
|
export {
|
|
File without changes
|
|
@@ -1,19 +1,3 @@
|
|
|
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
1
|
import path from "path";
|
|
18
2
|
import { fs, chokidar } from "@modern-js/utils";
|
|
19
3
|
import { DependencyTree } from "./dependency-tree";
|
|
@@ -33,7 +17,9 @@ const getWatchedFiles = (watcher) => {
|
|
|
33
17
|
return files;
|
|
34
18
|
};
|
|
35
19
|
const mergeWatchOptions = (options) => {
|
|
36
|
-
const watchOptions =
|
|
20
|
+
const watchOptions = {
|
|
21
|
+
...options
|
|
22
|
+
};
|
|
37
23
|
if (watchOptions) {
|
|
38
24
|
const { ignored } = watchOptions;
|
|
39
25
|
const finalIgnored = ignored ? [
|
|
@@ -44,7 +30,10 @@ const mergeWatchOptions = (options) => {
|
|
|
44
30
|
watchOptions.ignored = finalIgnored;
|
|
45
31
|
}
|
|
46
32
|
}
|
|
47
|
-
const finalWatchOptions =
|
|
33
|
+
const finalWatchOptions = {
|
|
34
|
+
...defaultWatchOptions,
|
|
35
|
+
...watchOptions
|
|
36
|
+
};
|
|
48
37
|
return finalWatchOptions;
|
|
49
38
|
};
|
|
50
39
|
class Watcher {
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { createServer } from "http";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { createServer as createHttpsServer } from "https";
|
|
4
|
+
import {
|
|
5
|
+
API_DIR,
|
|
6
|
+
SERVER_BUNDLE_DIRECTORY,
|
|
7
|
+
SERVER_DIR,
|
|
8
|
+
SHARED_DIR
|
|
9
|
+
} from "@modern-js/utils";
|
|
10
|
+
import {
|
|
11
|
+
createProxyHandler,
|
|
12
|
+
ModernServer,
|
|
13
|
+
AGGRED_DIR
|
|
14
|
+
} from "@modern-js/prod-server";
|
|
15
|
+
import { LOADABLE_STATS_FILE } from "@modern-js/utils/constants";
|
|
16
|
+
import { getDefaultDevOptions } from "../constants";
|
|
17
|
+
import { createMockHandler } from "../dev-tools/mock";
|
|
18
|
+
import { enableRegister } from "../dev-tools/register";
|
|
19
|
+
import Watcher, { mergeWatchOptions } from "../dev-tools/watcher";
|
|
20
|
+
import DevMiddleware from "../dev-tools/dev-middleware";
|
|
21
|
+
class ModernDevServer extends ModernServer {
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super(options);
|
|
24
|
+
this.mockHandler = null;
|
|
25
|
+
this.workDir = this.pwd;
|
|
26
|
+
this.dev = this.getDevOptions(options);
|
|
27
|
+
this.devMiddleware = new DevMiddleware({
|
|
28
|
+
dev: this.dev,
|
|
29
|
+
devMiddleware: options.devMiddleware
|
|
30
|
+
});
|
|
31
|
+
enableRegister(this.pwd, this.conf);
|
|
32
|
+
}
|
|
33
|
+
getDevOptions(options) {
|
|
34
|
+
const devOptions = typeof options.dev === "boolean" ? {} : options.dev;
|
|
35
|
+
const defaultOptions = getDefaultDevOptions();
|
|
36
|
+
return {
|
|
37
|
+
...defaultOptions,
|
|
38
|
+
...devOptions,
|
|
39
|
+
client: {
|
|
40
|
+
...defaultOptions.client,
|
|
41
|
+
...devOptions == null ? void 0 : devOptions.client
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
addMiddlewareHandler(handlers) {
|
|
46
|
+
handlers.forEach((handler) => {
|
|
47
|
+
this.addHandler((ctx, next) => {
|
|
48
|
+
const { req, res } = ctx;
|
|
49
|
+
return handler(req, res, next);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
applySetupMiddlewares() {
|
|
54
|
+
const setupMiddlewares = this.dev.setupMiddlewares || [];
|
|
55
|
+
const serverOptions = {
|
|
56
|
+
sockWrite: (type, data) => this.devMiddleware.sockWrite(type, data)
|
|
57
|
+
};
|
|
58
|
+
const befores = [];
|
|
59
|
+
const afters = [];
|
|
60
|
+
setupMiddlewares.forEach((handler) => {
|
|
61
|
+
handler(
|
|
62
|
+
{
|
|
63
|
+
unshift: (...handlers) => befores.unshift(...handlers),
|
|
64
|
+
push: (...handlers) => afters.push(...handlers)
|
|
65
|
+
},
|
|
66
|
+
serverOptions
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
return { befores, afters };
|
|
70
|
+
}
|
|
71
|
+
async onInit(runner, app) {
|
|
72
|
+
this.runner = runner;
|
|
73
|
+
const { dev } = this;
|
|
74
|
+
const { befores, afters } = this.applySetupMiddlewares();
|
|
75
|
+
const beforeHandlers = await this.setupBeforeDevMiddleware();
|
|
76
|
+
this.addMiddlewareHandler([...beforeHandlers, ...befores]);
|
|
77
|
+
await this.applyDefaultMiddlewares(app);
|
|
78
|
+
const afterHandlers = await this.setupAfterDevMiddleware();
|
|
79
|
+
this.addMiddlewareHandler([...afters, ...afterHandlers]);
|
|
80
|
+
await super.onInit(runner, app);
|
|
81
|
+
if (dev.watch) {
|
|
82
|
+
this.startWatcher();
|
|
83
|
+
app.on("close", async () => {
|
|
84
|
+
var _a;
|
|
85
|
+
await ((_a = this.watcher) == null ? void 0 : _a.close());
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async applyDefaultMiddlewares(app) {
|
|
90
|
+
const { pwd, dev, devMiddleware } = this;
|
|
91
|
+
this.addHandler((ctx, next) => {
|
|
92
|
+
ctx.res.setHeader("Access-Control-Allow-Origin", "*");
|
|
93
|
+
if (ctx.path.includes("hot-update")) {
|
|
94
|
+
ctx.res.setHeader("Access-Control-Allow-Credentials", "false");
|
|
95
|
+
}
|
|
96
|
+
const confHeaders = dev.headers;
|
|
97
|
+
if (confHeaders) {
|
|
98
|
+
for (const [key, value] of Object.entries(confHeaders)) {
|
|
99
|
+
ctx.res.setHeader(key, value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
next();
|
|
103
|
+
});
|
|
104
|
+
this.mockHandler = createMockHandler({ pwd });
|
|
105
|
+
this.addHandler((ctx, next) => {
|
|
106
|
+
if (this.mockHandler) {
|
|
107
|
+
this.mockHandler(ctx, next);
|
|
108
|
+
} else {
|
|
109
|
+
next();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const proxyHandlers = createProxyHandler(dev.proxy);
|
|
113
|
+
if (proxyHandlers) {
|
|
114
|
+
proxyHandlers.forEach((handler) => {
|
|
115
|
+
this.addHandler(handler);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
devMiddleware.init(app);
|
|
119
|
+
devMiddleware.on("change", (stats) => {
|
|
120
|
+
if (stats.toJson({ all: false }).name === "client") {
|
|
121
|
+
this.onRepack({ routes: this.getRoutes() });
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
this.addHandler((ctx, next) => {
|
|
125
|
+
const { req, res } = ctx;
|
|
126
|
+
if (devMiddleware.middleware) {
|
|
127
|
+
devMiddleware.middleware(req, res, next);
|
|
128
|
+
} else {
|
|
129
|
+
next();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
if (dev.historyApiFallback) {
|
|
133
|
+
const { default: connectHistoryApiFallback } = await import("connect-history-api-fallback");
|
|
134
|
+
const historyApiFallbackMiddleware = connectHistoryApiFallback(
|
|
135
|
+
typeof dev.historyApiFallback === "boolean" ? {} : dev.historyApiFallback
|
|
136
|
+
);
|
|
137
|
+
this.addHandler(
|
|
138
|
+
(ctx, next) => historyApiFallbackMiddleware(ctx.req, ctx.res, next)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
onRepack(options = {}) {
|
|
143
|
+
if (Array.isArray(options.routes)) {
|
|
144
|
+
this.router.reset(this.filterRoutes(options.routes));
|
|
145
|
+
}
|
|
146
|
+
this.cleanSSRCache();
|
|
147
|
+
this.reader.updateFile();
|
|
148
|
+
this.runner.repack();
|
|
149
|
+
super.onRepack(options);
|
|
150
|
+
}
|
|
151
|
+
async createHTTPServer(handler) {
|
|
152
|
+
const { dev } = this;
|
|
153
|
+
const devHttpsOption = typeof dev === "object" && dev.https;
|
|
154
|
+
if (devHttpsOption) {
|
|
155
|
+
const { genHttpsOptions } = require("../dev-tools/https");
|
|
156
|
+
const httpsOptions = await genHttpsOptions(devHttpsOption, this.pwd);
|
|
157
|
+
return createHttpsServer(httpsOptions, handler);
|
|
158
|
+
} else {
|
|
159
|
+
return createServer(handler);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
warmupSSRBundle() {
|
|
163
|
+
}
|
|
164
|
+
onServerChange({
|
|
165
|
+
filepath,
|
|
166
|
+
event
|
|
167
|
+
}) {
|
|
168
|
+
const { pwd } = this;
|
|
169
|
+
const { mock } = AGGRED_DIR;
|
|
170
|
+
const mockPath = path.normalize(path.join(pwd, mock));
|
|
171
|
+
this.runner.reset();
|
|
172
|
+
if (filepath.startsWith(mockPath)) {
|
|
173
|
+
this.mockHandler = createMockHandler({ pwd });
|
|
174
|
+
} else {
|
|
175
|
+
try {
|
|
176
|
+
const success = this.runner.onApiChange([
|
|
177
|
+
{ filename: filepath, event }
|
|
178
|
+
]);
|
|
179
|
+
if (success !== true) {
|
|
180
|
+
super.onServerChange({ filepath });
|
|
181
|
+
}
|
|
182
|
+
} catch (e) {
|
|
183
|
+
this.logger.error(e);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
createContext(req, res) {
|
|
188
|
+
return super.createContext(req, res, { etag: true });
|
|
189
|
+
}
|
|
190
|
+
async setupBeforeDevMiddleware() {
|
|
191
|
+
const { runner, conf, dev } = this;
|
|
192
|
+
const setupMids = dev.before || [];
|
|
193
|
+
const pluginMids = await runner.beforeDevServer(conf);
|
|
194
|
+
return [...setupMids, ...pluginMids].flat();
|
|
195
|
+
}
|
|
196
|
+
async setupAfterDevMiddleware() {
|
|
197
|
+
const { runner, conf, dev } = this;
|
|
198
|
+
const setupMids = dev.after || [];
|
|
199
|
+
const pluginMids = await runner.afterDevServer(conf);
|
|
200
|
+
return [...pluginMids, ...setupMids].flat();
|
|
201
|
+
}
|
|
202
|
+
cleanSSRCache() {
|
|
203
|
+
const { distDir } = this;
|
|
204
|
+
const bundles = this.router.getBundles();
|
|
205
|
+
bundles.forEach((bundle) => {
|
|
206
|
+
const filepath = path.join(distDir, bundle);
|
|
207
|
+
if (require.cache[filepath]) {
|
|
208
|
+
delete require.cache[filepath];
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
const loadable = path.join(distDir, LOADABLE_STATS_FILE);
|
|
212
|
+
if (require.cache[loadable]) {
|
|
213
|
+
delete require.cache[loadable];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
startWatcher() {
|
|
217
|
+
var _a;
|
|
218
|
+
const { pwd, distDir } = this;
|
|
219
|
+
const { mock } = AGGRED_DIR;
|
|
220
|
+
const defaultWatched = [
|
|
221
|
+
`${mock}/**/*`,
|
|
222
|
+
`${SERVER_DIR}/**/*`,
|
|
223
|
+
`${API_DIR}/**`,
|
|
224
|
+
`${SHARED_DIR}/**/*`,
|
|
225
|
+
`${distDir}/${SERVER_BUNDLE_DIRECTORY}/*-server-loaders.js`
|
|
226
|
+
];
|
|
227
|
+
const watchOptions = mergeWatchOptions((_a = this.conf.server) == null ? void 0 : _a.watchOptions);
|
|
228
|
+
const defaultWatchedPaths = defaultWatched.map((p) => {
|
|
229
|
+
const finalPath = path.isAbsolute(p) ? p : path.join(pwd, p);
|
|
230
|
+
return path.normalize(finalPath);
|
|
231
|
+
});
|
|
232
|
+
const watcher = new Watcher();
|
|
233
|
+
watcher.createDepTree();
|
|
234
|
+
watcher.listen(defaultWatchedPaths, watchOptions, (filepath, event) => {
|
|
235
|
+
watcher.updateDepTree();
|
|
236
|
+
watcher.cleanDepCache(filepath);
|
|
237
|
+
this.onServerChange({
|
|
238
|
+
filepath,
|
|
239
|
+
event
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
this.watcher = watcher;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
export {
|
|
246
|
+
ModernDevServer
|
|
247
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -12,7 +12,7 @@ export type MockApi = {
|
|
|
12
12
|
declare const createFunctionDataHandler: (method: string, handler: (req: IncomingMessage, res: ServerResponse, next: NextFunction) => void) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
|
|
13
13
|
declare const createStaticDataHandler: (method: string, handler: Record<string, any>) => (context: ModernServerContext) => void;
|
|
14
14
|
|
|
15
|
-
declare const _default: (
|
|
15
|
+
declare const _default: (mockConfig: MockConfig) => MockApi[];
|
|
16
16
|
|
|
17
17
|
export default _default;
|
|
18
18
|
export declare const getMatched: (context: ModernServerContext, mockApiList: MockApi[]) => MockApi | undefined;
|