@tbox.cn/app-build 1.0.0-alpha.18 → 1.0.0-alpha.20
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/index.d.ts +36 -1
- package/dist/index.js +149 -9
- package/package.json +2 -1
- package/static-build-worker.mjs +145 -0
package/dist/index.d.ts
CHANGED
|
@@ -202,6 +202,41 @@ interface TboxAppPluginOptions {
|
|
|
202
202
|
}
|
|
203
203
|
declare function tboxAppPlugin(options?: TboxAppPluginOptions): Plugin;
|
|
204
204
|
|
|
205
|
+
type StaticBuildMode = 'child' | 'inline' | 'off';
|
|
206
|
+
interface StaticBuildErrorInfo {
|
|
207
|
+
/** build = 编译失败;publish = 产物切换失败;worker-exit = 子进程异常退出 */
|
|
208
|
+
reason: 'build' | 'publish' | 'worker-exit';
|
|
209
|
+
message: string;
|
|
210
|
+
}
|
|
211
|
+
interface StaticBuildOptions {
|
|
212
|
+
/** apps/client 绝对路径(vite root) */
|
|
213
|
+
clientRoot: string;
|
|
214
|
+
/** 缺省读 TBOX_DEV_STATIC_BUILD,再缺省 'child';非法值告警后按 'child' */
|
|
215
|
+
mode?: StaticBuildMode;
|
|
216
|
+
onBuildStart: () => void;
|
|
217
|
+
onBuildEnd: () => void;
|
|
218
|
+
onBuildError: (info: StaticBuildErrorInfo) => void;
|
|
219
|
+
logger: {
|
|
220
|
+
info(msg: string, ctx?: Record<string, unknown>): void;
|
|
221
|
+
warn(msg: string, ctx?: Record<string, unknown>): void;
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
interface StaticBuildHandle {
|
|
225
|
+
readonly mode: StaticBuildMode;
|
|
226
|
+
close(): Promise<void>;
|
|
227
|
+
}
|
|
228
|
+
interface StaticBuildMessage {
|
|
229
|
+
tbox: 'static-build';
|
|
230
|
+
v: 1;
|
|
231
|
+
type: 'start' | 'end' | 'error';
|
|
232
|
+
durationMs?: number;
|
|
233
|
+
reason?: 'build' | 'publish';
|
|
234
|
+
message?: string;
|
|
235
|
+
}
|
|
236
|
+
/** IPC 消息过滤:只认本协议标记,防外来消息(如加载器依赖上报)误触发回调。 */
|
|
237
|
+
declare function isStaticBuildMessage(raw: unknown): raw is StaticBuildMessage;
|
|
238
|
+
declare function startStaticBuild(options: StaticBuildOptions): StaticBuildHandle;
|
|
239
|
+
|
|
205
240
|
interface TboxModuleSourcesOptions {
|
|
206
241
|
/** 应用根(含 apps/client 与 modules/) */
|
|
207
242
|
appRoot: string;
|
|
@@ -224,4 +259,4 @@ interface TboxVendorLayerOptions {
|
|
|
224
259
|
*/
|
|
225
260
|
declare function tboxVendorLayerPlugin(options?: TboxVendorLayerOptions): Plugin;
|
|
226
261
|
|
|
227
|
-
export { type ActionModule, AppBuildError, type AppKind, type AppManifest, type AppMeta, type CardModule, type CardSampleEntry, type CardSampleSet, type CheckIssue, type CheckResult, FUNCTION_ID_RE, type FunctionModule, type GenerateOptions, type GenerateResult, type GeneratedApp, type LoadedModule, type ModuleDescriptor, type PageEntry, type PageMeta, type PageModule, type ScanResult, type SkillModule, type TboxAppPluginOptions, type TboxModuleSourcesOptions, type TboxVendorLayerOptions, type ToolModule, buildManifest, checkApp, generateApp, listGeneratedFiles, parsePageMeta, readAppMeta, readCardSamples, scanApp, scanAppPrimitives, scanFunctions, scanModulePrimitives, scanModules, scanPages, tboxAppPlugin, tboxModuleSourcesPlugin, tboxVendorLayerPlugin, wrapVendorCss };
|
|
262
|
+
export { type ActionModule, AppBuildError, type AppKind, type AppManifest, type AppMeta, type CardModule, type CardSampleEntry, type CardSampleSet, type CheckIssue, type CheckResult, FUNCTION_ID_RE, type FunctionModule, type GenerateOptions, type GenerateResult, type GeneratedApp, type LoadedModule, type ModuleDescriptor, type PageEntry, type PageMeta, type PageModule, type ScanResult, type SkillModule, type StaticBuildErrorInfo, type StaticBuildHandle, type StaticBuildMode, type StaticBuildOptions, type TboxAppPluginOptions, type TboxModuleSourcesOptions, type TboxVendorLayerOptions, type ToolModule, buildManifest, checkApp, generateApp, isStaticBuildMessage, listGeneratedFiles, parsePageMeta, readAppMeta, readCardSamples, scanApp, scanAppPrimitives, scanFunctions, scanModulePrimitives, scanModules, scanPages, startStaticBuild, tboxAppPlugin, tboxModuleSourcesPlugin, tboxVendorLayerPlugin, wrapVendorCss };
|
package/dist/index.js
CHANGED
|
@@ -1162,31 +1162,169 @@ function tboxAppPlugin(options = {}) {
|
|
|
1162
1162
|
};
|
|
1163
1163
|
}
|
|
1164
1164
|
|
|
1165
|
+
// src/static-build.ts
|
|
1166
|
+
import { fork } from "child_process";
|
|
1167
|
+
import path11 from "path";
|
|
1168
|
+
import { createRequire } from "module";
|
|
1169
|
+
import { pathToFileURL, fileURLToPath } from "url";
|
|
1170
|
+
function isStaticBuildMessage(raw) {
|
|
1171
|
+
const m = raw;
|
|
1172
|
+
return typeof m === "object" && m !== null && m.tbox === "static-build" && m.v === 1 && (m.type === "start" || m.type === "end" || m.type === "error");
|
|
1173
|
+
}
|
|
1174
|
+
var WORKER_PATH = fileURLToPath(new URL("../static-build-worker.mjs", import.meta.url));
|
|
1175
|
+
var BACKOFF_MS = [2e3, 1e4, 3e4];
|
|
1176
|
+
var CRASH_WINDOW_MS = 6e5;
|
|
1177
|
+
var CRASH_LIMIT = 3;
|
|
1178
|
+
function resolveMode(explicit, envValue, logger) {
|
|
1179
|
+
const value = explicit ?? envValue ?? "child";
|
|
1180
|
+
if (value === "child" || value === "inline" || value === "off") return value;
|
|
1181
|
+
logger.warn("dev_static_build_mode_invalid", { value, fallback: "child" });
|
|
1182
|
+
return "child";
|
|
1183
|
+
}
|
|
1184
|
+
function startInline(options) {
|
|
1185
|
+
let watcher = null;
|
|
1186
|
+
void (async () => {
|
|
1187
|
+
try {
|
|
1188
|
+
const req = createRequire(path11.join(options.clientRoot, "package.json"));
|
|
1189
|
+
const vite = await import(pathToFileURL(req.resolve("vite")).href);
|
|
1190
|
+
const result = await vite.build({
|
|
1191
|
+
root: options.clientRoot,
|
|
1192
|
+
build: { watch: {}, sourcemap: false }
|
|
1193
|
+
});
|
|
1194
|
+
const w = Array.isArray(result) ? result[0] : result;
|
|
1195
|
+
watcher = w;
|
|
1196
|
+
w?.on?.("event", (event) => {
|
|
1197
|
+
if (event.code === "BUNDLE_START") options.onBuildStart();
|
|
1198
|
+
if (event.code === "BUNDLE_END") options.onBuildEnd();
|
|
1199
|
+
if (event.code === "ERROR") options.onBuildError({ reason: "build", message: event.error?.message ?? String(event.error) });
|
|
1200
|
+
});
|
|
1201
|
+
} catch (err) {
|
|
1202
|
+
options.onBuildError({ reason: "build", message: err instanceof Error ? err.message : String(err) });
|
|
1203
|
+
}
|
|
1204
|
+
})();
|
|
1205
|
+
return {
|
|
1206
|
+
mode: "inline",
|
|
1207
|
+
close: async () => {
|
|
1208
|
+
try {
|
|
1209
|
+
await watcher?.close?.();
|
|
1210
|
+
} catch {
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
function startChild(options) {
|
|
1216
|
+
let child = null;
|
|
1217
|
+
let closing = false;
|
|
1218
|
+
let restartTimer = null;
|
|
1219
|
+
let exitTimes = [];
|
|
1220
|
+
const spawn = () => {
|
|
1221
|
+
if (closing) return;
|
|
1222
|
+
try {
|
|
1223
|
+
child = fork(WORKER_PATH, [options.clientRoot], {
|
|
1224
|
+
// 不继承 tsx 注入的 --require/--import:子进程以纯 Node 加载 vite.config,且
|
|
1225
|
+
// tsx 的加载器会占用 IPC 通道发依赖消息,干扰本协议。
|
|
1226
|
+
execArgv: [],
|
|
1227
|
+
env: { ...process.env, NODE_ENV: "production" },
|
|
1228
|
+
stdio: ["ignore", "pipe", "pipe", "ipc"]
|
|
1229
|
+
});
|
|
1230
|
+
} catch (err) {
|
|
1231
|
+
options.onBuildError({ reason: "worker-exit", message: err instanceof Error ? err.message : String(err) });
|
|
1232
|
+
scheduleRestart();
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
options.logger.info("dev_static_build_worker_started", {});
|
|
1236
|
+
child.stdout?.on("data", (chunk) => process.stdout.write(chunk));
|
|
1237
|
+
child.stderr?.on("data", (chunk) => process.stderr.write(chunk));
|
|
1238
|
+
child.on("message", (raw) => {
|
|
1239
|
+
if (!isStaticBuildMessage(raw)) return;
|
|
1240
|
+
if (raw.type === "start") options.onBuildStart();
|
|
1241
|
+
else if (raw.type === "end") options.onBuildEnd();
|
|
1242
|
+
else if (raw.type === "error") options.onBuildError({ reason: raw.reason ?? "build", message: raw.message ?? "" });
|
|
1243
|
+
});
|
|
1244
|
+
child.on("exit", (code, signal) => {
|
|
1245
|
+
child = null;
|
|
1246
|
+
if (closing) return;
|
|
1247
|
+
options.onBuildError({ reason: "worker-exit", message: `static build worker exited (code=${code} signal=${signal ?? "none"})` });
|
|
1248
|
+
const now = Date.now();
|
|
1249
|
+
exitTimes = [...exitTimes.filter((t) => now - t < CRASH_WINDOW_MS), now];
|
|
1250
|
+
if (exitTimes.length > CRASH_LIMIT) {
|
|
1251
|
+
options.logger.warn("dev_static_build_disabled", { exits: exitTimes.length });
|
|
1252
|
+
closing = true;
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
scheduleRestart();
|
|
1256
|
+
});
|
|
1257
|
+
};
|
|
1258
|
+
const scheduleRestart = () => {
|
|
1259
|
+
if (closing) return;
|
|
1260
|
+
const delay = BACKOFF_MS[Math.min(exitTimes.length - 1, BACKOFF_MS.length - 1)] ?? BACKOFF_MS[0];
|
|
1261
|
+
restartTimer = setTimeout(spawn, delay);
|
|
1262
|
+
};
|
|
1263
|
+
spawn();
|
|
1264
|
+
return {
|
|
1265
|
+
mode: "child",
|
|
1266
|
+
close: async () => {
|
|
1267
|
+
closing = true;
|
|
1268
|
+
if (restartTimer) clearTimeout(restartTimer);
|
|
1269
|
+
const current = child;
|
|
1270
|
+
if (!current) return;
|
|
1271
|
+
await new Promise((resolve) => {
|
|
1272
|
+
const done = () => resolve();
|
|
1273
|
+
current.once("exit", done);
|
|
1274
|
+
try {
|
|
1275
|
+
current.disconnect();
|
|
1276
|
+
} catch {
|
|
1277
|
+
}
|
|
1278
|
+
setTimeout(() => {
|
|
1279
|
+
try {
|
|
1280
|
+
current.kill("SIGTERM");
|
|
1281
|
+
} catch {
|
|
1282
|
+
}
|
|
1283
|
+
setTimeout(() => {
|
|
1284
|
+
try {
|
|
1285
|
+
current.kill("SIGKILL");
|
|
1286
|
+
} catch {
|
|
1287
|
+
}
|
|
1288
|
+
}, 1e3).unref();
|
|
1289
|
+
}, 2e3).unref();
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
function startStaticBuild(options) {
|
|
1295
|
+
const mode = resolveMode(options.mode, process.env.TBOX_DEV_STATIC_BUILD, options.logger);
|
|
1296
|
+
options.logger.info("dev_static_build", { mode });
|
|
1297
|
+
if (mode === "off") return { mode, close: async () => {
|
|
1298
|
+
} };
|
|
1299
|
+
if (mode === "inline") return startInline(options);
|
|
1300
|
+
return startChild(options);
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1165
1303
|
// src/tailwind-sources.ts
|
|
1166
|
-
import
|
|
1304
|
+
import path13 from "path";
|
|
1167
1305
|
|
|
1168
1306
|
// tailwind-sources.mjs
|
|
1169
1307
|
import { existsSync as existsSync9, readdirSync as readdirSync2, realpathSync, statSync } from "fs";
|
|
1170
|
-
import
|
|
1308
|
+
import path12 from "path";
|
|
1171
1309
|
var MODULE_ROOTS = [
|
|
1172
1310
|
{ dir: "modules", prefixes: ["module-"] },
|
|
1173
1311
|
{ dir: "apps/client/node_modules/@tbox.cn", prefixes: ["app-module-", "module-"] }
|
|
1174
1312
|
];
|
|
1175
1313
|
function collectModuleSourceDirs(appRoot) {
|
|
1176
|
-
const root =
|
|
1314
|
+
const root = path12.resolve(appRoot);
|
|
1177
1315
|
const seen = /* @__PURE__ */ new Set();
|
|
1178
1316
|
const out = [];
|
|
1179
1317
|
for (const { dir, prefixes } of MODULE_ROOTS) {
|
|
1180
|
-
const base =
|
|
1318
|
+
const base = path12.join(root, dir);
|
|
1181
1319
|
if (!existsSync9(base)) continue;
|
|
1182
1320
|
for (const name of readdirSync2(base).sort()) {
|
|
1183
1321
|
if (!prefixes.some((prefix) => name.startsWith(prefix))) continue;
|
|
1184
|
-
const moduleDir =
|
|
1185
|
-
const clientDir =
|
|
1322
|
+
const moduleDir = path12.join(base, name);
|
|
1323
|
+
const clientDir = path12.join(moduleDir, "src/client");
|
|
1186
1324
|
let real;
|
|
1187
1325
|
try {
|
|
1188
1326
|
if (!statSync(moduleDir).isDirectory()) continue;
|
|
1189
|
-
if (!existsSync9(
|
|
1327
|
+
if (!existsSync9(path12.join(clientDir, "index.ts"))) continue;
|
|
1190
1328
|
real = realpathSync(clientDir);
|
|
1191
1329
|
} catch {
|
|
1192
1330
|
continue;
|
|
@@ -1199,7 +1337,7 @@ function collectModuleSourceDirs(appRoot) {
|
|
|
1199
1337
|
return out.sort();
|
|
1200
1338
|
}
|
|
1201
1339
|
function renderModuleSources(dirs) {
|
|
1202
|
-
return dirs.map((dir) => `@source ${JSON.stringify(dir.split(
|
|
1340
|
+
return dirs.map((dir) => `@source ${JSON.stringify(dir.split(path12.sep).join("/"))};`).join("\n");
|
|
1203
1341
|
}
|
|
1204
1342
|
|
|
1205
1343
|
// src/tailwind-sources.ts
|
|
@@ -1209,7 +1347,7 @@ function isTailwindEntry(id) {
|
|
|
1209
1347
|
return file.endsWith(TAILWIND_ENTRY) || file === "/src/styles/tailwind.css";
|
|
1210
1348
|
}
|
|
1211
1349
|
function tboxModuleSourcesPlugin(options) {
|
|
1212
|
-
const appRoot =
|
|
1350
|
+
const appRoot = path13.resolve(options.appRoot);
|
|
1213
1351
|
return {
|
|
1214
1352
|
name: "tbox-module-sources",
|
|
1215
1353
|
enforce: "pre",
|
|
@@ -1266,6 +1404,7 @@ export {
|
|
|
1266
1404
|
buildManifest,
|
|
1267
1405
|
checkApp,
|
|
1268
1406
|
generateApp,
|
|
1407
|
+
isStaticBuildMessage,
|
|
1269
1408
|
listGeneratedFiles,
|
|
1270
1409
|
parsePageMeta,
|
|
1271
1410
|
readAppMeta,
|
|
@@ -1276,6 +1415,7 @@ export {
|
|
|
1276
1415
|
scanModulePrimitives,
|
|
1277
1416
|
scanModules,
|
|
1278
1417
|
scanPages,
|
|
1418
|
+
startStaticBuild,
|
|
1279
1419
|
tboxAppPlugin,
|
|
1280
1420
|
tboxModuleSourcesPlugin,
|
|
1281
1421
|
tboxVendorLayerPlugin,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-build",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "tbox-app 1.0 build-time leaf: scan apps/ modules/ and generate manifest, registries, documents; Vite plugin and checks.",
|
|
6
6
|
"exports": {
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"files": [
|
|
45
45
|
"dist",
|
|
46
|
+
"static-build-worker.mjs",
|
|
46
47
|
"tailwind-sources.mjs",
|
|
47
48
|
"tailwind-sources.d.mts",
|
|
48
49
|
"README.md",
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* static-build-worker.mjs — protected dev 静态面 watch 构建子进程(fork 由 @tbox.cn/app-build
|
|
4
|
+
* 的 startStaticBuild 驱动;app-build 唯一真源,模板零复制)。
|
|
5
|
+
*
|
|
6
|
+
* 职责:
|
|
7
|
+
* - 以 production 模式跑 apps/client 的 vite build --watch(dev 进程的 vite server 会把
|
|
8
|
+
* NODE_ENV 置为 development,子进程显式覆写,静态面产物与生产对齐);
|
|
9
|
+
* - 先写 node_modules/.tbox/static-build/out 暂存目录,BUNDLE_END 后原子切换 dist——
|
|
10
|
+
* 重建期间静态面持续服务上一次成功产物(不再整体 503),构建失败时 dist 保持不变;
|
|
11
|
+
* - 经 IPC 上报 start/end/error 三种事件(父进程只消费带 tbox 标记的消息);
|
|
12
|
+
* - stdout/stderr 由父进程原样转发(box 侧按行首正则识别错误,禁加前缀/改写)。
|
|
13
|
+
*
|
|
14
|
+
* 生命周期:父进程 IPC 断开(dev 进程退出 / tsx watch 重启)即自行退出;fork 子进程
|
|
15
|
+
* 与 dev 进程同进程组,沙箱管理进程按组结束时会一并回收。
|
|
16
|
+
*/
|
|
17
|
+
import { cpSync, existsSync, readdirSync, renameSync, rmSync } from 'node:fs';
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { createRequire } from 'node:module';
|
|
21
|
+
import { pathToFileURL } from 'node:url';
|
|
22
|
+
|
|
23
|
+
const clientRoot = path.resolve(process.argv[2] ?? '.');
|
|
24
|
+
const send = (msg) => {
|
|
25
|
+
if (typeof process.send === 'function') {
|
|
26
|
+
try { process.send(msg); } catch { /* IPC 已断开,父进程退出中 */ }
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const fail = (reason, message) =>
|
|
30
|
+
send({ tbox: 'static-build', v: 1, type: 'error', reason, message: String(message).slice(0, 2000) });
|
|
31
|
+
|
|
32
|
+
// 低优先级:CPU 紧张(小核沙箱)时让 dev 面 HTTP/HMR/就绪探测优先调度;失败忽略。
|
|
33
|
+
try { os.setPriority(10); } catch { /* 平台不支持(如 Windows)则跳过 */ }
|
|
34
|
+
|
|
35
|
+
const buildRoot = path.join(clientRoot, 'node_modules', '.tbox', 'static-build');
|
|
36
|
+
const staging = path.join(buildRoot, 'out');
|
|
37
|
+
const distDir = path.join(clientRoot, 'dist');
|
|
38
|
+
|
|
39
|
+
// 上一轮异常退出残留处置:dist 缺失(恰在 dist→backup 与 out→dist 两次 rename 之间被杀)
|
|
40
|
+
// 时先用 backup 恢复最后一次成功产物,再清理 staging 与残留 backup。
|
|
41
|
+
rmSync(staging, { recursive: true, force: true });
|
|
42
|
+
if (existsSync(buildRoot)) {
|
|
43
|
+
const backups = readdirSync(buildRoot)
|
|
44
|
+
.filter((name) => name.startsWith('backup-'))
|
|
45
|
+
.sort();
|
|
46
|
+
if (!existsSync(distDir) && backups.length > 0) {
|
|
47
|
+
try {
|
|
48
|
+
renameSync(path.join(buildRoot, backups.at(-1)), distDir);
|
|
49
|
+
} catch { /* 恢复失败留给首轮构建产出 */ }
|
|
50
|
+
}
|
|
51
|
+
for (const name of backups) {
|
|
52
|
+
rmSync(path.join(buildRoot, name), { recursive: true, force: true });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** staging 就绪校验 → dist→backup → staging→dist → 删 backup;跨挂载点(EXDEV)回退复制;失败回滚。 */
|
|
57
|
+
function publish() {
|
|
58
|
+
if (!existsSync(path.join(staging, 'index.html'))) {
|
|
59
|
+
throw new Error(`staging 产物不完整(缺 index.html):${staging}`);
|
|
60
|
+
}
|
|
61
|
+
const backup = path.join(buildRoot, `backup-${Date.now().toString(36)}`);
|
|
62
|
+
rmSync(backup, { recursive: true, force: true });
|
|
63
|
+
const hadDist = existsSync(distDir);
|
|
64
|
+
if (hadDist) renameSync(distDir, backup);
|
|
65
|
+
try {
|
|
66
|
+
renameSync(staging, distDir);
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (err && err.code === 'EXDEV') {
|
|
69
|
+
rmSync(distDir, { recursive: true, force: true });
|
|
70
|
+
cpSync(staging, distDir, { recursive: true });
|
|
71
|
+
} else {
|
|
72
|
+
if (hadDist) renameSync(backup, distDir);
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
rmSync(backup, { recursive: true, force: true });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let watcher = null;
|
|
80
|
+
let closing = false;
|
|
81
|
+
process.on('disconnect', () => {
|
|
82
|
+
closing = true;
|
|
83
|
+
const exit = () => process.exit(0);
|
|
84
|
+
try { watcher?.close?.().then(exit, exit); } catch { exit(); }
|
|
85
|
+
setTimeout(exit, 2000).unref();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
const req = createRequire(path.join(clientRoot, 'package.json'));
|
|
90
|
+
const vite = await import(pathToFileURL(req.resolve('vite')).href);
|
|
91
|
+
watcher = await vite.build({
|
|
92
|
+
root: clientRoot,
|
|
93
|
+
logLevel: 'warn',
|
|
94
|
+
build: {
|
|
95
|
+
outDir: staging,
|
|
96
|
+
emptyOutDir: true,
|
|
97
|
+
sourcemap: false,
|
|
98
|
+
reportCompressedSize: false,
|
|
99
|
+
chunkSizeWarningLimit: 100000,
|
|
100
|
+
watch: { buildDelay: 300 },
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
// end 单飞:BUNDLE_END 与竞态补偿共用;BUNDLE_START 归零供下一轮重建上报。
|
|
104
|
+
let endSent = false;
|
|
105
|
+
const notifyEnd = (durationMs) => {
|
|
106
|
+
if (endSent) return;
|
|
107
|
+
endSent = true;
|
|
108
|
+
send({ tbox: 'static-build', v: 1, type: 'end', durationMs });
|
|
109
|
+
};
|
|
110
|
+
const publishAndNotify = (durationMs) => {
|
|
111
|
+
if (endSent) return;
|
|
112
|
+
try {
|
|
113
|
+
publish();
|
|
114
|
+
notifyEnd(durationMs);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
fail('publish', err?.message ?? err);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
watcher.on('event', (event) => {
|
|
120
|
+
if (closing) return;
|
|
121
|
+
if (event.code === 'BUNDLE_START') {
|
|
122
|
+
endSent = false;
|
|
123
|
+
send({ tbox: 'static-build', v: 1, type: 'start' });
|
|
124
|
+
} else if (event.code === 'BUNDLE_END') {
|
|
125
|
+
publishAndNotify(event.duration);
|
|
126
|
+
} else if (event.code === 'ERROR') {
|
|
127
|
+
fail('build', event.error?.message ?? event.error);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
// 竞态补偿:极快机器上首轮 BUNDLE_START/END 可能在监听器挂载前已发完(构建已完成但
|
|
131
|
+
// publish 未执行)——挂载后短暂轮询,staging 有完整产物且 end 未上报则补发一次。
|
|
132
|
+
void (async () => {
|
|
133
|
+
for (let i = 0; i < 12 && !closing && !endSent; i++) {
|
|
134
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
135
|
+
if (closing || endSent) return;
|
|
136
|
+
if (existsSync(path.join(staging, 'index.html'))) {
|
|
137
|
+
publishAndNotify(0);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
})();
|
|
142
|
+
} catch (err) {
|
|
143
|
+
fail('build', err?.message ?? err);
|
|
144
|
+
process.exit(1);
|
|
145
|
+
}
|