@lark-apaas/coding-html-devserver 0.1.6-alpha.0 → 0.1.6-alpha.2
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.
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* 撕掉 Vite 给「运行时/后端绝对 URL」误加的 base 前缀(dev only)。
|
|
4
|
+
*
|
|
5
|
+
* dev 设了 `base = ${CLIENT_BASE_PATH}/` 让 vite 在前缀下服务 index/assets/@vite/client。
|
|
6
|
+
* 副作用:vite 的 HTML 转换会给**静态 HTML 里所有 `/` 开头的资源 URL**(img[src] /
|
|
7
|
+
* link[href] / CSS `url()` 等)无脑加 base 前缀,包括本不该被前缀污染的后端运行时地址:
|
|
8
|
+
*
|
|
9
|
+
* 源 `/spark/app/<id>/runtime/api/v1/storage/object/...`(历史格式)
|
|
10
|
+
* vite 改写后 `/app/<id>/spark/app/<id>/runtime/...` ← 多一级,网关路由不到
|
|
11
|
+
* 源 `/app/<id>/runtime/api/v1/storage/object/...`(新格式,已带前缀)
|
|
12
|
+
* vite 改写后 `/app/<id>/app/<id>/runtime/...` ← 同样 double
|
|
13
|
+
*
|
|
14
|
+
* 这类 URL 是打到运行时/后端(存储对象、account、permissions 等)的,都带 `runtime` /
|
|
15
|
+
* `__runtime__` 段;而 app **自有静态资源**(`/assets/x.png`)不带该段、且**需要** base
|
|
16
|
+
* 前缀(vite dev 在前缀下服务它)—— 所以只能按 `runtime` 段精确豁免,不能一刀切撕所有绝对
|
|
17
|
+
* URL 的前缀。
|
|
18
|
+
*
|
|
19
|
+
* 走 `transformIndexHtml` 的 `order: 'post'`,在 vite 核心 HTML 改写之后跑,把「以 base 开头
|
|
20
|
+
* 且 remainder 含 runtime 段」的 URL 前缀去掉,还原成作者写的形态。build 是纯拷贝、不经 vite
|
|
21
|
+
* 改写,无需本插件 —— dev 修完正好与 prod(= 源码)一致。
|
|
22
|
+
*
|
|
23
|
+
* 匹配边界:URL 出现在属性值 / `url()` 起始(前置字符为 `="'(` 或空白),remainder 以 `/`
|
|
24
|
+
* 开头并含 `/runtime/` 或 `/__runtime__/` 段。`<base href>`、`/@vite/client` 等不含 runtime
|
|
25
|
+
* 段,天然不受影响。
|
|
26
|
+
*/
|
|
27
|
+
export declare function stripBaseFromRuntimeUrls(html: string, clientBasePath: string): string;
|
|
28
|
+
export declare function runtimeUrlFixPlugin(clientBasePath: string): Plugin;
|
|
29
|
+
export default runtimeUrlFixPlugin;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stripBaseFromRuntimeUrls = stripBaseFromRuntimeUrls;
|
|
4
|
+
exports.runtimeUrlFixPlugin = runtimeUrlFixPlugin;
|
|
5
|
+
/**
|
|
6
|
+
* 撕掉 Vite 给「运行时/后端绝对 URL」误加的 base 前缀(dev only)。
|
|
7
|
+
*
|
|
8
|
+
* dev 设了 `base = ${CLIENT_BASE_PATH}/` 让 vite 在前缀下服务 index/assets/@vite/client。
|
|
9
|
+
* 副作用:vite 的 HTML 转换会给**静态 HTML 里所有 `/` 开头的资源 URL**(img[src] /
|
|
10
|
+
* link[href] / CSS `url()` 等)无脑加 base 前缀,包括本不该被前缀污染的后端运行时地址:
|
|
11
|
+
*
|
|
12
|
+
* 源 `/spark/app/<id>/runtime/api/v1/storage/object/...`(历史格式)
|
|
13
|
+
* vite 改写后 `/app/<id>/spark/app/<id>/runtime/...` ← 多一级,网关路由不到
|
|
14
|
+
* 源 `/app/<id>/runtime/api/v1/storage/object/...`(新格式,已带前缀)
|
|
15
|
+
* vite 改写后 `/app/<id>/app/<id>/runtime/...` ← 同样 double
|
|
16
|
+
*
|
|
17
|
+
* 这类 URL 是打到运行时/后端(存储对象、account、permissions 等)的,都带 `runtime` /
|
|
18
|
+
* `__runtime__` 段;而 app **自有静态资源**(`/assets/x.png`)不带该段、且**需要** base
|
|
19
|
+
* 前缀(vite dev 在前缀下服务它)—— 所以只能按 `runtime` 段精确豁免,不能一刀切撕所有绝对
|
|
20
|
+
* URL 的前缀。
|
|
21
|
+
*
|
|
22
|
+
* 走 `transformIndexHtml` 的 `order: 'post'`,在 vite 核心 HTML 改写之后跑,把「以 base 开头
|
|
23
|
+
* 且 remainder 含 runtime 段」的 URL 前缀去掉,还原成作者写的形态。build 是纯拷贝、不经 vite
|
|
24
|
+
* 改写,无需本插件 —— dev 修完正好与 prod(= 源码)一致。
|
|
25
|
+
*
|
|
26
|
+
* 匹配边界:URL 出现在属性值 / `url()` 起始(前置字符为 `="'(` 或空白),remainder 以 `/`
|
|
27
|
+
* 开头并含 `/runtime/` 或 `/__runtime__/` 段。`<base href>`、`/@vite/client` 等不含 runtime
|
|
28
|
+
* 段,天然不受影响。
|
|
29
|
+
*/
|
|
30
|
+
function stripBaseFromRuntimeUrls(html, clientBasePath) {
|
|
31
|
+
const base = clientBasePath.replace(/\/+$/, '');
|
|
32
|
+
if (!base)
|
|
33
|
+
return html;
|
|
34
|
+
const esc = base.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
35
|
+
const re = new RegExp(`([="'(\\s])${esc}(/(?:[^"'()\\s]*/)?(?:__runtime__|runtime)/[^"'()\\s]*)`, 'g');
|
|
36
|
+
return html.replace(re, '$1$2');
|
|
37
|
+
}
|
|
38
|
+
function runtimeUrlFixPlugin(clientBasePath) {
|
|
39
|
+
return {
|
|
40
|
+
name: 'miaoda-html-runtime-url-fix',
|
|
41
|
+
apply: 'serve',
|
|
42
|
+
transformIndexHtml: {
|
|
43
|
+
order: 'post',
|
|
44
|
+
handler(html) {
|
|
45
|
+
return stripBaseFromRuntimeUrls(html, clientBasePath);
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
exports.default = runtimeUrlFixPlugin;
|
package/lib/vite-config.d.ts
CHANGED
|
@@ -16,8 +16,11 @@ export interface HtmlViteOptions {
|
|
|
16
16
|
* 配套 `basePathRewritePlugin` 处理无尾斜杠(`/app/<id>` → `/app/<id>/`)的内部 rewrite。
|
|
17
17
|
* 无 `CLIENT_BASE_PATH` 时 base 退化为 `/`(本地裸 dev 不受影响)。
|
|
18
18
|
* - HMR 走 vite 的 WS:`MIAODA_WS_HOST` 存在时切独立 WS 域名(对齐 vite-react 网关方案)。
|
|
19
|
+
* 两种分支都设 `overlay: false`,关掉 JS 解析/编译失败时的错误红屏遮罩(沙箱不给终端
|
|
20
|
+
* 用户看红屏,报错仍在浏览器 console)。
|
|
19
21
|
* - `appType: 'mpa'`:纯静态多页,不做 SPA history fallback。
|
|
20
|
-
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
22
|
+
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
23
|
+
* + runtime-url-fix(撕掉 vite 给后端运行时绝对 URL 误加的 base 前缀)。
|
|
21
24
|
*
|
|
22
25
|
* 沙箱 WS 韧性(dev only,对齐 coding-preset-vite-react):
|
|
23
26
|
* - `viteClientPatchPlugin`:删 WS 重连后的 `location.reload()`(休眠唤醒白屏),
|
package/lib/vite-config.js
CHANGED
|
@@ -10,6 +10,7 @@ const inject_1 = require("./plugins/inject");
|
|
|
10
10
|
const vite_client_patch_1 = require("./plugins/vite-client-patch");
|
|
11
11
|
const ws_watchdog_1 = require("./plugins/ws-watchdog");
|
|
12
12
|
const base_path_1 = require("./plugins/base-path");
|
|
13
|
+
const runtime_url_fix_1 = require("./plugins/runtime-url-fix");
|
|
13
14
|
const raw_jsx_1 = require("./plugins/raw-jsx");
|
|
14
15
|
const routes_json_1 = require("./plugins/routes-json");
|
|
15
16
|
const normalize_base_path_1 = require("./normalize-base-path");
|
|
@@ -22,8 +23,11 @@ const normalize_base_path_1 = require("./normalize-base-path");
|
|
|
22
23
|
* 配套 `basePathRewritePlugin` 处理无尾斜杠(`/app/<id>` → `/app/<id>/`)的内部 rewrite。
|
|
23
24
|
* 无 `CLIENT_BASE_PATH` 时 base 退化为 `/`(本地裸 dev 不受影响)。
|
|
24
25
|
* - HMR 走 vite 的 WS:`MIAODA_WS_HOST` 存在时切独立 WS 域名(对齐 vite-react 网关方案)。
|
|
26
|
+
* 两种分支都设 `overlay: false`,关掉 JS 解析/编译失败时的错误红屏遮罩(沙箱不给终端
|
|
27
|
+
* 用户看红屏,报错仍在浏览器 console)。
|
|
25
28
|
* - `appType: 'mpa'`:纯静态多页,不做 SPA history fallback。
|
|
26
|
-
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
29
|
+
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
30
|
+
* + runtime-url-fix(撕掉 vite 给后端运行时绝对 URL 误加的 base 前缀)。
|
|
27
31
|
*
|
|
28
32
|
* 沙箱 WS 韧性(dev only,对齐 coding-preset-vite-react):
|
|
29
33
|
* - `viteClientPatchPlugin`:删 WS 重连后的 `location.reload()`(休眠唤醒白屏),
|
|
@@ -56,9 +60,11 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
56
60
|
strictPort: true,
|
|
57
61
|
cors: true,
|
|
58
62
|
allowedHosts: true,
|
|
63
|
+
// overlay: false —— 关掉 vite 的错误红屏遮罩(JS 解析/编译失败时的全屏弹窗)。
|
|
64
|
+
// 沙箱场景不给终端用户看红屏,报错仍保留在浏览器 console。
|
|
59
65
|
hmr: process.env.MIAODA_WS_HOST
|
|
60
|
-
? { protocol: 'wss', host: process.env.MIAODA_WS_HOST, clientPort: 443, path: '/ws' }
|
|
61
|
-
: { path: '/ws' },
|
|
66
|
+
? { protocol: 'wss', host: process.env.MIAODA_WS_HOST, clientPort: 443, path: '/ws', overlay: false }
|
|
67
|
+
: { path: '/ws', overlay: false },
|
|
62
68
|
},
|
|
63
69
|
plugins: [
|
|
64
70
|
(0, raw_jsx_1.rawJsxPlugin)({ root, clientBasePath }),
|
|
@@ -69,6 +75,8 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
69
75
|
(0, ws_watchdog_1.wsWatchdogPlugin)({ clientBasePath }),
|
|
70
76
|
// base 非 `/` 时,把无尾斜杠的 `/app/<id>` 内部 rewrite 成带尾斜杠(自守卫空 base)
|
|
71
77
|
(0, base_path_1.basePathRewritePlugin)(clientBasePath),
|
|
78
|
+
// post transform:撕掉 vite 给运行时/后端绝对 URL(含 runtime 段)误加的 base 前缀
|
|
79
|
+
(0, runtime_url_fix_1.runtimeUrlFixPlugin)(clientBasePath),
|
|
72
80
|
// 在 `${base}routes.json` 提供路由清单(与 build 产物一致)
|
|
73
81
|
(0, routes_json_1.routesJsonPlugin)({ root, clientBasePath }),
|
|
74
82
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/coding-html-devserver",
|
|
3
|
-
"version": "0.1.6-alpha.
|
|
3
|
+
"version": "0.1.6-alpha.2",
|
|
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",
|