@modern-js/server 2.53.0 → 2.54.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/dist/cjs/createDevServer.js +15 -47
- package/dist/cjs/dev.js +85 -0
- package/dist/cjs/helpers/index.js +20 -13
- package/dist/cjs/helpers/mock.js +113 -0
- package/dist/cjs/helpers/repack.js +5 -1
- package/dist/esm/createDevServer.js +28 -71
- package/dist/esm/dev.js +82 -0
- package/dist/esm/helpers/index.js +14 -8
- package/dist/esm/helpers/mock.js +198 -0
- package/dist/esm/helpers/repack.js +5 -1
- package/dist/esm-node/createDevServer.js +15 -47
- package/dist/esm-node/dev.js +61 -0
- package/dist/esm-node/helpers/index.js +15 -9
- package/dist/esm-node/helpers/mock.js +79 -0
- package/dist/esm-node/helpers/repack.js +5 -1
- package/dist/types/createDevServer.d.ts +4 -4
- package/dist/types/dev.d.ts +3 -0
- package/dist/types/helpers/fileReader.d.ts +1 -1
- package/dist/types/helpers/index.d.ts +2 -1
- package/dist/types/helpers/mock.d.ts +5 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/types.d.ts +5 -2
- package/package.json +10 -10
|
@@ -32,31 +32,23 @@ __export(createDevServer_exports, {
|
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(createDevServer_exports);
|
|
34
34
|
var import_node_path = __toESM(require("node:path"));
|
|
35
|
-
var
|
|
36
|
-
var import_node = require("@modern-js/server-core/
|
|
37
|
-
var import_utils = require("@modern-js/utils");
|
|
35
|
+
var import_server_core = require("@modern-js/server-core");
|
|
36
|
+
var import_node = require("@modern-js/server-core/node");
|
|
38
37
|
var import_helpers = require("./helpers");
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
var import_dev = require("./dev");
|
|
39
|
+
const createDevServer = async (options, applyPlugins) => {
|
|
40
|
+
const { config, pwd, serverConfigFile, serverConfigPath } = options;
|
|
41
41
|
const dev = (0, import_helpers.getDevOptions)(options);
|
|
42
42
|
const distDir = import_node_path.default.resolve(pwd, config.output.path || "dist");
|
|
43
|
-
const
|
|
44
|
-
const sharedDir = (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || import_utils.SHARED_DIR;
|
|
43
|
+
const serverConfig = (0, import_node.loadServerConfig)(distDir, serverConfigFile, serverConfigPath);
|
|
45
44
|
const prodServerOptions = {
|
|
46
45
|
...options,
|
|
47
46
|
pwd: distDir
|
|
48
47
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
rsbuildMiddlewares && server.all("*", (0, import_node.connectMid2HonoMid)(rsbuildMiddlewares));
|
|
54
|
-
await (0, import_node.registerMockHandlers)({
|
|
55
|
-
pwd,
|
|
56
|
-
server
|
|
57
|
-
});
|
|
58
|
-
server.use("*", (0, import_helpers.initFileReader)());
|
|
59
|
-
await server.init();
|
|
48
|
+
if (serverConfig) {
|
|
49
|
+
prodServerOptions.serverConfig = serverConfig;
|
|
50
|
+
}
|
|
51
|
+
const server = (0, import_server_core.createServerBase)(prodServerOptions);
|
|
60
52
|
const devHttpsOption = typeof dev === "object" && dev.https;
|
|
61
53
|
let nodeServer;
|
|
62
54
|
if (devHttpsOption) {
|
|
@@ -66,35 +58,11 @@ const createDevServer = async (options, initProdMiddlewares) => {
|
|
|
66
58
|
} else {
|
|
67
59
|
nodeServer = await (0, import_node.createNodeServer)(server.handle.bind(server));
|
|
68
60
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
onHTTPUpgrade && nodeServer.on("upgrade", onHTTPUpgrade);
|
|
77
|
-
await server.runner.beforeServerInit({
|
|
78
|
-
app: nodeServer
|
|
79
|
-
});
|
|
80
|
-
await initProdMiddlewares(server, prodServerOptions);
|
|
81
|
-
if (dev.watch) {
|
|
82
|
-
const { watchOptions } = config.server;
|
|
83
|
-
const watcher = (0, import_helpers.startWatcher)({
|
|
84
|
-
pwd,
|
|
85
|
-
distDir,
|
|
86
|
-
apiDir,
|
|
87
|
-
sharedDir,
|
|
88
|
-
watchOptions,
|
|
89
|
-
server
|
|
90
|
-
});
|
|
91
|
-
closeCb.push(watcher.close.bind(watcher));
|
|
92
|
-
}
|
|
93
|
-
closeCb.length > 0 && nodeServer.on("close", () => {
|
|
94
|
-
closeCb.forEach((cb) => {
|
|
95
|
-
cb();
|
|
96
|
-
});
|
|
97
|
-
});
|
|
61
|
+
server.addPlugins([
|
|
62
|
+
(0, import_dev.devPlugin)(options)
|
|
63
|
+
]);
|
|
64
|
+
await applyPlugins(server, prodServerOptions, nodeServer);
|
|
65
|
+
await server.init();
|
|
98
66
|
return nodeServer;
|
|
99
67
|
};
|
|
100
68
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/cjs/dev.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var dev_exports = {};
|
|
20
|
+
__export(dev_exports, {
|
|
21
|
+
devPlugin: () => devPlugin
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(dev_exports);
|
|
24
|
+
var import_node = require("@modern-js/server-core/node");
|
|
25
|
+
var import_utils = require("@modern-js/utils");
|
|
26
|
+
var import_helpers = require("./helpers");
|
|
27
|
+
const devPlugin = (options) => ({
|
|
28
|
+
name: "@modern-js/plugin-dev",
|
|
29
|
+
setup(api) {
|
|
30
|
+
const { getMiddlewares, rsbuild, config, pwd } = options;
|
|
31
|
+
const closeCb = [];
|
|
32
|
+
const { middlewares: rsbuildMiddlewares, close, onHTTPUpgrade } = (getMiddlewares === null || getMiddlewares === void 0 ? void 0 : getMiddlewares()) || {};
|
|
33
|
+
close && closeCb.push(close);
|
|
34
|
+
const dev = (0, import_helpers.getDevOptions)(options);
|
|
35
|
+
return {
|
|
36
|
+
async prepare() {
|
|
37
|
+
const { middlewares, distDirectory, routes, nodeServer, apiDirectory, sharedDirectory, serverBase } = api.useAppContext();
|
|
38
|
+
onHTTPUpgrade && (nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.on("upgrade", onHTTPUpgrade));
|
|
39
|
+
const runner = api.useHookRunners();
|
|
40
|
+
rsbuild === null || rsbuild === void 0 ? void 0 : rsbuild.onDevCompileDone(({ stats }) => {
|
|
41
|
+
if (stats.toJson({
|
|
42
|
+
all: false
|
|
43
|
+
}).name !== "server") {
|
|
44
|
+
(0, import_helpers.onRepack)(distDirectory, runner, routes || []);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (dev.watch) {
|
|
48
|
+
const { watchOptions } = config.server;
|
|
49
|
+
const watcher = (0, import_helpers.startWatcher)({
|
|
50
|
+
pwd,
|
|
51
|
+
distDir: distDirectory,
|
|
52
|
+
apiDir: apiDirectory || import_utils.API_DIR,
|
|
53
|
+
sharedDir: sharedDirectory || import_utils.SHARED_DIR,
|
|
54
|
+
watchOptions,
|
|
55
|
+
server: serverBase
|
|
56
|
+
});
|
|
57
|
+
closeCb.push(watcher.close.bind(watcher));
|
|
58
|
+
}
|
|
59
|
+
closeCb.length > 0 && (nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.on("close", () => {
|
|
60
|
+
closeCb.forEach((cb) => {
|
|
61
|
+
cb();
|
|
62
|
+
});
|
|
63
|
+
}));
|
|
64
|
+
if (rsbuildMiddlewares) {
|
|
65
|
+
middlewares.push({
|
|
66
|
+
name: "rsbuild-dev",
|
|
67
|
+
handler: (0, import_node.connectMid2HonoMid)(rsbuildMiddlewares)
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
await (0, import_helpers.registerMockHandlers)({
|
|
71
|
+
pwd,
|
|
72
|
+
server: serverBase
|
|
73
|
+
});
|
|
74
|
+
middlewares.push({
|
|
75
|
+
name: "init-file-reader",
|
|
76
|
+
handler: (0, import_helpers.initFileReader)()
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
devPlugin
|
|
85
|
+
});
|
|
@@ -34,32 +34,38 @@ __export(helpers_exports, {
|
|
|
34
34
|
module.exports = __toCommonJS(helpers_exports);
|
|
35
35
|
var import_path = __toESM(require("path"));
|
|
36
36
|
var import_utils = require("@modern-js/utils");
|
|
37
|
-
var
|
|
38
|
-
var import_node = require("@modern-js/server-core/base/node");
|
|
37
|
+
var import_server_core = require("@modern-js/server-core");
|
|
39
38
|
var import_watcher = __toESM(require("../dev-tools/watcher"));
|
|
40
39
|
var import_utils2 = require("./utils");
|
|
40
|
+
var import_mock = require("./mock");
|
|
41
41
|
__reExport(helpers_exports, require("./repack"), module.exports);
|
|
42
42
|
__reExport(helpers_exports, require("./devOptions"), module.exports);
|
|
43
43
|
__reExport(helpers_exports, require("./fileReader"), module.exports);
|
|
44
|
+
__reExport(helpers_exports, require("./mock"), module.exports);
|
|
44
45
|
async function onServerChange({ pwd, filepath, event, server }) {
|
|
45
|
-
const { mock } =
|
|
46
|
+
const { mock } = import_server_core.AGGRED_DIR;
|
|
46
47
|
const mockPath = import_path.default.normalize(import_path.default.join(pwd, mock));
|
|
47
48
|
const { runner } = server;
|
|
48
|
-
runner.reset();
|
|
49
49
|
if (filepath.startsWith(mockPath)) {
|
|
50
|
-
await (0,
|
|
50
|
+
await (0, import_mock.registerMockHandlers)({
|
|
51
51
|
pwd,
|
|
52
52
|
server
|
|
53
53
|
});
|
|
54
54
|
import_utils.logger.info("Finish registering the mock handlers");
|
|
55
55
|
} else {
|
|
56
56
|
try {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
const fileChangeEvent = {
|
|
58
|
+
type: "file-change",
|
|
59
|
+
payload: [
|
|
60
|
+
{
|
|
61
|
+
filename: filepath,
|
|
62
|
+
event
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
await runner.reset({
|
|
67
|
+
event: fileChangeEvent
|
|
68
|
+
});
|
|
63
69
|
(0, import_utils2.debug)(`Finish reload server, trigger by ${filepath} ${event}`);
|
|
64
70
|
} catch (e) {
|
|
65
71
|
import_utils.logger.error(e);
|
|
@@ -67,7 +73,7 @@ async function onServerChange({ pwd, filepath, event, server }) {
|
|
|
67
73
|
}
|
|
68
74
|
}
|
|
69
75
|
function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, server }) {
|
|
70
|
-
const { mock } =
|
|
76
|
+
const { mock } = import_server_core.AGGRED_DIR;
|
|
71
77
|
const defaultWatched = [
|
|
72
78
|
`${mock}/**/*`,
|
|
73
79
|
`${import_utils.SERVER_DIR}/**/*`,
|
|
@@ -103,5 +109,6 @@ function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, server })
|
|
|
103
109
|
startWatcher,
|
|
104
110
|
...require("./repack"),
|
|
105
111
|
...require("./devOptions"),
|
|
106
|
-
...require("./fileReader")
|
|
112
|
+
...require("./fileReader"),
|
|
113
|
+
...require("./mock")
|
|
107
114
|
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var mock_exports = {};
|
|
30
|
+
__export(mock_exports, {
|
|
31
|
+
registerMockHandlers: () => registerMockHandlers
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(mock_exports);
|
|
34
|
+
var import_node_path = __toESM(require("node:path"));
|
|
35
|
+
var import_utils = require("@modern-js/utils");
|
|
36
|
+
var import_server_core = require("@modern-js/server-core");
|
|
37
|
+
var import_node = require("@modern-js/server-core/node");
|
|
38
|
+
const parseKey = (key) => {
|
|
39
|
+
const _blank = " ";
|
|
40
|
+
const splitted = key.split(_blank).filter(Boolean);
|
|
41
|
+
if (splitted.length > 1) {
|
|
42
|
+
const [method, pathname] = splitted;
|
|
43
|
+
return {
|
|
44
|
+
method: method.toLowerCase(),
|
|
45
|
+
path: pathname
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
method: "get",
|
|
50
|
+
path: key
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const mockHandlerRegistry = /* @__PURE__ */ new Map();
|
|
54
|
+
const registerMockHandlers = async ({ pwd, server }) => {
|
|
55
|
+
const exts = [
|
|
56
|
+
".ts",
|
|
57
|
+
".js"
|
|
58
|
+
];
|
|
59
|
+
let mockFilePath = "";
|
|
60
|
+
for (const ext of exts) {
|
|
61
|
+
const maybeMatch = import_node_path.default.join(pwd, `${import_server_core.AGGRED_DIR.mock}/index${ext}`);
|
|
62
|
+
if (await import_utils.fs.pathExists(maybeMatch)) {
|
|
63
|
+
mockFilePath = maybeMatch;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!mockFilePath) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const { default: mockModule, config } = await Promise.resolve().then(() => __toESM(require(mockFilePath)));
|
|
71
|
+
const enable = config === null || config === void 0 ? void 0 : config.enable;
|
|
72
|
+
if (enable === false) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!mockModule) {
|
|
76
|
+
throw new Error(`Mock file ${mockFilePath} parsed failed!`);
|
|
77
|
+
}
|
|
78
|
+
Object.entries(mockModule).forEach(([key, handler]) => {
|
|
79
|
+
const { method, path: path2 } = parseKey(key);
|
|
80
|
+
const methodName = method.toLowerCase();
|
|
81
|
+
const handlerId = `${methodName}-${path2}`;
|
|
82
|
+
mockHandlerRegistry.set(handlerId, {
|
|
83
|
+
handler,
|
|
84
|
+
isRegistered: false
|
|
85
|
+
});
|
|
86
|
+
if (typeof server[methodName] === "function") {
|
|
87
|
+
const mockHandler = async (c, next) => {
|
|
88
|
+
var _mockHandlerRegistry_get;
|
|
89
|
+
if (typeof enable === "function") {
|
|
90
|
+
const isEnabled = enable(c.env.node.req, c.env.node.res);
|
|
91
|
+
if (!isEnabled) {
|
|
92
|
+
return next();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const handler2 = (_mockHandlerRegistry_get = mockHandlerRegistry.get(handlerId)) === null || _mockHandlerRegistry_get === void 0 ? void 0 : _mockHandlerRegistry_get.handler;
|
|
96
|
+
if (typeof handler2 === "function") {
|
|
97
|
+
await (0, import_node.connectMid2HonoMid)(handler2)(c, next);
|
|
98
|
+
} else {
|
|
99
|
+
return c.json(handler2);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const handlerInfo = mockHandlerRegistry.get(handlerId);
|
|
103
|
+
if (handlerInfo && !(handlerInfo === null || handlerInfo === void 0 ? void 0 : handlerInfo.isRegistered)) {
|
|
104
|
+
server[methodName](path2, mockHandler);
|
|
105
|
+
handlerInfo.isRegistered = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
111
|
+
0 && (module.exports = {
|
|
112
|
+
registerMockHandlers
|
|
113
|
+
});
|
|
@@ -53,7 +53,11 @@ const cleanSSRCache = (distDir, routes) => {
|
|
|
53
53
|
const onRepack = (distDir, runner, routes) => {
|
|
54
54
|
cleanSSRCache(distDir, routes);
|
|
55
55
|
import_fileReader.fileReader.reset();
|
|
56
|
-
runner.
|
|
56
|
+
runner.reset({
|
|
57
|
+
event: {
|
|
58
|
+
type: "repack"
|
|
59
|
+
}
|
|
60
|
+
});
|
|
57
61
|
};
|
|
58
62
|
// Annotate the CommonJS export names for ESM import in node:
|
|
59
63
|
0 && (module.exports = {
|
|
@@ -3,122 +3,79 @@ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
|
3
3
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
4
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
5
|
import path from "node:path";
|
|
6
|
-
import { createServerBase } from "@modern-js/server-core
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import { createServerBase } from "@modern-js/server-core";
|
|
7
|
+
import { createNodeServer, loadServerConfig } from "@modern-js/server-core/node";
|
|
8
|
+
import { getDevOptions } from "./helpers";
|
|
9
|
+
import { devPlugin } from "./dev";
|
|
10
10
|
var createDevServer = function() {
|
|
11
|
-
var _ref = _async_to_generator(function(options,
|
|
12
|
-
var config, pwd,
|
|
11
|
+
var _ref = _async_to_generator(function(options, applyPlugins) {
|
|
12
|
+
var config, pwd, serverConfigFile, serverConfigPath, dev, distDir, serverConfig, prodServerOptions, server, devHttpsOption, nodeServer, genHttpsOptions, httpsOptions;
|
|
13
13
|
return _ts_generator(this, function(_state) {
|
|
14
14
|
switch (_state.label) {
|
|
15
15
|
case 0:
|
|
16
|
-
config = options.config, pwd = options.pwd,
|
|
16
|
+
config = options.config, pwd = options.pwd, serverConfigFile = options.serverConfigFile, serverConfigPath = options.serverConfigPath;
|
|
17
17
|
dev = getDevOptions(options);
|
|
18
18
|
distDir = path.resolve(pwd, config.output.path || "dist");
|
|
19
|
-
|
|
20
|
-
sharedDir = (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || SHARED_DIR;
|
|
19
|
+
serverConfig = loadServerConfig(distDir, serverConfigFile, serverConfigPath);
|
|
21
20
|
prodServerOptions = _object_spread_props(_object_spread({}, options), {
|
|
22
21
|
pwd: distDir
|
|
23
22
|
});
|
|
23
|
+
if (serverConfig) {
|
|
24
|
+
prodServerOptions.serverConfig = serverConfig;
|
|
25
|
+
}
|
|
24
26
|
server = createServerBase(prodServerOptions);
|
|
25
|
-
closeCb = [];
|
|
26
|
-
_ref2 = (getMiddlewares === null || getMiddlewares === void 0 ? void 0 : getMiddlewares()) || {}, rsbuildMiddlewares = _ref2.middlewares, close = _ref2.close, onHTTPUpgrade = _ref2.onHTTPUpgrade;
|
|
27
|
-
close && closeCb.push(close);
|
|
28
|
-
rsbuildMiddlewares && server.all("*", connectMid2HonoMid(rsbuildMiddlewares));
|
|
29
|
-
return [
|
|
30
|
-
4,
|
|
31
|
-
registerMockHandlers({
|
|
32
|
-
pwd,
|
|
33
|
-
server
|
|
34
|
-
})
|
|
35
|
-
];
|
|
36
|
-
case 1:
|
|
37
|
-
_state.sent();
|
|
38
|
-
server.use("*", initFileReader());
|
|
39
|
-
return [
|
|
40
|
-
4,
|
|
41
|
-
server.init()
|
|
42
|
-
];
|
|
43
|
-
case 2:
|
|
44
|
-
_state.sent();
|
|
45
27
|
devHttpsOption = typeof dev === "object" && dev.https;
|
|
46
28
|
if (!devHttpsOption)
|
|
47
29
|
return [
|
|
48
30
|
3,
|
|
49
|
-
|
|
31
|
+
4
|
|
50
32
|
];
|
|
51
33
|
return [
|
|
52
34
|
4,
|
|
53
35
|
import("./dev-tools/https")
|
|
54
36
|
];
|
|
55
|
-
case
|
|
37
|
+
case 1:
|
|
56
38
|
genHttpsOptions = _state.sent().genHttpsOptions;
|
|
57
39
|
return [
|
|
58
40
|
4,
|
|
59
41
|
genHttpsOptions(devHttpsOption, pwd)
|
|
60
42
|
];
|
|
61
|
-
case
|
|
43
|
+
case 2:
|
|
62
44
|
httpsOptions = _state.sent();
|
|
63
45
|
return [
|
|
64
46
|
4,
|
|
65
47
|
createNodeServer(server.handle.bind(server), httpsOptions)
|
|
66
48
|
];
|
|
67
|
-
case
|
|
49
|
+
case 3:
|
|
68
50
|
nodeServer = _state.sent();
|
|
69
51
|
return [
|
|
70
52
|
3,
|
|
71
|
-
|
|
53
|
+
6
|
|
72
54
|
];
|
|
73
|
-
case
|
|
55
|
+
case 4:
|
|
74
56
|
return [
|
|
75
57
|
4,
|
|
76
58
|
createNodeServer(server.handle.bind(server))
|
|
77
59
|
];
|
|
78
|
-
case
|
|
60
|
+
case 5:
|
|
79
61
|
nodeServer = _state.sent();
|
|
80
|
-
_state.label =
|
|
81
|
-
case
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
all: false
|
|
86
|
-
}).name !== "server") {
|
|
87
|
-
onRepack(distDir, server.runner, routes);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
onHTTPUpgrade && nodeServer.on("upgrade", onHTTPUpgrade);
|
|
62
|
+
_state.label = 6;
|
|
63
|
+
case 6:
|
|
64
|
+
server.addPlugins([
|
|
65
|
+
devPlugin(options)
|
|
66
|
+
]);
|
|
91
67
|
return [
|
|
92
68
|
4,
|
|
93
|
-
server
|
|
94
|
-
app: nodeServer
|
|
95
|
-
})
|
|
69
|
+
applyPlugins(server, prodServerOptions, nodeServer)
|
|
96
70
|
];
|
|
97
|
-
case
|
|
71
|
+
case 7:
|
|
98
72
|
_state.sent();
|
|
99
73
|
return [
|
|
100
74
|
4,
|
|
101
|
-
|
|
75
|
+
server.init()
|
|
102
76
|
];
|
|
103
|
-
case
|
|
77
|
+
case 8:
|
|
104
78
|
_state.sent();
|
|
105
|
-
if (dev.watch) {
|
|
106
|
-
watchOptions = config.server.watchOptions;
|
|
107
|
-
watcher = startWatcher({
|
|
108
|
-
pwd,
|
|
109
|
-
distDir,
|
|
110
|
-
apiDir,
|
|
111
|
-
sharedDir,
|
|
112
|
-
watchOptions,
|
|
113
|
-
server
|
|
114
|
-
});
|
|
115
|
-
closeCb.push(watcher.close.bind(watcher));
|
|
116
|
-
}
|
|
117
|
-
closeCb.length > 0 && nodeServer.on("close", function() {
|
|
118
|
-
closeCb.forEach(function(cb) {
|
|
119
|
-
cb();
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
79
|
return [
|
|
123
80
|
2,
|
|
124
81
|
nodeServer
|
|
@@ -126,7 +83,7 @@ var createDevServer = function() {
|
|
|
126
83
|
}
|
|
127
84
|
});
|
|
128
85
|
});
|
|
129
|
-
return function createDevServer2(options,
|
|
86
|
+
return function createDevServer2(options, applyPlugins) {
|
|
130
87
|
return _ref.apply(this, arguments);
|
|
131
88
|
};
|
|
132
89
|
}();
|
package/dist/esm/dev.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
import { connectMid2HonoMid } from "@modern-js/server-core/node";
|
|
4
|
+
import { API_DIR, SHARED_DIR } from "@modern-js/utils";
|
|
5
|
+
import { startWatcher, onRepack, getDevOptions, initFileReader, registerMockHandlers } from "./helpers";
|
|
6
|
+
var devPlugin = function(options) {
|
|
7
|
+
return {
|
|
8
|
+
name: "@modern-js/plugin-dev",
|
|
9
|
+
setup: function setup(api) {
|
|
10
|
+
var getMiddlewares = options.getMiddlewares, rsbuild = options.rsbuild, config = options.config, pwd = options.pwd;
|
|
11
|
+
var closeCb = [];
|
|
12
|
+
var _ref = (getMiddlewares === null || getMiddlewares === void 0 ? void 0 : getMiddlewares()) || {}, rsbuildMiddlewares = _ref.middlewares, close = _ref.close, onHTTPUpgrade = _ref.onHTTPUpgrade;
|
|
13
|
+
close && closeCb.push(close);
|
|
14
|
+
var dev = getDevOptions(options);
|
|
15
|
+
return {
|
|
16
|
+
prepare: function prepare() {
|
|
17
|
+
return _async_to_generator(function() {
|
|
18
|
+
var _api_useAppContext, middlewares, distDirectory, routes, nodeServer, apiDirectory, sharedDirectory, serverBase, runner, watchOptions, watcher;
|
|
19
|
+
return _ts_generator(this, function(_state) {
|
|
20
|
+
switch (_state.label) {
|
|
21
|
+
case 0:
|
|
22
|
+
_api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, distDirectory = _api_useAppContext.distDirectory, routes = _api_useAppContext.routes, nodeServer = _api_useAppContext.nodeServer, apiDirectory = _api_useAppContext.apiDirectory, sharedDirectory = _api_useAppContext.sharedDirectory, serverBase = _api_useAppContext.serverBase;
|
|
23
|
+
onHTTPUpgrade && (nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.on("upgrade", onHTTPUpgrade));
|
|
24
|
+
runner = api.useHookRunners();
|
|
25
|
+
rsbuild === null || rsbuild === void 0 ? void 0 : rsbuild.onDevCompileDone(function(param) {
|
|
26
|
+
var stats = param.stats;
|
|
27
|
+
if (stats.toJson({
|
|
28
|
+
all: false
|
|
29
|
+
}).name !== "server") {
|
|
30
|
+
onRepack(distDirectory, runner, routes || []);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
if (dev.watch) {
|
|
34
|
+
watchOptions = config.server.watchOptions;
|
|
35
|
+
watcher = startWatcher({
|
|
36
|
+
pwd,
|
|
37
|
+
distDir: distDirectory,
|
|
38
|
+
apiDir: apiDirectory || API_DIR,
|
|
39
|
+
sharedDir: sharedDirectory || SHARED_DIR,
|
|
40
|
+
watchOptions,
|
|
41
|
+
server: serverBase
|
|
42
|
+
});
|
|
43
|
+
closeCb.push(watcher.close.bind(watcher));
|
|
44
|
+
}
|
|
45
|
+
closeCb.length > 0 && (nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.on("close", function() {
|
|
46
|
+
closeCb.forEach(function(cb) {
|
|
47
|
+
cb();
|
|
48
|
+
});
|
|
49
|
+
}));
|
|
50
|
+
if (rsbuildMiddlewares) {
|
|
51
|
+
middlewares.push({
|
|
52
|
+
name: "rsbuild-dev",
|
|
53
|
+
handler: connectMid2HonoMid(rsbuildMiddlewares)
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return [
|
|
57
|
+
4,
|
|
58
|
+
registerMockHandlers({
|
|
59
|
+
pwd,
|
|
60
|
+
server: serverBase
|
|
61
|
+
})
|
|
62
|
+
];
|
|
63
|
+
case 1:
|
|
64
|
+
_state.sent();
|
|
65
|
+
middlewares.push({
|
|
66
|
+
name: "init-file-reader",
|
|
67
|
+
handler: initFileReader()
|
|
68
|
+
});
|
|
69
|
+
return [
|
|
70
|
+
2
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
})();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
export {
|
|
81
|
+
devPlugin
|
|
82
|
+
};
|
|
@@ -2,19 +2,20 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { SERVER_BUNDLE_DIRECTORY, SERVER_DIR, logger } from "@modern-js/utils";
|
|
5
|
-
import { AGGRED_DIR } from "@modern-js/server-core
|
|
6
|
-
import { registerMockHandlers } from "@modern-js/server-core/base/node";
|
|
5
|
+
import { AGGRED_DIR } from "@modern-js/server-core";
|
|
7
6
|
import Watcher, { mergeWatchOptions } from "../dev-tools/watcher";
|
|
8
7
|
import { debug } from "./utils";
|
|
8
|
+
import { registerMockHandlers } from "./mock";
|
|
9
9
|
export * from "./repack";
|
|
10
10
|
export * from "./devOptions";
|
|
11
11
|
export * from "./fileReader";
|
|
12
|
+
export * from "./mock";
|
|
12
13
|
function onServerChange(_) {
|
|
13
14
|
return _onServerChange.apply(this, arguments);
|
|
14
15
|
}
|
|
15
16
|
function _onServerChange() {
|
|
16
17
|
_onServerChange = _async_to_generator(function(param) {
|
|
17
|
-
var pwd, filepath, event, server, mock, mockPath, runner, e;
|
|
18
|
+
var pwd, filepath, event, server, mock, mockPath, runner, fileChangeEvent, e;
|
|
18
19
|
return _ts_generator(this, function(_state) {
|
|
19
20
|
switch (_state.label) {
|
|
20
21
|
case 0:
|
|
@@ -22,7 +23,6 @@ function _onServerChange() {
|
|
|
22
23
|
mock = AGGRED_DIR.mock;
|
|
23
24
|
mockPath = path.normalize(path.join(pwd, mock));
|
|
24
25
|
runner = server.runner;
|
|
25
|
-
runner.reset();
|
|
26
26
|
if (!filepath.startsWith(mockPath))
|
|
27
27
|
return [
|
|
28
28
|
3,
|
|
@@ -49,14 +49,20 @@ function _onServerChange() {
|
|
|
49
49
|
,
|
|
50
50
|
5
|
|
51
51
|
]);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
fileChangeEvent = {
|
|
53
|
+
type: "file-change",
|
|
54
|
+
payload: [
|
|
55
55
|
{
|
|
56
56
|
filename: filepath,
|
|
57
57
|
event
|
|
58
58
|
}
|
|
59
|
-
]
|
|
59
|
+
]
|
|
60
|
+
};
|
|
61
|
+
return [
|
|
62
|
+
4,
|
|
63
|
+
runner.reset({
|
|
64
|
+
event: fileChangeEvent
|
|
65
|
+
})
|
|
60
66
|
];
|
|
61
67
|
case 3:
|
|
62
68
|
_state.sent();
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fs } from "@modern-js/utils";
|
|
6
|
+
import { AGGRED_DIR } from "@modern-js/server-core";
|
|
7
|
+
import { connectMid2HonoMid } from "@modern-js/server-core/node";
|
|
8
|
+
var parseKey = function(key) {
|
|
9
|
+
var _blank = " ";
|
|
10
|
+
var splitted = key.split(_blank).filter(Boolean);
|
|
11
|
+
if (splitted.length > 1) {
|
|
12
|
+
var _splitted = _sliced_to_array(splitted, 2), method = _splitted[0], pathname = _splitted[1];
|
|
13
|
+
return {
|
|
14
|
+
method: method.toLowerCase(),
|
|
15
|
+
path: pathname
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
method: "get",
|
|
20
|
+
path: key
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
var mockHandlerRegistry = /* @__PURE__ */ new Map();
|
|
24
|
+
var registerMockHandlers = function() {
|
|
25
|
+
var _ref = _async_to_generator(function(param) {
|
|
26
|
+
var pwd, server, exts, mockFilePath, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, ext, maybeMatch, err, _ref2, mockModule, config, enable;
|
|
27
|
+
return _ts_generator(this, function(_state) {
|
|
28
|
+
switch (_state.label) {
|
|
29
|
+
case 0:
|
|
30
|
+
pwd = param.pwd, server = param.server;
|
|
31
|
+
exts = [
|
|
32
|
+
".ts",
|
|
33
|
+
".js"
|
|
34
|
+
];
|
|
35
|
+
mockFilePath = "";
|
|
36
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
37
|
+
_state.label = 1;
|
|
38
|
+
case 1:
|
|
39
|
+
_state.trys.push([
|
|
40
|
+
1,
|
|
41
|
+
6,
|
|
42
|
+
7,
|
|
43
|
+
8
|
|
44
|
+
]);
|
|
45
|
+
_iterator = exts[Symbol.iterator]();
|
|
46
|
+
_state.label = 2;
|
|
47
|
+
case 2:
|
|
48
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
|
|
49
|
+
return [
|
|
50
|
+
3,
|
|
51
|
+
5
|
|
52
|
+
];
|
|
53
|
+
ext = _step.value;
|
|
54
|
+
maybeMatch = path.join(pwd, "".concat(AGGRED_DIR.mock, "/index").concat(ext));
|
|
55
|
+
return [
|
|
56
|
+
4,
|
|
57
|
+
fs.pathExists(maybeMatch)
|
|
58
|
+
];
|
|
59
|
+
case 3:
|
|
60
|
+
if (_state.sent()) {
|
|
61
|
+
mockFilePath = maybeMatch;
|
|
62
|
+
return [
|
|
63
|
+
3,
|
|
64
|
+
5
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
_state.label = 4;
|
|
68
|
+
case 4:
|
|
69
|
+
_iteratorNormalCompletion = true;
|
|
70
|
+
return [
|
|
71
|
+
3,
|
|
72
|
+
2
|
|
73
|
+
];
|
|
74
|
+
case 5:
|
|
75
|
+
return [
|
|
76
|
+
3,
|
|
77
|
+
8
|
|
78
|
+
];
|
|
79
|
+
case 6:
|
|
80
|
+
err = _state.sent();
|
|
81
|
+
_didIteratorError = true;
|
|
82
|
+
_iteratorError = err;
|
|
83
|
+
return [
|
|
84
|
+
3,
|
|
85
|
+
8
|
|
86
|
+
];
|
|
87
|
+
case 7:
|
|
88
|
+
try {
|
|
89
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
90
|
+
_iterator.return();
|
|
91
|
+
}
|
|
92
|
+
} finally {
|
|
93
|
+
if (_didIteratorError) {
|
|
94
|
+
throw _iteratorError;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return [
|
|
98
|
+
7
|
|
99
|
+
];
|
|
100
|
+
case 8:
|
|
101
|
+
if (!mockFilePath) {
|
|
102
|
+
return [
|
|
103
|
+
2,
|
|
104
|
+
null
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
return [
|
|
108
|
+
4,
|
|
109
|
+
import(mockFilePath)
|
|
110
|
+
];
|
|
111
|
+
case 9:
|
|
112
|
+
_ref2 = _state.sent(), mockModule = _ref2.default, config = _ref2.config;
|
|
113
|
+
enable = config === null || config === void 0 ? void 0 : config.enable;
|
|
114
|
+
if (enable === false) {
|
|
115
|
+
return [
|
|
116
|
+
2
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
if (!mockModule) {
|
|
120
|
+
throw new Error("Mock file ".concat(mockFilePath, " parsed failed!"));
|
|
121
|
+
}
|
|
122
|
+
Object.entries(mockModule).forEach(function(param2) {
|
|
123
|
+
var _param = _sliced_to_array(param2, 2), key = _param[0], handler = _param[1];
|
|
124
|
+
var _parseKey = parseKey(key), method = _parseKey.method, _$path = _parseKey.path;
|
|
125
|
+
var methodName = method.toLowerCase();
|
|
126
|
+
var handlerId = "".concat(methodName, "-").concat(_$path);
|
|
127
|
+
mockHandlerRegistry.set(handlerId, {
|
|
128
|
+
handler,
|
|
129
|
+
isRegistered: false
|
|
130
|
+
});
|
|
131
|
+
if (typeof server[methodName] === "function") {
|
|
132
|
+
var mockHandler = function() {
|
|
133
|
+
var _ref3 = _async_to_generator(function(c, next) {
|
|
134
|
+
var _mockHandlerRegistry_get, isEnabled, handler2;
|
|
135
|
+
return _ts_generator(this, function(_state2) {
|
|
136
|
+
switch (_state2.label) {
|
|
137
|
+
case 0:
|
|
138
|
+
if (typeof enable === "function") {
|
|
139
|
+
isEnabled = enable(c.env.node.req, c.env.node.res);
|
|
140
|
+
if (!isEnabled) {
|
|
141
|
+
return [
|
|
142
|
+
2,
|
|
143
|
+
next()
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
handler2 = (_mockHandlerRegistry_get = mockHandlerRegistry.get(handlerId)) === null || _mockHandlerRegistry_get === void 0 ? void 0 : _mockHandlerRegistry_get.handler;
|
|
148
|
+
if (!(typeof handler2 === "function"))
|
|
149
|
+
return [
|
|
150
|
+
3,
|
|
151
|
+
2
|
|
152
|
+
];
|
|
153
|
+
return [
|
|
154
|
+
4,
|
|
155
|
+
connectMid2HonoMid(handler2)(c, next)
|
|
156
|
+
];
|
|
157
|
+
case 1:
|
|
158
|
+
_state2.sent();
|
|
159
|
+
return [
|
|
160
|
+
3,
|
|
161
|
+
3
|
|
162
|
+
];
|
|
163
|
+
case 2:
|
|
164
|
+
return [
|
|
165
|
+
2,
|
|
166
|
+
c.json(handler2)
|
|
167
|
+
];
|
|
168
|
+
case 3:
|
|
169
|
+
return [
|
|
170
|
+
2
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
return function mockHandler2(c, next) {
|
|
176
|
+
return _ref3.apply(this, arguments);
|
|
177
|
+
};
|
|
178
|
+
}();
|
|
179
|
+
var handlerInfo = mockHandlerRegistry.get(handlerId);
|
|
180
|
+
if (handlerInfo && !(handlerInfo === null || handlerInfo === void 0 ? void 0 : handlerInfo.isRegistered)) {
|
|
181
|
+
server[methodName](_$path, mockHandler);
|
|
182
|
+
handlerInfo.isRegistered = true;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
return [
|
|
187
|
+
2
|
|
188
|
+
];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
return function registerMockHandlers2(_) {
|
|
193
|
+
return _ref.apply(this, arguments);
|
|
194
|
+
};
|
|
195
|
+
}();
|
|
196
|
+
export {
|
|
197
|
+
registerMockHandlers
|
|
198
|
+
};
|
|
@@ -24,7 +24,11 @@ var cleanSSRCache = function(distDir, routes) {
|
|
|
24
24
|
var onRepack = function(distDir, runner, routes) {
|
|
25
25
|
cleanSSRCache(distDir, routes);
|
|
26
26
|
fileReader.reset();
|
|
27
|
-
runner.
|
|
27
|
+
runner.reset({
|
|
28
|
+
event: {
|
|
29
|
+
type: "repack"
|
|
30
|
+
}
|
|
31
|
+
});
|
|
28
32
|
};
|
|
29
33
|
export {
|
|
30
34
|
onRepack
|
|
@@ -1,29 +1,21 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import { createServerBase } from "@modern-js/server-core
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
const createDevServer = async (options,
|
|
7
|
-
const { config, pwd,
|
|
2
|
+
import { createServerBase } from "@modern-js/server-core";
|
|
3
|
+
import { createNodeServer, loadServerConfig } from "@modern-js/server-core/node";
|
|
4
|
+
import { getDevOptions } from "./helpers";
|
|
5
|
+
import { devPlugin } from "./dev";
|
|
6
|
+
const createDevServer = async (options, applyPlugins) => {
|
|
7
|
+
const { config, pwd, serverConfigFile, serverConfigPath } = options;
|
|
8
8
|
const dev = getDevOptions(options);
|
|
9
9
|
const distDir = path.resolve(pwd, config.output.path || "dist");
|
|
10
|
-
const
|
|
11
|
-
const sharedDir = (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || SHARED_DIR;
|
|
10
|
+
const serverConfig = loadServerConfig(distDir, serverConfigFile, serverConfigPath);
|
|
12
11
|
const prodServerOptions = {
|
|
13
12
|
...options,
|
|
14
13
|
pwd: distDir
|
|
15
14
|
};
|
|
15
|
+
if (serverConfig) {
|
|
16
|
+
prodServerOptions.serverConfig = serverConfig;
|
|
17
|
+
}
|
|
16
18
|
const server = createServerBase(prodServerOptions);
|
|
17
|
-
const closeCb = [];
|
|
18
|
-
const { middlewares: rsbuildMiddlewares, close, onHTTPUpgrade } = (getMiddlewares === null || getMiddlewares === void 0 ? void 0 : getMiddlewares()) || {};
|
|
19
|
-
close && closeCb.push(close);
|
|
20
|
-
rsbuildMiddlewares && server.all("*", connectMid2HonoMid(rsbuildMiddlewares));
|
|
21
|
-
await registerMockHandlers({
|
|
22
|
-
pwd,
|
|
23
|
-
server
|
|
24
|
-
});
|
|
25
|
-
server.use("*", initFileReader());
|
|
26
|
-
await server.init();
|
|
27
19
|
const devHttpsOption = typeof dev === "object" && dev.https;
|
|
28
20
|
let nodeServer;
|
|
29
21
|
if (devHttpsOption) {
|
|
@@ -33,35 +25,11 @@ const createDevServer = async (options, initProdMiddlewares) => {
|
|
|
33
25
|
} else {
|
|
34
26
|
nodeServer = await createNodeServer(server.handle.bind(server));
|
|
35
27
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
onHTTPUpgrade && nodeServer.on("upgrade", onHTTPUpgrade);
|
|
44
|
-
await server.runner.beforeServerInit({
|
|
45
|
-
app: nodeServer
|
|
46
|
-
});
|
|
47
|
-
await initProdMiddlewares(server, prodServerOptions);
|
|
48
|
-
if (dev.watch) {
|
|
49
|
-
const { watchOptions } = config.server;
|
|
50
|
-
const watcher = startWatcher({
|
|
51
|
-
pwd,
|
|
52
|
-
distDir,
|
|
53
|
-
apiDir,
|
|
54
|
-
sharedDir,
|
|
55
|
-
watchOptions,
|
|
56
|
-
server
|
|
57
|
-
});
|
|
58
|
-
closeCb.push(watcher.close.bind(watcher));
|
|
59
|
-
}
|
|
60
|
-
closeCb.length > 0 && nodeServer.on("close", () => {
|
|
61
|
-
closeCb.forEach((cb) => {
|
|
62
|
-
cb();
|
|
63
|
-
});
|
|
64
|
-
});
|
|
28
|
+
server.addPlugins([
|
|
29
|
+
devPlugin(options)
|
|
30
|
+
]);
|
|
31
|
+
await applyPlugins(server, prodServerOptions, nodeServer);
|
|
32
|
+
await server.init();
|
|
65
33
|
return nodeServer;
|
|
66
34
|
};
|
|
67
35
|
export {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { connectMid2HonoMid } from "@modern-js/server-core/node";
|
|
2
|
+
import { API_DIR, SHARED_DIR } from "@modern-js/utils";
|
|
3
|
+
import { startWatcher, onRepack, getDevOptions, initFileReader, registerMockHandlers } from "./helpers";
|
|
4
|
+
const devPlugin = (options) => ({
|
|
5
|
+
name: "@modern-js/plugin-dev",
|
|
6
|
+
setup(api) {
|
|
7
|
+
const { getMiddlewares, rsbuild, config, pwd } = options;
|
|
8
|
+
const closeCb = [];
|
|
9
|
+
const { middlewares: rsbuildMiddlewares, close, onHTTPUpgrade } = (getMiddlewares === null || getMiddlewares === void 0 ? void 0 : getMiddlewares()) || {};
|
|
10
|
+
close && closeCb.push(close);
|
|
11
|
+
const dev = getDevOptions(options);
|
|
12
|
+
return {
|
|
13
|
+
async prepare() {
|
|
14
|
+
const { middlewares, distDirectory, routes, nodeServer, apiDirectory, sharedDirectory, serverBase } = api.useAppContext();
|
|
15
|
+
onHTTPUpgrade && (nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.on("upgrade", onHTTPUpgrade));
|
|
16
|
+
const runner = api.useHookRunners();
|
|
17
|
+
rsbuild === null || rsbuild === void 0 ? void 0 : rsbuild.onDevCompileDone(({ stats }) => {
|
|
18
|
+
if (stats.toJson({
|
|
19
|
+
all: false
|
|
20
|
+
}).name !== "server") {
|
|
21
|
+
onRepack(distDirectory, runner, routes || []);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
if (dev.watch) {
|
|
25
|
+
const { watchOptions } = config.server;
|
|
26
|
+
const watcher = startWatcher({
|
|
27
|
+
pwd,
|
|
28
|
+
distDir: distDirectory,
|
|
29
|
+
apiDir: apiDirectory || API_DIR,
|
|
30
|
+
sharedDir: sharedDirectory || SHARED_DIR,
|
|
31
|
+
watchOptions,
|
|
32
|
+
server: serverBase
|
|
33
|
+
});
|
|
34
|
+
closeCb.push(watcher.close.bind(watcher));
|
|
35
|
+
}
|
|
36
|
+
closeCb.length > 0 && (nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.on("close", () => {
|
|
37
|
+
closeCb.forEach((cb) => {
|
|
38
|
+
cb();
|
|
39
|
+
});
|
|
40
|
+
}));
|
|
41
|
+
if (rsbuildMiddlewares) {
|
|
42
|
+
middlewares.push({
|
|
43
|
+
name: "rsbuild-dev",
|
|
44
|
+
handler: connectMid2HonoMid(rsbuildMiddlewares)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
await registerMockHandlers({
|
|
48
|
+
pwd,
|
|
49
|
+
server: serverBase
|
|
50
|
+
});
|
|
51
|
+
middlewares.push({
|
|
52
|
+
name: "init-file-reader",
|
|
53
|
+
handler: initFileReader()
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
export {
|
|
60
|
+
devPlugin
|
|
61
|
+
};
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { SERVER_BUNDLE_DIRECTORY, SERVER_DIR, logger } from "@modern-js/utils";
|
|
3
|
-
import { AGGRED_DIR } from "@modern-js/server-core
|
|
4
|
-
import { registerMockHandlers } from "@modern-js/server-core/base/node";
|
|
3
|
+
import { AGGRED_DIR } from "@modern-js/server-core";
|
|
5
4
|
import Watcher, { mergeWatchOptions } from "../dev-tools/watcher";
|
|
6
5
|
import { debug } from "./utils";
|
|
6
|
+
import { registerMockHandlers } from "./mock";
|
|
7
7
|
export * from "./repack";
|
|
8
8
|
export * from "./devOptions";
|
|
9
9
|
export * from "./fileReader";
|
|
10
|
+
export * from "./mock";
|
|
10
11
|
async function onServerChange({ pwd, filepath, event, server }) {
|
|
11
12
|
const { mock } = AGGRED_DIR;
|
|
12
13
|
const mockPath = path.normalize(path.join(pwd, mock));
|
|
13
14
|
const { runner } = server;
|
|
14
|
-
runner.reset();
|
|
15
15
|
if (filepath.startsWith(mockPath)) {
|
|
16
16
|
await registerMockHandlers({
|
|
17
17
|
pwd,
|
|
@@ -20,12 +20,18 @@ async function onServerChange({ pwd, filepath, event, server }) {
|
|
|
20
20
|
logger.info("Finish registering the mock handlers");
|
|
21
21
|
} else {
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
const fileChangeEvent = {
|
|
24
|
+
type: "file-change",
|
|
25
|
+
payload: [
|
|
26
|
+
{
|
|
27
|
+
filename: filepath,
|
|
28
|
+
event
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
await runner.reset({
|
|
33
|
+
event: fileChangeEvent
|
|
34
|
+
});
|
|
29
35
|
debug(`Finish reload server, trigger by ${filepath} ${event}`);
|
|
30
36
|
} catch (e) {
|
|
31
37
|
logger.error(e);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { AGGRED_DIR } from "@modern-js/server-core";
|
|
4
|
+
import { connectMid2HonoMid } from "@modern-js/server-core/node";
|
|
5
|
+
const parseKey = (key) => {
|
|
6
|
+
const _blank = " ";
|
|
7
|
+
const splitted = key.split(_blank).filter(Boolean);
|
|
8
|
+
if (splitted.length > 1) {
|
|
9
|
+
const [method, pathname] = splitted;
|
|
10
|
+
return {
|
|
11
|
+
method: method.toLowerCase(),
|
|
12
|
+
path: pathname
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
method: "get",
|
|
17
|
+
path: key
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
const mockHandlerRegistry = /* @__PURE__ */ new Map();
|
|
21
|
+
const registerMockHandlers = async ({ pwd, server }) => {
|
|
22
|
+
const exts = [
|
|
23
|
+
".ts",
|
|
24
|
+
".js"
|
|
25
|
+
];
|
|
26
|
+
let mockFilePath = "";
|
|
27
|
+
for (const ext of exts) {
|
|
28
|
+
const maybeMatch = path.join(pwd, `${AGGRED_DIR.mock}/index${ext}`);
|
|
29
|
+
if (await fs.pathExists(maybeMatch)) {
|
|
30
|
+
mockFilePath = maybeMatch;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!mockFilePath) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const { default: mockModule, config } = await import(mockFilePath);
|
|
38
|
+
const enable = config === null || config === void 0 ? void 0 : config.enable;
|
|
39
|
+
if (enable === false) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!mockModule) {
|
|
43
|
+
throw new Error(`Mock file ${mockFilePath} parsed failed!`);
|
|
44
|
+
}
|
|
45
|
+
Object.entries(mockModule).forEach(([key, handler]) => {
|
|
46
|
+
const { method, path: path2 } = parseKey(key);
|
|
47
|
+
const methodName = method.toLowerCase();
|
|
48
|
+
const handlerId = `${methodName}-${path2}`;
|
|
49
|
+
mockHandlerRegistry.set(handlerId, {
|
|
50
|
+
handler,
|
|
51
|
+
isRegistered: false
|
|
52
|
+
});
|
|
53
|
+
if (typeof server[methodName] === "function") {
|
|
54
|
+
const mockHandler = async (c, next) => {
|
|
55
|
+
var _mockHandlerRegistry_get;
|
|
56
|
+
if (typeof enable === "function") {
|
|
57
|
+
const isEnabled = enable(c.env.node.req, c.env.node.res);
|
|
58
|
+
if (!isEnabled) {
|
|
59
|
+
return next();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const handler2 = (_mockHandlerRegistry_get = mockHandlerRegistry.get(handlerId)) === null || _mockHandlerRegistry_get === void 0 ? void 0 : _mockHandlerRegistry_get.handler;
|
|
63
|
+
if (typeof handler2 === "function") {
|
|
64
|
+
await connectMid2HonoMid(handler2)(c, next);
|
|
65
|
+
} else {
|
|
66
|
+
return c.json(handler2);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const handlerInfo = mockHandlerRegistry.get(handlerId);
|
|
70
|
+
if (handlerInfo && !(handlerInfo === null || handlerInfo === void 0 ? void 0 : handlerInfo.isRegistered)) {
|
|
71
|
+
server[methodName](path2, mockHandler);
|
|
72
|
+
handlerInfo.isRegistered = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
registerMockHandlers
|
|
79
|
+
};
|
|
@@ -20,7 +20,11 @@ const cleanSSRCache = (distDir, routes) => {
|
|
|
20
20
|
const onRepack = (distDir, runner, routes) => {
|
|
21
21
|
cleanSSRCache(distDir, routes);
|
|
22
22
|
fileReader.reset();
|
|
23
|
-
runner.
|
|
23
|
+
runner.reset({
|
|
24
|
+
event: {
|
|
25
|
+
type: "repack"
|
|
26
|
+
}
|
|
27
|
+
});
|
|
24
28
|
};
|
|
25
29
|
export {
|
|
26
30
|
onRepack
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Server as NodeServer } from 'node:http';
|
|
3
|
-
import { ServerBaseOptions } from '@modern-js/server-core
|
|
4
|
-
import {
|
|
5
|
-
export type { ModernDevServerOptions
|
|
6
|
-
export declare const createDevServer: <O extends ServerBaseOptions>(options: ModernDevServerOptions<O>,
|
|
3
|
+
import { ServerBaseOptions } from '@modern-js/server-core';
|
|
4
|
+
import { ApplyPlugins, ModernDevServerOptions } from './types';
|
|
5
|
+
export type { ModernDevServerOptions } from './types';
|
|
6
|
+
export declare const createDevServer: <O extends ServerBaseOptions>(options: ModernDevServerOptions<O>, applyPlugins: ApplyPlugins<O>) => Promise<NodeServer>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Middleware } from '@modern-js/server-core
|
|
1
|
+
import { Middleware } from '@modern-js/server-core';
|
|
2
2
|
export declare const initFileReader: () => Middleware;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { WatchOptions } from '@modern-js/utils';
|
|
2
|
-
import { ServerBase } from '@modern-js/server-core
|
|
2
|
+
import { ServerBase } from '@modern-js/server-core';
|
|
3
3
|
import Watcher from '../dev-tools/watcher';
|
|
4
4
|
export * from './repack';
|
|
5
5
|
export * from './devOptions';
|
|
6
6
|
export * from './fileReader';
|
|
7
|
+
export * from './mock';
|
|
7
8
|
export declare function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, server, }: {
|
|
8
9
|
pwd: string;
|
|
9
10
|
distDir: string;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { createDevServer } from './createDevServer';
|
|
2
|
-
export type { ModernDevServerOptions
|
|
2
|
+
export type { ModernDevServerOptions } from './createDevServer';
|
package/dist/types/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { IncomingMessage, ServerResponse } from 'http';
|
|
|
4
4
|
import type { Socket } from 'net';
|
|
5
5
|
import type { DevServerOptions, DevServerHttpsOptions, NextFunction } from '@modern-js/types';
|
|
6
6
|
import type { RsbuildInstance } from '@rsbuild/core';
|
|
7
|
-
import { ServerBase, ServerBaseOptions } from '@modern-js/server-core
|
|
7
|
+
import { NodeServer, ServerBase, ServerBaseOptions } from '@modern-js/server-core';
|
|
8
8
|
export type { DevServerOptions, DevServerHttpsOptions };
|
|
9
9
|
type Middleware = (req: IncomingMessage, res: ServerResponse, next: NextFunction) => Promise<void>;
|
|
10
10
|
export type DevMiddlewareAPI = Middleware & {
|
|
@@ -37,6 +37,9 @@ export type ExtraOptions = {
|
|
|
37
37
|
dev: Pick<DevServerOptions, 'watch' | 'https'> & {
|
|
38
38
|
writeToDisk?: boolean | ((filename: string) => boolean);
|
|
39
39
|
};
|
|
40
|
+
/** compat, the default value is modern.server-runtime.config.ts */
|
|
41
|
+
serverConfigFile?: string;
|
|
42
|
+
serverConfigPath?: string;
|
|
40
43
|
useSSRWorker?: boolean;
|
|
41
44
|
rsbuild: RsbuildInstance;
|
|
42
45
|
getMiddlewares?: () => {
|
|
@@ -46,4 +49,4 @@ export type ExtraOptions = {
|
|
|
46
49
|
};
|
|
47
50
|
};
|
|
48
51
|
export type ModernDevServerOptions<O extends ServerBaseOptions = ServerBaseOptions> = O & ExtraOptions;
|
|
49
|
-
export type
|
|
52
|
+
export type ApplyPlugins<O extends ServerBaseOptions = ServerBaseOptions> = (server: ServerBase, options: O, nodeServer?: NodeServer) => Promise<void>;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.
|
|
18
|
+
"version": "2.54.1",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -48,14 +48,14 @@
|
|
|
48
48
|
"minimatch": "^3.0.4",
|
|
49
49
|
"path-to-regexp": "^6.2.0",
|
|
50
50
|
"ws": "^8.13.0",
|
|
51
|
-
"@modern-js/runtime-utils": "2.
|
|
52
|
-
"@modern-js/utils": "2.
|
|
53
|
-
"@modern-js/
|
|
54
|
-
"@modern-js/server-core": "2.
|
|
55
|
-
"@modern-js/
|
|
51
|
+
"@modern-js/runtime-utils": "2.54.1",
|
|
52
|
+
"@modern-js/server-utils": "2.54.1",
|
|
53
|
+
"@modern-js/utils": "2.54.1",
|
|
54
|
+
"@modern-js/server-core": "2.54.1",
|
|
55
|
+
"@modern-js/types": "2.54.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@rsbuild/core": "0.7.
|
|
58
|
+
"@rsbuild/core": "0.7.9",
|
|
59
59
|
"@types/connect-history-api-fallback": "^1.3.5",
|
|
60
60
|
"@types/jest": "^29",
|
|
61
61
|
"@types/minimatch": "^3.0.5",
|
|
@@ -66,10 +66,10 @@
|
|
|
66
66
|
"ts-node": "^10.9.1",
|
|
67
67
|
"tsconfig-paths": "4.1.1",
|
|
68
68
|
"typescript": "^5",
|
|
69
|
-
"webpack": "^5.
|
|
69
|
+
"webpack": "^5.92.0",
|
|
70
70
|
"websocket": "^1",
|
|
71
|
-
"@scripts/
|
|
72
|
-
"@scripts/
|
|
71
|
+
"@scripts/jest-config": "2.54.1",
|
|
72
|
+
"@scripts/build": "2.54.1"
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
75
|
"devcert": "^1.2.2",
|