@lark-apaas/coding-html-devserver 0.1.11 → 0.1.12-alpha.20261019123256
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/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/plugins/hmr-token.d.ts +34 -0
- package/lib/plugins/hmr-token.js +108 -0
- package/lib/plugins/vite-client-patch.js +23 -11
- package/lib/vite-config.d.ts +2 -1
- package/lib/vite-config.js +5 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export { healthPlugin, HEALTH_ENDPOINT } from './plugins/health';
|
|
|
22
22
|
export { platformInjectPlugin } from './plugins/inject';
|
|
23
23
|
export type { PlatformInjectPluginOptions } from './plugins/inject';
|
|
24
24
|
export { viteClientPatchPlugin } from './plugins/vite-client-patch';
|
|
25
|
+
export { hmrTokenPlugin } from './plugins/hmr-token';
|
|
25
26
|
export { wsWatchdogPlugin } from './plugins/ws-watchdog';
|
|
26
27
|
export type { WsWatchdogPluginOptions } from './plugins/ws-watchdog';
|
|
27
28
|
export { basePathRewritePlugin } from './plugins/base-path';
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.routesJsonPlugin = exports.basePathRewritePlugin = exports.wsWatchdogPlugin = exports.viteClientPatchPlugin = exports.platformInjectPlugin = exports.HEALTH_ENDPOINT = exports.healthPlugin = exports.serializeTag = exports.injectTags = exports.injectOgMeta = exports.MIAODA_PREVIEW_BRIDGE_URL = exports.previewBridgeTags = exports.slardarTags = exports.buildRenderContext = exports.viewContextTag = exports.PLATFORM_TAGS = exports.injectPlatformTags = exports.normalizeBasePath = exports.ROUTES_JSON_FILENAME = exports.routePathFromFile = exports.resolveRoutesJson = exports.generateRoutes = exports.minifyHtmlWithHbsProtection = exports.buildStatic = exports.createHtmlViteConfig = exports.startDevServer = void 0;
|
|
3
|
+
exports.routesJsonPlugin = exports.basePathRewritePlugin = exports.wsWatchdogPlugin = exports.hmrTokenPlugin = exports.viteClientPatchPlugin = exports.platformInjectPlugin = exports.HEALTH_ENDPOINT = exports.healthPlugin = exports.serializeTag = exports.injectTags = exports.injectOgMeta = exports.MIAODA_PREVIEW_BRIDGE_URL = exports.previewBridgeTags = exports.slardarTags = exports.buildRenderContext = exports.viewContextTag = exports.PLATFORM_TAGS = exports.injectPlatformTags = exports.normalizeBasePath = exports.ROUTES_JSON_FILENAME = exports.routePathFromFile = exports.resolveRoutesJson = exports.generateRoutes = exports.minifyHtmlWithHbsProtection = exports.buildStatic = exports.createHtmlViteConfig = exports.startDevServer = void 0;
|
|
4
4
|
var dev_1 = require("./dev");
|
|
5
5
|
Object.defineProperty(exports, "startDevServer", { enumerable: true, get: function () { return dev_1.startDevServer; } });
|
|
6
6
|
var vite_config_1 = require("./vite-config");
|
|
@@ -39,6 +39,8 @@ var inject_1 = require("./plugins/inject");
|
|
|
39
39
|
Object.defineProperty(exports, "platformInjectPlugin", { enumerable: true, get: function () { return inject_1.platformInjectPlugin; } });
|
|
40
40
|
var vite_client_patch_1 = require("./plugins/vite-client-patch");
|
|
41
41
|
Object.defineProperty(exports, "viteClientPatchPlugin", { enumerable: true, get: function () { return vite_client_patch_1.viteClientPatchPlugin; } });
|
|
42
|
+
var hmr_token_1 = require("./plugins/hmr-token");
|
|
43
|
+
Object.defineProperty(exports, "hmrTokenPlugin", { enumerable: true, get: function () { return hmr_token_1.hmrTokenPlugin; } });
|
|
42
44
|
var ws_watchdog_1 = require("./plugins/ws-watchdog");
|
|
43
45
|
Object.defineProperty(exports, "wsWatchdogPlugin", { enumerable: true, get: function () { return ws_watchdog_1.wsWatchdogPlugin; } });
|
|
44
46
|
var base_path_1 = require("./plugins/base-path");
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/** 浏览器侧全局变量名:本插件写入,vite-client-patch 的 patch 2 在建链时读取。 */
|
|
3
|
+
export declare const HMR_WS_TOKEN_GLOBAL = "__MIAODA_HMR_WS_TOKEN__";
|
|
4
|
+
/**
|
|
5
|
+
* 读取当前最新的预览 WS token。
|
|
6
|
+
*
|
|
7
|
+
* `.force/environment/env` 由 sandbox_console ReInitSandboxEnv 整体覆盖写入(`export KEY="VALUE"`),
|
|
8
|
+
* 沙箱暂停恢复时会续签 token 写进该文件;但本进程的 `process.env` 是启动那一刻 bashrc source
|
|
9
|
+
* 出来的快照,不会跟着变。所以每次现读文件,读不到(本地开发 / 文件未写入)才退回 `process.env`。
|
|
10
|
+
*
|
|
11
|
+
* ⚠️ 该文件混有 AK/SK 等凭证,只抠 MIAODA_HMR_WS_TOKEN 这一行,任何其他内容都不得外传或打日志。
|
|
12
|
+
*/
|
|
13
|
+
export declare function readHmrWsToken(): string;
|
|
14
|
+
export interface HmrTokenPluginOptions {
|
|
15
|
+
/**
|
|
16
|
+
* 客户端 base path(如 `/app/app_xxx`)。沙箱网关把完整路径透传给 Vite,
|
|
17
|
+
* middleware 匹配和注入的 `<script src>` 都必须带 base;空串退化到裸路径。
|
|
18
|
+
*/
|
|
19
|
+
clientBasePath?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 预览 HMR 长链 token 改为页面级现取(沙箱暂停恢复后无需重启 dev server)。
|
|
23
|
+
*
|
|
24
|
+
* - HTML 里只注入固定的 `<script src=".../@miaoda-hmr-token.js">`,不含 token 本身——
|
|
25
|
+
* fullstack 形态 HTML 由 html-output 落盘后交给 Nest 渲染,token 写进 HTML 既会过期也会落盘。
|
|
26
|
+
* - 该脚本每次请求现读 env 文件,返回 `window.__MIAODA_HMR_WS_TOKEN__ = "..."`,`no-store` 禁缓存。
|
|
27
|
+
* - 普通 script(非 module)在解析时同步执行,早于 module 类型的 `@vite/client`,
|
|
28
|
+
* 保证 vite-client-patch 改写后的建链代码读得到。
|
|
29
|
+
*
|
|
30
|
+
* 须与 viteClientPatchPlugin 同时注册(patch 2 读取本插件写入的全局变量)。
|
|
31
|
+
* 仅 dev(`apply: 'serve'`),token 绝不进 build 产物。
|
|
32
|
+
*/
|
|
33
|
+
export declare function hmrTokenPlugin(options?: HmrTokenPluginOptions): Plugin;
|
|
34
|
+
export default hmrTokenPlugin;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.HMR_WS_TOKEN_GLOBAL = void 0;
|
|
37
|
+
exports.readHmrWsToken = readHmrWsToken;
|
|
38
|
+
exports.hmrTokenPlugin = hmrTokenPlugin;
|
|
39
|
+
const fs = __importStar(require("node:fs"));
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
41
|
+
const TOKEN_SCRIPT_PATH = '/@miaoda-hmr-token.js';
|
|
42
|
+
/** 浏览器侧全局变量名:本插件写入,vite-client-patch 的 patch 2 在建链时读取。 */
|
|
43
|
+
exports.HMR_WS_TOKEN_GLOBAL = '__MIAODA_HMR_WS_TOKEN__';
|
|
44
|
+
const ENV_KEY = 'MIAODA_HMR_WS_TOKEN';
|
|
45
|
+
const ENV_LINE_PATTERN = /^export MIAODA_HMR_WS_TOKEN="([^"\n]*)"\s*$/m;
|
|
46
|
+
/**
|
|
47
|
+
* 沙箱镜像 `ENV WORKSPACE_DIR="/home/gem/workspace"`(sandbox_console/sandbox/Dockerfile),
|
|
48
|
+
* 经 supervisor → force-server → `bash -i -c "npm run dev"` 一路继承到本进程。
|
|
49
|
+
* 兜底值与 ide-vscode constants.ts / sandbox_console monitor 口径一致。
|
|
50
|
+
* 不用 cwd 推:工作目录可能是 WORKSPACE_CODE_DIR 或 dev 脚本切进去的子目录。
|
|
51
|
+
*/
|
|
52
|
+
const DEFAULT_WORKSPACE_DIR = '/home/gem/workspace';
|
|
53
|
+
/**
|
|
54
|
+
* 读取当前最新的预览 WS token。
|
|
55
|
+
*
|
|
56
|
+
* `.force/environment/env` 由 sandbox_console ReInitSandboxEnv 整体覆盖写入(`export KEY="VALUE"`),
|
|
57
|
+
* 沙箱暂停恢复时会续签 token 写进该文件;但本进程的 `process.env` 是启动那一刻 bashrc source
|
|
58
|
+
* 出来的快照,不会跟着变。所以每次现读文件,读不到(本地开发 / 文件未写入)才退回 `process.env`。
|
|
59
|
+
*
|
|
60
|
+
* ⚠️ 该文件混有 AK/SK 等凭证,只抠 MIAODA_HMR_WS_TOKEN 这一行,任何其他内容都不得外传或打日志。
|
|
61
|
+
*/
|
|
62
|
+
function readHmrWsToken() {
|
|
63
|
+
const envFile = path.join(process.env.WORKSPACE_DIR || DEFAULT_WORKSPACE_DIR, '.force/environment/env');
|
|
64
|
+
try {
|
|
65
|
+
const match = ENV_LINE_PATTERN.exec(fs.readFileSync(envFile, 'utf-8'));
|
|
66
|
+
if (match)
|
|
67
|
+
return match[1];
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// 文件不存在或不可读:走 process.env 兜底
|
|
71
|
+
}
|
|
72
|
+
return process.env[ENV_KEY] ?? '';
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 预览 HMR 长链 token 改为页面级现取(沙箱暂停恢复后无需重启 dev server)。
|
|
76
|
+
*
|
|
77
|
+
* - HTML 里只注入固定的 `<script src=".../@miaoda-hmr-token.js">`,不含 token 本身——
|
|
78
|
+
* fullstack 形态 HTML 由 html-output 落盘后交给 Nest 渲染,token 写进 HTML 既会过期也会落盘。
|
|
79
|
+
* - 该脚本每次请求现读 env 文件,返回 `window.__MIAODA_HMR_WS_TOKEN__ = "..."`,`no-store` 禁缓存。
|
|
80
|
+
* - 普通 script(非 module)在解析时同步执行,早于 module 类型的 `@vite/client`,
|
|
81
|
+
* 保证 vite-client-patch 改写后的建链代码读得到。
|
|
82
|
+
*
|
|
83
|
+
* 须与 viteClientPatchPlugin 同时注册(patch 2 读取本插件写入的全局变量)。
|
|
84
|
+
* 仅 dev(`apply: 'serve'`),token 绝不进 build 产物。
|
|
85
|
+
*/
|
|
86
|
+
function hmrTokenPlugin(options = {}) {
|
|
87
|
+
const scriptPath = (options.clientBasePath ?? '') + TOKEN_SCRIPT_PATH;
|
|
88
|
+
return {
|
|
89
|
+
name: 'miaoda-hmr-token',
|
|
90
|
+
apply: 'serve',
|
|
91
|
+
configureServer(server) {
|
|
92
|
+
server.middlewares.use((req, res, next) => {
|
|
93
|
+
if (req.url?.split('?')[0] !== scriptPath)
|
|
94
|
+
return next();
|
|
95
|
+
res.setHeader('Content-Type', 'application/javascript');
|
|
96
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
97
|
+
res.end(`window.${exports.HMR_WS_TOKEN_GLOBAL} = ${JSON.stringify(readHmrWsToken())};`);
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
transformIndexHtml() {
|
|
101
|
+
const tags = [
|
|
102
|
+
{ tag: 'script', attrs: { src: scriptPath }, injectTo: 'head-prepend' },
|
|
103
|
+
];
|
|
104
|
+
return tags;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
exports.default = hmrTokenPlugin;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.viteClientPatchPlugin = viteClientPatchPlugin;
|
|
4
|
+
const hmr_token_1 = require("./hmr-token");
|
|
4
5
|
/**
|
|
5
6
|
* 抑制 `@vite/client` 在 HMR WS 重连成功后的 `location.reload()`。
|
|
6
7
|
*
|
|
@@ -20,6 +21,16 @@ exports.viteClientPatchPlugin = viteClientPatchPlugin;
|
|
|
20
21
|
* 同一套沙箱反代链路里,重连白屏与 WS 鉴权两个问题同样成立。
|
|
21
22
|
*/
|
|
22
23
|
const DISCONNECT_RECOVERY_PATTERN = /url\.search = "";\r?\n(\s*)await waitForSuccessfulPing\(url\.href\);\r?\n\s*location\.reload\(\);/;
|
|
24
|
+
/**
|
|
25
|
+
* 写进 @vite/client 建链 URL 的取值代码(在浏览器里执行):建链时读页面上的最新 token,
|
|
26
|
+
* 有值才追加 `&sandbox_token=`。页面上的值由 hmrTokenPlugin 在每次页面加载时现读 env 文件写入。
|
|
27
|
+
*/
|
|
28
|
+
const TOKEN_REF = `globalThis.${hmr_token_1.HMR_WS_TOKEN_GLOBAL}`;
|
|
29
|
+
const SANDBOX_TOKEN_EXPR = '${' +
|
|
30
|
+
TOKEN_REF +
|
|
31
|
+
' ? "&sandbox_token=" + encodeURIComponent(' +
|
|
32
|
+
TOKEN_REF +
|
|
33
|
+
') : ""}';
|
|
23
34
|
/**
|
|
24
35
|
* 匹配 transport 里的 HMR 心跳定时器:
|
|
25
36
|
* pingIntervalId = setInterval(() => { ... }, pingInterval);
|
|
@@ -63,17 +74,18 @@ function viteClientPatchPlugin() {
|
|
|
63
74
|
console.warn('[miaoda-html-vite-client-patch] disconnect-recovery pattern not found in @vite/client. ' +
|
|
64
75
|
'patch NOT applied — Vite client.mjs may have changed.');
|
|
65
76
|
}
|
|
66
|
-
// patch 2: HMR 长链追加沙箱鉴权 token
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
+
// patch 2: HMR 长链追加沙箱鉴权 token——不写死 token 值,写入“建连时从页面取最新值”的代码。
|
|
78
|
+
// token 写死在 client.mjs 里会被 Vite transformResult + 浏览器 ETag 缓存,沙箱暂停恢复续签后
|
|
79
|
+
// 不重启 dev server 就换不掉;改成运行时取值后 client.mjs 内容不随 token 变化。
|
|
80
|
+
if (patched.includes(TOKEN_REF)) {
|
|
81
|
+
// 已 patch,保持幂等。
|
|
82
|
+
}
|
|
83
|
+
else if (patched.includes('?token=${wsToken}')) {
|
|
84
|
+
patched = patched.replace(/\?token=\$\{wsToken\}/g, () => '?token=${wsToken}' + SANDBOX_TOKEN_EXPR);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
console.warn('[miaoda-html-vite-client-patch] WS url pattern `?token=${wsToken}` not found; ' +
|
|
88
|
+
'sandbox_token NOT injected — Vite client.mjs may have changed.');
|
|
77
89
|
}
|
|
78
90
|
// patch 4: 心跳定时器自清,修 patch 1 自动重连放大的 Vite 定时器泄漏
|
|
79
91
|
// (本包无 patch 3,编号与其它 preset 对齐,见上方常量注释)
|
package/lib/vite-config.d.ts
CHANGED
|
@@ -24,7 +24,8 @@ export interface HtmlViteOptions {
|
|
|
24
24
|
*
|
|
25
25
|
* 沙箱 WS 韧性(dev only,对齐 coding-preset-vite-react):
|
|
26
26
|
* - `viteClientPatchPlugin`:删 WS 重连后的 `location.reload()`(休眠唤醒白屏),
|
|
27
|
-
*
|
|
27
|
+
* 并让 HMR 长链 URL 在建链时取页面上的 `sandbox_token`(穿网关鉴权)。
|
|
28
|
+
* - `hmrTokenPlugin`:每次页面加载现读 env 文件下发最新 token(沙箱暂停恢复续签后无需重启)。
|
|
28
29
|
* - `wsWatchdogPlugin`:检测僵尸 WS,判死后 postMessage 通知沙箱外壳。
|
|
29
30
|
*/
|
|
30
31
|
export declare function createHtmlViteConfig(opts?: HtmlViteOptions): InlineConfig;
|
package/lib/vite-config.js
CHANGED
|
@@ -8,6 +8,7 @@ const node_path_1 = __importDefault(require("node:path"));
|
|
|
8
8
|
const health_1 = require("./plugins/health");
|
|
9
9
|
const inject_1 = require("./plugins/inject");
|
|
10
10
|
const vite_client_patch_1 = require("./plugins/vite-client-patch");
|
|
11
|
+
const hmr_token_1 = require("./plugins/hmr-token");
|
|
11
12
|
const ws_watchdog_1 = require("./plugins/ws-watchdog");
|
|
12
13
|
const base_path_1 = require("./plugins/base-path");
|
|
13
14
|
const runtime_url_fix_1 = require("./plugins/runtime-url-fix");
|
|
@@ -31,7 +32,8 @@ const normalize_base_path_1 = require("./normalize-base-path");
|
|
|
31
32
|
*
|
|
32
33
|
* 沙箱 WS 韧性(dev only,对齐 coding-preset-vite-react):
|
|
33
34
|
* - `viteClientPatchPlugin`:删 WS 重连后的 `location.reload()`(休眠唤醒白屏),
|
|
34
|
-
*
|
|
35
|
+
* 并让 HMR 长链 URL 在建链时取页面上的 `sandbox_token`(穿网关鉴权)。
|
|
36
|
+
* - `hmrTokenPlugin`:每次页面加载现读 env 文件下发最新 token(沙箱暂停恢复续签后无需重启)。
|
|
35
37
|
* - `wsWatchdogPlugin`:检测僵尸 WS,判死后 postMessage 通知沙箱外壳。
|
|
36
38
|
*/
|
|
37
39
|
function createHtmlViteConfig(opts = {}) {
|
|
@@ -72,6 +74,8 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
72
74
|
(0, inject_1.platformInjectPlugin)({ appId, base }),
|
|
73
75
|
// dev only:apply:'serve' 自带,build 不挂
|
|
74
76
|
(0, vite_client_patch_1.viteClientPatchPlugin)(),
|
|
77
|
+
// patch 2 建链时读取的 token 由 hmrTokenPlugin 下发,二者须同时注册
|
|
78
|
+
(0, hmr_token_1.hmrTokenPlugin)({ clientBasePath }),
|
|
75
79
|
(0, ws_watchdog_1.wsWatchdogPlugin)({ clientBasePath }),
|
|
76
80
|
// base 非 `/` 时,把无尾斜杠的 `/app/<id>` 内部 rewrite 成带尾斜杠(自守卫空 base)
|
|
77
81
|
(0, base_path_1.basePathRewritePlugin)(clientBasePath),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/coding-html-devserver",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12-alpha.20261019123256",
|
|
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",
|