@lark-apaas/coding-html-devserver 0.1.8-alpha.20260818172600 → 0.1.8-alpha.20260820104214
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/lib/dev.d.ts +1 -5
- package/lib/dev.js +6 -3
- package/lib/minify.js +3 -3
- package/lib/plugins/base-path.d.ts +1 -1
- package/lib/plugins/health.d.ts +12 -7
- package/lib/plugins/health.js +14 -5
- package/lib/plugins/inject.d.ts +1 -1
- package/lib/plugins/raw-jsx.d.ts +1 -1
- package/lib/plugins/raw-jsx.js +1 -3
- package/lib/plugins/routes-json.d.ts +1 -3
- package/lib/plugins/runtime-url-fix.d.ts +1 -1
- package/lib/plugins/vite-client-patch.d.ts +1 -1
- package/lib/plugins/vite-client-patch.js +38 -7
- package/lib/plugins/ws-watchdog.d.ts +1 -3
- package/lib/plugins/ws-watchdog.js +1 -1
- package/lib/vite-config.d.ts +1 -1
- package/lib/vite-config.js +2 -10
- package/package.json +1 -1
package/lib/dev.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import type { ViteDevServer } from 'vite'
|
|
1
|
+
import type { ViteDevServer } from 'vite';
|
|
2
2
|
import { type HtmlViteOptions } from './vite-config';
|
|
3
|
-
/**
|
|
4
|
-
* vite 7/8 的 Node API 是 ESM-only。`module: Node16` 会在 CommonJS 产物中保留
|
|
5
|
-
* 原生动态 import,避免将它降级成无法加载 Vite 的 require。
|
|
6
|
-
*/
|
|
7
3
|
/**
|
|
8
4
|
* 起 html stack 的 dev server(vite 封装在内,用户不感知)。
|
|
9
5
|
* 热更新走 vite 原生 HMR/WS。
|
package/lib/dev.js
CHANGED
|
@@ -3,15 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.startDevServer = startDevServer;
|
|
4
4
|
const vite_config_1 = require("./vite-config");
|
|
5
5
|
/**
|
|
6
|
-
* vite 7/8 的 Node API 是 ESM-only
|
|
7
|
-
*
|
|
6
|
+
* vite 7/8 的 Node API 是 ESM-only(package `exports` 无 CJS 入口)。本包编译为
|
|
7
|
+
* CommonJS,而 tsc 在 `module: CommonJS` 下会把字面 `import()` 降级成 `require()`
|
|
8
|
+
* —— 对 ESM-only 的 vite 会 `ERR_REQUIRE_ESM`。用 `new Function` 包一层,让 tsc
|
|
9
|
+
* 看不到这是 import(),从而保留运行时真正的动态 ESM import。
|
|
8
10
|
*/
|
|
11
|
+
const importESM = new Function('specifier', 'return import(specifier)');
|
|
9
12
|
/**
|
|
10
13
|
* 起 html stack 的 dev server(vite 封装在内,用户不感知)。
|
|
11
14
|
* 热更新走 vite 原生 HMR/WS。
|
|
12
15
|
*/
|
|
13
16
|
async function startDevServer(opts = {}) {
|
|
14
|
-
const { createServer } = await
|
|
17
|
+
const { createServer } = await importESM('vite');
|
|
15
18
|
const server = await createServer((0, vite_config_1.createHtmlViteConfig)(opts));
|
|
16
19
|
await server.listen();
|
|
17
20
|
server.printUrls();
|
package/lib/minify.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.minifyHtmlWithHbsProtection = minifyHtmlWithHbsProtection;
|
|
4
|
+
const html_minifier_terser_1 = require("html-minifier-terser");
|
|
4
5
|
/**
|
|
5
6
|
* 压缩 HTML,同时保护 HBS(Handlebars)占位符。
|
|
6
7
|
*
|
|
@@ -11,14 +12,13 @@ exports.minifyHtmlWithHbsProtection = minifyHtmlWithHbsProtection;
|
|
|
11
12
|
* 行为对齐 `@lark-apaas/coding-preset-vite-react` 的 minifyHtmlWithHbsProtection。
|
|
12
13
|
*/
|
|
13
14
|
async function minifyHtmlWithHbsProtection(html) {
|
|
14
|
-
const { minify } = await import('html-minifier-terser');
|
|
15
15
|
const placeholders = [];
|
|
16
|
-
let processed = html.replace(/\{\{\{?.+?\}\}\}?/g, match => {
|
|
16
|
+
let processed = html.replace(/\{\{\{?.+?\}\}\}?/g, (match) => {
|
|
17
17
|
const i = placeholders.length;
|
|
18
18
|
placeholders.push(match);
|
|
19
19
|
return `__HBS_${i}__`;
|
|
20
20
|
});
|
|
21
|
-
processed = await minify(processed, {
|
|
21
|
+
processed = await (0, html_minifier_terser_1.minify)(processed, {
|
|
22
22
|
collapseWhitespace: true,
|
|
23
23
|
removeComments: true,
|
|
24
24
|
removeRedundantAttributes: true,
|
package/lib/plugins/health.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import type { Plugin } from 'vite'
|
|
2
|
-
'resolution-mode': 'import'
|
|
3
|
-
};
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
3
|
+
* 妙搭沙箱外壳探活接口。协议(飞书文档 - 启动状态梳理):
|
|
4
|
+
* ```jsonc
|
|
5
|
+
* { "ready": true, "status": "ready" }
|
|
6
|
+
* ```
|
|
7
|
+
* - 无法访问 → 服务启动中,超时后流转为「应用启动失败」
|
|
8
|
+
* - status: pending → 应用启动中
|
|
9
|
+
* - status: ready → 流转到前端「应用加载中」
|
|
10
|
+
* - status: server_failed → 应用启动失败
|
|
11
|
+
*
|
|
12
|
+
* 本栈是 client-only,没有 Node 后端可等,dev server 起来即 `ready`,
|
|
13
|
+
* **永不产生 `server_failed`**。
|
|
9
14
|
*/
|
|
10
15
|
export declare const HEALTH_ENDPOINT = "/dev/health";
|
|
11
16
|
export declare function healthPlugin(): Plugin;
|
package/lib/plugins/health.js
CHANGED
|
@@ -3,10 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.HEALTH_ENDPOINT = void 0;
|
|
4
4
|
exports.healthPlugin = healthPlugin;
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* 妙搭沙箱外壳探活接口。协议(飞书文档 - 启动状态梳理):
|
|
7
|
+
* ```jsonc
|
|
8
|
+
* { "ready": true, "status": "ready" }
|
|
9
|
+
* ```
|
|
10
|
+
* - 无法访问 → 服务启动中,超时后流转为「应用启动失败」
|
|
11
|
+
* - status: pending → 应用启动中
|
|
12
|
+
* - status: ready → 流转到前端「应用加载中」
|
|
13
|
+
* - status: server_failed → 应用启动失败
|
|
14
|
+
*
|
|
15
|
+
* 本栈是 client-only,没有 Node 后端可等,dev server 起来即 `ready`,
|
|
16
|
+
* **永不产生 `server_failed`**。
|
|
10
17
|
*/
|
|
11
18
|
exports.HEALTH_ENDPOINT = '/dev/health';
|
|
12
19
|
function healthPlugin() {
|
|
@@ -30,7 +37,9 @@ function healthPlugin() {
|
|
|
30
37
|
res.setHeader('Content-Type', 'application/json');
|
|
31
38
|
res.setHeader('Cache-Control', 'no-store');
|
|
32
39
|
res.statusCode = 200;
|
|
33
|
-
|
|
40
|
+
// ready 恒等于 status === 'ready',是 status 的投影而非独立信号
|
|
41
|
+
const status = ready ? 'ready' : 'pending';
|
|
42
|
+
res.end(JSON.stringify({ ready: status === 'ready', status }));
|
|
34
43
|
});
|
|
35
44
|
},
|
|
36
45
|
};
|
package/lib/plugins/inject.d.ts
CHANGED
package/lib/plugins/raw-jsx.d.ts
CHANGED
package/lib/plugins/raw-jsx.js
CHANGED
|
@@ -23,9 +23,7 @@ function rawJsxPlugin(opts) {
|
|
|
23
23
|
const url = (req.url ?? '').split('?')[0];
|
|
24
24
|
if (!url.endsWith('.jsx'))
|
|
25
25
|
return next();
|
|
26
|
-
const stripped = clientBasePath && url.startsWith(clientBasePath)
|
|
27
|
-
? url.slice(clientBasePath.length)
|
|
28
|
-
: url;
|
|
26
|
+
const stripped = clientBasePath && url.startsWith(clientBasePath) ? url.slice(clientBasePath.length) : url;
|
|
29
27
|
const filePath = node_path_1.default.join(root, stripped);
|
|
30
28
|
try {
|
|
31
29
|
const content = node_fs_1.default.readFileSync(filePath, 'utf-8');
|
|
@@ -9,15 +9,28 @@ exports.viteClientPatchPlugin = viteClientPatchPlugin;
|
|
|
9
9
|
* 但 reload 用的是浏览器 HTTP/2 连接池里复用的休眠前死连接,
|
|
10
10
|
* 导致 ~18s `ERR_CONNECTION_TIMED_OUT` 白屏。
|
|
11
11
|
*
|
|
12
|
-
* 解:transform 拦截 `vite/dist/client/client.mjs
|
|
13
|
-
*
|
|
12
|
+
* 解:transform 拦截 `vite/dist/client/client.mjs`,把 reload 换成 WS 重连——重连与
|
|
13
|
+
* 探活同为 WebSocket 通道,不碰 HTTP/2 连接池,上述白屏不再触发;失败只打 warn,
|
|
14
|
+
* 由用户决定是否刷新。顺带保住探活地址上的 `sandbox_token`:Vite 原本 `url.search = ""`
|
|
15
|
+
* 会把它一起抹掉,导致探活恒被沙箱网关 401(`ws_proxy_service.go` 的 `token == ""`)。
|
|
14
16
|
*
|
|
15
17
|
* 仅 dev(`apply: 'serve'`)。
|
|
16
18
|
*
|
|
17
19
|
* 行为对齐 `@lark-apaas/coding-preset-vite-react` 的同名插件 —— html stack 跑在
|
|
18
20
|
* 同一套沙箱反代链路里,重连白屏与 WS 鉴权两个问题同样成立。
|
|
19
21
|
*/
|
|
20
|
-
const
|
|
22
|
+
const DISCONNECT_RECOVERY_PATTERN = /url\.search = "";\r?\n(\s*)await waitForSuccessfulPing\(url\.href\);\r?\n\s*location\.reload\(\);/;
|
|
23
|
+
/**
|
|
24
|
+
* 匹配 transport 里的 HMR 心跳定时器:
|
|
25
|
+
* pingIntervalId = setInterval(() => { ... }, pingInterval);
|
|
26
|
+
* Vite 被动断开只走 close → `onDisconnection()`,不清定时器;且 `pingIntervalId` 被下次
|
|
27
|
+
* connect 覆盖后再也清不掉,重连后每断一次泄漏一个。改成 IIFE 内的局部 id 自清(不能清
|
|
28
|
+
* 共享变量,会误杀新连接的心跳;用 IIFE 而非 `const` 声明,避免注入点落在无大括号的
|
|
29
|
+
* 语句位置时产生语法错误)。已实测 Vite 7.3.x / 8.0.x 文本一致。
|
|
30
|
+
*
|
|
31
|
+
* 本包没有其它 preset 的 patch 3,此处仍编号 patch 4 以便跨包对齐。
|
|
32
|
+
*/
|
|
33
|
+
const PING_INTERVAL_LEAK_PATTERN = /pingIntervalId = setInterval\(\(\) => \{\s*if \(socket\.readyState === socket\.OPEN\) socket\.send\(JSON\.stringify\(\{ type: "ping" \}\)\);\s*\}, pingInterval\);/;
|
|
21
34
|
function viteClientPatchPlugin() {
|
|
22
35
|
return {
|
|
23
36
|
name: 'miaoda-html-vite-client-patch',
|
|
@@ -27,12 +40,19 @@ function viteClientPatchPlugin() {
|
|
|
27
40
|
if (!id.includes('vite/dist/client/client.mjs'))
|
|
28
41
|
return;
|
|
29
42
|
let patched = code;
|
|
30
|
-
// patch 1:
|
|
31
|
-
if (
|
|
32
|
-
patched = patched.replace(
|
|
43
|
+
// patch 1: 重写断线恢复——探活保住 sandbox_token,探活成功后重连(失败只打 warn)
|
|
44
|
+
if (DISCONNECT_RECOVERY_PATTERN.test(patched)) {
|
|
45
|
+
patched = patched.replace(DISCONNECT_RECOVERY_PATTERN, (_, indent) => 'const __st = url.searchParams.get("sandbox_token");\n' +
|
|
46
|
+
indent +
|
|
47
|
+
'url.search = __st ? "?sandbox_token=" + encodeURIComponent(__st) : "";\n' +
|
|
48
|
+
indent +
|
|
49
|
+
'await waitForSuccessfulPing(url.href);\n' +
|
|
50
|
+
indent +
|
|
51
|
+
'transport.connect(createHMRHandler(handleMessage)).catch((e) => { ' +
|
|
52
|
+
"console.warn('[miaoda-html-vite-client-patch] HMR reconnect failed (ignored):', e); });");
|
|
33
53
|
}
|
|
34
54
|
else {
|
|
35
|
-
console.warn('[miaoda-html-vite-client-patch]
|
|
55
|
+
console.warn('[miaoda-html-vite-client-patch] disconnect-recovery pattern not found in @vite/client. ' +
|
|
36
56
|
'patch NOT applied — Vite client.mjs may have changed.');
|
|
37
57
|
}
|
|
38
58
|
// patch 2: HMR 长链追加沙箱鉴权 token(gated on MIAODA_HMR_WS_TOKEN)
|
|
@@ -47,6 +67,17 @@ function viteClientPatchPlugin() {
|
|
|
47
67
|
'sandbox_token NOT injected — Vite client.mjs may have changed.');
|
|
48
68
|
}
|
|
49
69
|
}
|
|
70
|
+
// patch 4: 心跳定时器自清,修 patch 1 自动重连放大的 Vite 定时器泄漏
|
|
71
|
+
// (本包无 patch 3,编号与其它 preset 对齐,见上方常量注释)
|
|
72
|
+
if (PING_INTERVAL_LEAK_PATTERN.test(patched)) {
|
|
73
|
+
patched = patched.replace(PING_INTERVAL_LEAK_PATTERN, 'pingIntervalId = (() => { const id = setInterval(() => { ' +
|
|
74
|
+
'if (socket.readyState === socket.OPEN) socket.send(JSON.stringify({ type: "ping" })); ' +
|
|
75
|
+
'else clearInterval(id); }, pingInterval); return id; })();');
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.warn('[miaoda-html-vite-client-patch] ping-interval pattern not found in @vite/client; ' +
|
|
79
|
+
'stale heartbeat timers will leak on reconnect — Vite client.mjs may have changed.');
|
|
80
|
+
}
|
|
50
81
|
return patched === code ? undefined : { code: patched };
|
|
51
82
|
},
|
|
52
83
|
};
|
|
@@ -42,7 +42,7 @@ function resolveClientRuntimePath() {
|
|
|
42
42
|
path.resolve(__dirname, '../../src/runtime/ws-watchdog-client.mjs'), // lib/plugins → src/runtime
|
|
43
43
|
path.resolve(__dirname, '../runtime/ws-watchdog-client.mjs'), // src/plugins → src/runtime
|
|
44
44
|
];
|
|
45
|
-
return candidates.find(p => fs.existsSync(p)) ?? null;
|
|
45
|
+
return candidates.find((p) => fs.existsSync(p)) ?? null;
|
|
46
46
|
}
|
|
47
47
|
function wsWatchdogPlugin(options = {}) {
|
|
48
48
|
const { pingEvent = 'ws-watchdog:ping', pongEvent = 'ws-watchdog:pong', clientBasePath = '', } = options;
|
package/lib/vite-config.d.ts
CHANGED
package/lib/vite-config.js
CHANGED
|
@@ -37,9 +37,7 @@ const normalize_base_path_1 = require("./normalize-base-path");
|
|
|
37
37
|
function createHtmlViteConfig(opts = {}) {
|
|
38
38
|
const root = node_path_1.default.resolve(opts.root ?? 'src');
|
|
39
39
|
// Number.isFinite 兜底:?? 只挡 null/undefined,挡不住 NaN(防 opts.port 传入脏值)
|
|
40
|
-
const port = Number.isFinite(opts.port)
|
|
41
|
-
? opts.port
|
|
42
|
-
: Number(process.env.CLIENT_DEV_PORT) || 8001;
|
|
40
|
+
const port = Number.isFinite(opts.port) ? opts.port : Number(process.env.CLIENT_DEV_PORT) || 8001;
|
|
43
41
|
const host = opts.host ?? '0.0.0.0';
|
|
44
42
|
// 沙箱网关把 `/app/<id>` 前缀透传给 vite 时,watchdog 的 <script src>、middleware
|
|
45
43
|
// 匹配路径、以及客户端 import 的 `/@vite/client` 都必须带同一前缀;空串退化到裸路径。
|
|
@@ -65,13 +63,7 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
65
63
|
// overlay: false —— 关掉 vite 的错误红屏遮罩(JS 解析/编译失败时的全屏弹窗)。
|
|
66
64
|
// 沙箱场景不给终端用户看红屏,报错仍保留在浏览器 console。
|
|
67
65
|
hmr: process.env.MIAODA_WS_HOST
|
|
68
|
-
? {
|
|
69
|
-
protocol: 'wss',
|
|
70
|
-
host: process.env.MIAODA_WS_HOST,
|
|
71
|
-
clientPort: 443,
|
|
72
|
-
path: '/ws',
|
|
73
|
-
overlay: false,
|
|
74
|
-
}
|
|
66
|
+
? { protocol: 'wss', host: process.env.MIAODA_WS_HOST, clientPort: 443, path: '/ws', overlay: false }
|
|
75
67
|
: { path: '/ws', overlay: false },
|
|
76
68
|
},
|
|
77
69
|
plugins: [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/coding-html-devserver",
|
|
3
|
-
"version": "0.1.8-alpha.
|
|
3
|
+
"version": "0.1.8-alpha.20260820104214",
|
|
4
4
|
"description": "Dev/build toolchain for the miaoda-coding html (pure HTML) stack. Wraps vite (dev + HMR) behind a bin so the template never sees vite; build is pure-copy + platform-script injection + optional minify.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|