@xfe-repo/web-app 2.0.0-beta.1 → 2.0.0-beta.3
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/README.md +60 -2
- package/deploy/.drone.yml +7 -0
- package/deploy/Dockerfile +3 -2
- package/dist/cli.js +486 -187
- package/dist/cli.mjs +475 -175
- package/dist/config.d.mts +11 -1
- package/dist/config.d.ts +11 -1
- package/dist/config.js +68 -10
- package/dist/config.mjs +59 -4
- package/dist/framework.js +88 -44
- package/dist/framework.mjs +47 -3
- package/dist/node-dev.d.mts +2 -0
- package/dist/node-dev.d.ts +2 -0
- package/dist/node-dev.js +356 -0
- package/dist/node-dev.mjs +331 -0
- package/dist/node.d.mts +27 -0
- package/dist/node.d.ts +27 -0
- package/dist/node.js +379 -0
- package/dist/node.mjs +342 -0
- package/dist/react-router-config.js +50 -7
- package/dist/react-router-config.mjs +46 -5
- package/dist/vite.d.mts +1 -0
- package/dist/vite.d.ts +1 -0
- package/dist/vite.js +55 -10
- package/dist/vite.mjs +46 -1
- package/package.json +6 -2
package/dist/node.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
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
|
+
|
|
30
|
+
// src/node.ts
|
|
31
|
+
var node_exports = {};
|
|
32
|
+
__export(node_exports, {
|
|
33
|
+
buildNode: () => buildNode,
|
|
34
|
+
runNode: () => runNode
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(node_exports);
|
|
37
|
+
var import_node_events = require("events");
|
|
38
|
+
var import_promises = require("fs/promises");
|
|
39
|
+
var import_node_http = require("http");
|
|
40
|
+
var import_node_path3 = __toESM(require("path"));
|
|
41
|
+
var import_node_url3 = require("url");
|
|
42
|
+
var import_express = __toESM(require("express"));
|
|
43
|
+
|
|
44
|
+
// src/config.ts
|
|
45
|
+
var import_node_fs = __toESM(require("fs"));
|
|
46
|
+
var import_node_path = __toESM(require("path"));
|
|
47
|
+
var import_node_url = require("url");
|
|
48
|
+
var import_yaml = __toESM(require("yaml"));
|
|
49
|
+
var DEFAULT_BUILD_DIRECTORY = "dist";
|
|
50
|
+
var protectedDirectories = /* @__PURE__ */ new Set(["src", "types", "public", "node_modules", ".git"]);
|
|
51
|
+
var loadXfeConfig = (root = process.cwd()) => {
|
|
52
|
+
const xfeConfigPath = import_node_path.default.join(root, "xfe.json");
|
|
53
|
+
if (!import_node_fs.default.existsSync(xfeConfigPath)) {
|
|
54
|
+
throw new Error("xfe.json \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5728\u9879\u76EE\u6839\u76EE\u5F55\u4E0B\u521B\u5EFA\u8BE5\u6587\u4EF6\u3002");
|
|
55
|
+
}
|
|
56
|
+
const xfeConfigContent = import_node_fs.default.readFileSync(xfeConfigPath, "utf8");
|
|
57
|
+
const config = JSON.parse(xfeConfigContent);
|
|
58
|
+
const renderMode = config.webapp?.renderMode;
|
|
59
|
+
if (renderMode !== void 0 && renderMode !== "client" && renderMode !== "server") {
|
|
60
|
+
throw new Error("xfe.json.webapp.renderMode \u53EA\u80FD\u4E3A client \u6216 server");
|
|
61
|
+
}
|
|
62
|
+
getBuildDirectory(root, config.webapp);
|
|
63
|
+
getServerEntry(root, config.webapp);
|
|
64
|
+
return config;
|
|
65
|
+
};
|
|
66
|
+
var getBuildDirectory = (projectRoot, webapp = {}) => {
|
|
67
|
+
const configured = webapp?.buildDirectory === void 0 ? DEFAULT_BUILD_DIRECTORY : webapp.buildDirectory;
|
|
68
|
+
if (typeof configured !== "string" || !configured.trim() || /[\0\r\n]/.test(configured)) {
|
|
69
|
+
throw new Error("xfe.json.webapp.buildDirectory \u5FC5\u987B\u662F\u975E\u7A7A\u7684\u5355\u884C\u5B57\u7B26\u4E32");
|
|
70
|
+
}
|
|
71
|
+
const directory = configured.trim().replaceAll("\\", "/");
|
|
72
|
+
const root = import_node_path.default.resolve(projectRoot);
|
|
73
|
+
const output = import_node_path.default.resolve(root, directory);
|
|
74
|
+
const relative = import_node_path.default.relative(root, output);
|
|
75
|
+
const [firstDirectory = ""] = relative.split(import_node_path.default.sep);
|
|
76
|
+
const escapesProject = import_node_path.default.isAbsolute(directory) || Boolean(import_node_path.default.win32.parse(directory).root) || firstDirectory === "..";
|
|
77
|
+
const overwritesInput = !relative || protectedDirectories.has(firstDirectory.toLowerCase());
|
|
78
|
+
if (escapesProject || overwritesInput) {
|
|
79
|
+
throw new Error("xfe.json.webapp.buildDirectory \u5FC5\u987B\u662F\u9879\u76EE\u5185\u72EC\u7ACB\u7684\u8F93\u51FA\u76EE\u5F55\uFF0C\u4E0D\u80FD\u8986\u76D6\u9879\u76EE\u3001\u6E90\u7801\u6216\u4F9D\u8D56");
|
|
80
|
+
}
|
|
81
|
+
let currentDirectory = root;
|
|
82
|
+
for (const segment of relative.split(import_node_path.default.sep)) {
|
|
83
|
+
currentDirectory = import_node_path.default.join(currentDirectory, segment);
|
|
84
|
+
if (import_node_fs.default.lstatSync(currentDirectory, { throwIfNoEntry: false })?.isSymbolicLink()) {
|
|
85
|
+
throw new Error("xfe.json.webapp.buildDirectory \u4E0D\u80FD\u7ECF\u8FC7\u7B26\u53F7\u94FE\u63A5\uFF0C\u4EE5\u514D\u6E05\u7406\u5176\u4ED6\u76EE\u5F55");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return output;
|
|
89
|
+
};
|
|
90
|
+
var getServerEntry = (projectRoot, webapp = {}) => {
|
|
91
|
+
const entry = webapp?.serverEntry;
|
|
92
|
+
if (entry === void 0) return;
|
|
93
|
+
if (typeof entry !== "string" || !entry.trim() || /[\0\r\n]/.test(entry)) {
|
|
94
|
+
throw new Error("xfe.json.webapp.serverEntry \u5FC5\u987B\u662F\u9879\u76EE\u5185\u7684 TypeScript / JavaScript \u76F8\u5BF9\u8DEF\u5F84");
|
|
95
|
+
}
|
|
96
|
+
const normalized = entry.trim().replaceAll("\\", "/");
|
|
97
|
+
const absolute = import_node_path.default.resolve(projectRoot, normalized);
|
|
98
|
+
const relative = import_node_path.default.relative(import_node_path.default.resolve(projectRoot), absolute);
|
|
99
|
+
const outside = Boolean(import_node_path.default.win32.parse(normalized).root) || relative.split(import_node_path.default.sep)[0] === "..";
|
|
100
|
+
if (outside || !/\.(?:[jt]sx?|m[jt]s)$/.test(relative)) {
|
|
101
|
+
throw new Error("xfe.json.webapp.serverEntry \u5FC5\u987B\u662F\u9879\u76EE\u5185\u7684 TypeScript / JavaScript \u76F8\u5BF9\u8DEF\u5F84");
|
|
102
|
+
}
|
|
103
|
+
return absolute;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// src/framework.ts
|
|
107
|
+
var import_node_fs2 = __toESM(require("fs"));
|
|
108
|
+
var import_node_module = require("module");
|
|
109
|
+
var import_node_path2 = __toESM(require("path"));
|
|
110
|
+
var import_node_url2 = require("url");
|
|
111
|
+
var import_meta = {};
|
|
112
|
+
var frameworkDirectory = typeof __dirname === "string" ? __dirname : import_node_path2.default.dirname((0, import_node_url2.fileURLToPath)(import_meta.url));
|
|
113
|
+
var requireFromFramework = (0, import_node_module.createRequire)(import_node_path2.default.join(frameworkDirectory, "framework.js"));
|
|
114
|
+
var extensions = [".tsx", ".ts", ".jsx", ".js"];
|
|
115
|
+
var findSource = (directory, name) => extensions.map((extension) => import_node_path2.default.join(directory, `${name}${extension}`)).find(import_node_fs2.default.existsSync);
|
|
116
|
+
var findValueExports = (file) => [...import_node_fs2.default.readFileSync(file, "utf8").matchAll(/^\s*export\s+(?:async\s+)?(?:const|let|var|function|class)\s+([A-Za-z_$][\w$]*)/gm)].map(
|
|
117
|
+
([, name]) => name
|
|
118
|
+
);
|
|
119
|
+
var importPath = (from, target) => {
|
|
120
|
+
const relative = import_node_path2.default.relative(from, target).replace(import_node_path2.default.extname(target), "").replaceAll(import_node_path2.default.sep, "/");
|
|
121
|
+
return relative.startsWith(".") ? relative : `./${relative}`;
|
|
122
|
+
};
|
|
123
|
+
var writeGenerated = (file, content) => {
|
|
124
|
+
if (import_node_fs2.default.existsSync(file) && import_node_fs2.default.readFileSync(file, "utf8") === content) return;
|
|
125
|
+
import_node_fs2.default.writeFileSync(file, content);
|
|
126
|
+
};
|
|
127
|
+
var defaultRootSource = (projectRoot, appDirectory) => {
|
|
128
|
+
const { webapp = {} } = loadXfeConfig(projectRoot);
|
|
129
|
+
const layoutSource = findSource(import_node_path2.default.join(projectRoot, "src"), "layout");
|
|
130
|
+
const layoutExports = layoutSource ? findValueExports(layoutSource) : [];
|
|
131
|
+
const hasErrorView = layoutExports.includes("ErrorView");
|
|
132
|
+
const layoutImport = layoutSource ? `import AppLayout${hasErrorView ? ", { ErrorView }" : ""} from ${JSON.stringify(importPath(appDirectory, layoutSource))}
|
|
133
|
+
` : "";
|
|
134
|
+
return `import { createElement, type ReactNode } from 'react'
|
|
135
|
+
import { Outlet } from 'react-router'
|
|
136
|
+
import { Document, PageError, PageLoading } from '@xfe-repo/web-app/dist/root.mjs'
|
|
137
|
+
${layoutImport}
|
|
138
|
+
export const meta = () => [{ title: ${JSON.stringify(webapp.title || "XFE App")} }]
|
|
139
|
+
export const Layout = ({ children }: { children: ReactNode }) => createElement(Document, { children, lang: ${JSON.stringify(webapp.lang || "zh-CN")}, themeColor: ${JSON.stringify(webapp.themeColor)} })
|
|
140
|
+
export default function Root() { return ${layoutSource ? "createElement(AppLayout, { children: createElement(Outlet) })" : "createElement(Outlet)"} }
|
|
141
|
+
export function ErrorBoundary() { return createElement(PageError, { ${hasErrorView ? "view: ErrorView" : ""} }) }
|
|
142
|
+
export function HydrateFallback() { return createElement(PageLoading) }
|
|
143
|
+
`;
|
|
144
|
+
};
|
|
145
|
+
var getFrameworkRoot = (projectRoot) => import_node_path2.default.join(projectRoot, "node_modules/.cache/xfe-web/react-router");
|
|
146
|
+
var prepareFramework = (projectRoot = process.cwd()) => {
|
|
147
|
+
const sourceDirectory = import_node_path2.default.join(projectRoot, "src");
|
|
148
|
+
const rootSource = findSource(sourceDirectory, "root");
|
|
149
|
+
const appServerSource = findSource(sourceDirectory, "app.server");
|
|
150
|
+
const pagesSource = import_node_path2.default.join(sourceDirectory, "pages");
|
|
151
|
+
if (!import_node_fs2.default.existsSync(pagesSource)) throw new Error("\u672A\u627E\u5230 src/pages");
|
|
152
|
+
const rootDirectory = getFrameworkRoot(projectRoot);
|
|
153
|
+
const appDirectory = import_node_path2.default.join(rootDirectory, "app");
|
|
154
|
+
import_node_fs2.default.mkdirSync(appDirectory, { recursive: true });
|
|
155
|
+
const routerDirectory = import_node_path2.default.dirname(requireFromFramework.resolve("react-router/package.json"));
|
|
156
|
+
const routerLink = import_node_path2.default.join(rootDirectory, "node_modules/react-router");
|
|
157
|
+
import_node_fs2.default.mkdirSync(import_node_path2.default.dirname(routerLink), { recursive: true });
|
|
158
|
+
if (!import_node_fs2.default.lstatSync(routerLink, { throwIfNoEntry: false })?.isSymbolicLink() || import_node_fs2.default.readlinkSync(routerLink) !== routerDirectory) {
|
|
159
|
+
import_node_fs2.default.rmSync(routerLink, { force: true });
|
|
160
|
+
import_node_fs2.default.symlinkSync(routerDirectory, routerLink, "junction");
|
|
161
|
+
}
|
|
162
|
+
const rootTarget = import_node_path2.default.join(appDirectory, "root.tsx");
|
|
163
|
+
const rootImport = rootSource ? importPath(appDirectory, rootSource) : "";
|
|
164
|
+
const rootExports = rootSource ? findValueExports(rootSource).filter((name) => name !== "middleware" || !appServerSource) : [];
|
|
165
|
+
const namedExports = rootExports.length ? `export { ${rootExports.join(", ")} } from ${JSON.stringify(rootImport)}
|
|
166
|
+
` : "";
|
|
167
|
+
const errorBoundaryExport = rootExports.includes("ErrorBoundary") ? "" : "export { DefaultErrorBoundary as ErrorBoundary } from '@xfe-repo/web-router/errors'\n";
|
|
168
|
+
const middlewareExport = appServerSource ? `export { middleware } from ${JSON.stringify(importPath(appDirectory, appServerSource))}
|
|
169
|
+
` : "";
|
|
170
|
+
writeGenerated(
|
|
171
|
+
rootTarget,
|
|
172
|
+
rootSource ? `export * from ${JSON.stringify(rootImport)}
|
|
173
|
+
export { default } from ${JSON.stringify(rootImport)}
|
|
174
|
+
${namedExports}${errorBoundaryExport}${middlewareExport}` : `${defaultRootSource(projectRoot, appDirectory)}${middlewareExport}`
|
|
175
|
+
);
|
|
176
|
+
const pagesLink = import_node_path2.default.join(appDirectory, "pages");
|
|
177
|
+
if (!import_node_fs2.default.existsSync(pagesLink)) import_node_fs2.default.symlinkSync(import_node_path2.default.relative(appDirectory, pagesSource), pagesLink, "dir");
|
|
178
|
+
writeGenerated(
|
|
179
|
+
import_node_path2.default.join(rootDirectory, "react-router.config.js"),
|
|
180
|
+
"export { default } from '@xfe-repo/web-app/dist/react-router-config.mjs'\n"
|
|
181
|
+
);
|
|
182
|
+
writeGenerated(
|
|
183
|
+
import_node_path2.default.join(appDirectory, "routes.js"),
|
|
184
|
+
`import { collectPageRoutes } from '@xfe-repo/web-router/routes'
|
|
185
|
+
export default collectPageRoutes(${JSON.stringify(appDirectory)})
|
|
186
|
+
`
|
|
187
|
+
);
|
|
188
|
+
const defaultsDirectory = import_node_path2.default.join(import_node_path2.default.dirname(requireFromFramework.resolve("@react-router/dev/package.json")), "dist/config/defaults");
|
|
189
|
+
writeGenerated(
|
|
190
|
+
import_node_path2.default.join(appDirectory, "entry.server.tsx"),
|
|
191
|
+
`export { default, streamTimeout } from ${JSON.stringify(importPath(appDirectory, import_node_path2.default.join(defaultsDirectory, "entry.server.web.tsx")))}
|
|
192
|
+
`
|
|
193
|
+
);
|
|
194
|
+
const typeScriptConfig = import_node_path2.default.join(rootDirectory, "tsconfig.json");
|
|
195
|
+
writeGenerated(
|
|
196
|
+
typeScriptConfig,
|
|
197
|
+
JSON.stringify(
|
|
198
|
+
{
|
|
199
|
+
extends: import_node_path2.default.join(projectRoot, "tsconfig.json"),
|
|
200
|
+
compilerOptions: { rootDirs: [projectRoot, rootDirectory, import_node_path2.default.join(rootDirectory, ".react-router/types")] },
|
|
201
|
+
include: [
|
|
202
|
+
import_node_path2.default.join(sourceDirectory, "**/*"),
|
|
203
|
+
import_node_path2.default.join(projectRoot, "types/**/*"),
|
|
204
|
+
import_node_path2.default.join(appDirectory, "*.tsx"),
|
|
205
|
+
import_node_path2.default.join(rootDirectory, ".react-router/types/**/*")
|
|
206
|
+
],
|
|
207
|
+
exclude: []
|
|
208
|
+
},
|
|
209
|
+
null,
|
|
210
|
+
2
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
return {
|
|
214
|
+
appDirectory,
|
|
215
|
+
rootDirectory,
|
|
216
|
+
viteConfig: import_node_path2.default.resolve(frameworkDirectory, "vite.mjs"),
|
|
217
|
+
typeScriptConfig
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// src/node.ts
|
|
222
|
+
var NODE_OUTPUT = "server/node.mjs";
|
|
223
|
+
var HMR_PATH = "/_xfe/hmr";
|
|
224
|
+
var SHUTDOWN_TIMEOUT = 3e4;
|
|
225
|
+
async function buildNode({ projectRoot, webapp }) {
|
|
226
|
+
const { build } = await import("esbuild");
|
|
227
|
+
const entry = getServerEntry(projectRoot, webapp);
|
|
228
|
+
if (!entry) throw new Error("\u7F3A\u5C11 xfe.json.webapp.serverEntry");
|
|
229
|
+
if (webapp.renderMode === "client") await (0, import_promises.rm)(import_node_path3.default.join(getBuildDirectory(projectRoot, webapp), "server"), { recursive: true, force: true });
|
|
230
|
+
await build({
|
|
231
|
+
absWorkingDir: projectRoot,
|
|
232
|
+
entryPoints: [entry],
|
|
233
|
+
outfile: import_node_path3.default.join(getBuildDirectory(projectRoot, webapp), NODE_OUTPUT),
|
|
234
|
+
tsconfig: import_node_path3.default.join(projectRoot, "tsconfig.json"),
|
|
235
|
+
bundle: true,
|
|
236
|
+
packages: "external",
|
|
237
|
+
platform: "node",
|
|
238
|
+
format: "esm",
|
|
239
|
+
target: "node22.22",
|
|
240
|
+
sourcemap: true
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
async function runNode({ projectRoot, webapp, development }) {
|
|
244
|
+
const entry = development ? getServerEntry(projectRoot, webapp) : import_node_path3.default.join(getBuildDirectory(projectRoot, webapp), NODE_OUTPUT);
|
|
245
|
+
if (!entry) throw new Error("\u7F3A\u5C11 xfe.json.webapp.serverEntry");
|
|
246
|
+
if (development) {
|
|
247
|
+
const { loadEnv } = await import("vite");
|
|
248
|
+
Object.assign(process.env, { ...loadEnv("development", projectRoot, ""), ...process.env });
|
|
249
|
+
} else process.env.NODE_ENV ??= "production";
|
|
250
|
+
const module2 = await import((0, import_node_url3.pathToFileURL)(entry).href);
|
|
251
|
+
if (typeof module2.default !== "function") throw new Error("serverEntry \u5FC5\u987B\u9ED8\u8BA4\u5BFC\u51FA Node \u670D\u52A1\u5DE5\u5382\u51FD\u6570");
|
|
252
|
+
const frontend = (0, import_express.default)().disable("x-powered-by");
|
|
253
|
+
const application = await module2.default({ frontend, mode: development ? "development" : "production" });
|
|
254
|
+
const upgrades = /* @__PURE__ */ new Set();
|
|
255
|
+
let vite;
|
|
256
|
+
let closing;
|
|
257
|
+
const close = () => closing ??= disposeApplication(application, { vite, upgrades });
|
|
258
|
+
const onSignal = async () => {
|
|
259
|
+
try {
|
|
260
|
+
await close();
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.error("XFE Node \u5173\u95ED\u5931\u8D25:", error);
|
|
263
|
+
process.exit(1);
|
|
264
|
+
} finally {
|
|
265
|
+
process.off("SIGINT", onSignal);
|
|
266
|
+
process.off("SIGTERM", onSignal);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
try {
|
|
270
|
+
const server = application?.server;
|
|
271
|
+
if (!(server instanceof import_node_http.Server) || server.listening) throw new Error("Node \u5DE5\u5382\u5FC5\u987B\u8FD4\u56DE\u5C1A\u672A listen \u7684 http.Server");
|
|
272
|
+
if (server.listenerCount("upgrade")) throw new Error("\u8BF7\u901A\u8FC7\u5DE5\u5382\u8FD4\u56DE\u7684 upgrade \u5904\u7406\u5347\u7EA7\uFF0C\u4E0D\u8981\u76F4\u63A5\u6CE8\u518C server \u7684 upgrade \u4E8B\u4EF6");
|
|
273
|
+
const hmrPath = import_node_path3.default.posix.join(webapp.basename || "/", HMR_PATH);
|
|
274
|
+
server.on(
|
|
275
|
+
"upgrade",
|
|
276
|
+
/** HMR 与业务互斥分流,升级连接由关闭流程统一回收。 */
|
|
277
|
+
(request, socket, head) => {
|
|
278
|
+
upgrades.add(socket);
|
|
279
|
+
socket.once(
|
|
280
|
+
"close",
|
|
281
|
+
/** 不保留已经断开的升级连接。 */
|
|
282
|
+
() => upgrades.delete(socket)
|
|
283
|
+
);
|
|
284
|
+
if (development && request.url?.split("?")[0] === hmrPath) {
|
|
285
|
+
if (!["vite-hmr", "vite-ping"].includes(String(request.headers["sec-websocket-protocol"]))) socket.destroy();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (application.upgrade) application.upgrade(request, socket, head);
|
|
289
|
+
else socket.destroy();
|
|
290
|
+
}
|
|
291
|
+
);
|
|
292
|
+
if (development) vite = await createDevelopmentServer(server, projectRoot);
|
|
293
|
+
await mountFrontend(frontend, { projectRoot, webapp, vite });
|
|
294
|
+
const listening = (0, import_node_events.once)(server, "listening");
|
|
295
|
+
const host = process.env.HOST || webapp.host || "0.0.0.0";
|
|
296
|
+
const port = Number(process.env.PORT ?? webapp.port ?? 3e3);
|
|
297
|
+
server.listen(port, host);
|
|
298
|
+
await listening;
|
|
299
|
+
console.info(`XFE Node: http://${host}:${port}`);
|
|
300
|
+
process.on("SIGINT", onSignal);
|
|
301
|
+
process.on("SIGTERM", onSignal);
|
|
302
|
+
} catch (error) {
|
|
303
|
+
await close();
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
async function createDevelopmentServer(server, projectRoot) {
|
|
308
|
+
const { createServer } = await import("vite");
|
|
309
|
+
const framework = prepareFramework(projectRoot);
|
|
310
|
+
const events = [[process, "SIGINT"], [process, "SIGTERM"], [process.stdin, "end"]];
|
|
311
|
+
const snapshots = events.map(
|
|
312
|
+
/** 只记录当前监听,恢复时保留宿主和业务原有的处理器。 */
|
|
313
|
+
([source, event]) => ({ source, event, listeners: new Set(source.listeners(event)) })
|
|
314
|
+
);
|
|
315
|
+
try {
|
|
316
|
+
return await createServer({
|
|
317
|
+
root: framework.rootDirectory,
|
|
318
|
+
configFile: framework.viteConfig,
|
|
319
|
+
server: { middlewareMode: true, ws: { server, path: HMR_PATH } },
|
|
320
|
+
appType: "custom"
|
|
321
|
+
});
|
|
322
|
+
} finally {
|
|
323
|
+
for (const { source, event, listeners } of snapshots) {
|
|
324
|
+
for (const listener of source.listeners(event)) {
|
|
325
|
+
if (!listeners.has(listener)) source.removeListener(event, listener);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async function mountFrontend(frontend, { projectRoot, webapp, vite }) {
|
|
331
|
+
let build;
|
|
332
|
+
if (vite) {
|
|
333
|
+
const { isRunnableDevEnvironment } = await import("vite");
|
|
334
|
+
const environment = vite.environments.ssr;
|
|
335
|
+
if (!isRunnableDevEnvironment(environment)) throw new Error("React Router \u5F00\u53D1\u73AF\u5883\u672A\u5C31\u7EEA");
|
|
336
|
+
frontend.use(vite.middlewares);
|
|
337
|
+
build = async () => environment.runner.import("virtual:react-router/server-build");
|
|
338
|
+
} else {
|
|
339
|
+
const directory = getBuildDirectory(projectRoot, webapp);
|
|
340
|
+
const basename = webapp.basename || "/";
|
|
341
|
+
frontend.use(import_node_path3.default.posix.join(basename, "assets"), import_express.default.static(import_node_path3.default.join(directory, "client/assets"), { immutable: true, maxAge: "1y", fallthrough: false }));
|
|
342
|
+
frontend.use(basename, import_express.default.static(import_node_path3.default.join(directory, "client")));
|
|
343
|
+
if (webapp.renderMode === "client") {
|
|
344
|
+
frontend.get(
|
|
345
|
+
import_node_path3.default.posix.join(basename, "{*path}"),
|
|
346
|
+
/** SPA 深链接始终返回构建后的首页。 */
|
|
347
|
+
(_request, response) => response.sendFile(import_node_path3.default.join(directory, "client/index.html"))
|
|
348
|
+
);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
build = await import((0, import_node_url3.pathToFileURL)(import_node_path3.default.join(directory, "server/index.js")).href);
|
|
352
|
+
}
|
|
353
|
+
const { createRequestHandler } = await import("@react-router/express");
|
|
354
|
+
frontend.use(createRequestHandler({ build, mode: vite ? "development" : "production" }));
|
|
355
|
+
}
|
|
356
|
+
async function disposeApplication(application, { vite, upgrades }) {
|
|
357
|
+
const server = application?.server instanceof import_node_http.Server ? application.server : void 0;
|
|
358
|
+
const forced = setTimeout(
|
|
359
|
+
/** 生产关闭时中断超过等待上限的普通 HTTP 请求。 */
|
|
360
|
+
() => server?.closeAllConnections(),
|
|
361
|
+
SHUTDOWN_TIMEOUT
|
|
362
|
+
);
|
|
363
|
+
forced.unref();
|
|
364
|
+
try {
|
|
365
|
+
const drained = server?.listening ? server[Symbol.asyncDispose]() : void 0;
|
|
366
|
+
for (const socket of upgrades) socket.destroy();
|
|
367
|
+
const results = await Promise.allSettled([drained, vite?.close()]);
|
|
368
|
+
await application?.close?.();
|
|
369
|
+
const failure = results.find((result) => result.status === "rejected");
|
|
370
|
+
if (failure?.status === "rejected") throw failure.reason;
|
|
371
|
+
} finally {
|
|
372
|
+
clearTimeout(forced);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
376
|
+
0 && (module.exports = {
|
|
377
|
+
buildNode,
|
|
378
|
+
runNode
|
|
379
|
+
});
|