@lark-apaas/coding-html-devserver 0.1.5 → 0.1.6-alpha.1
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/build.js +2 -0
- package/lib/plugins/inject.d.ts +2 -0
- package/lib/plugins/inject.js +1 -0
- package/lib/plugins/runtime-url-fix.d.ts +29 -0
- package/lib/plugins/runtime-url-fix.js +50 -0
- package/lib/transform/base-tag.d.ts +2 -0
- package/lib/transform/base-tag.js +33 -0
- package/lib/transform/index.d.ts +8 -2
- package/lib/transform/index.js +5 -2
- package/lib/vite-config.d.ts +2 -1
- package/lib/vite-config.js +6 -2
- package/package.json +1 -1
package/lib/build.js
CHANGED
|
@@ -34,6 +34,8 @@ async function buildStatic(opts = {}) {
|
|
|
34
34
|
if (!HTML_EXT.has(node_path_1.default.extname(file).toLowerCase()))
|
|
35
35
|
continue;
|
|
36
36
|
let html = await node_fs_1.default.promises.readFile(file, 'utf8');
|
|
37
|
+
// build 不注入 <base>:产物纯拷贝、线上静态托管自行处理尾斜杠。
|
|
38
|
+
// 相对链接错位是 dev 特有问题(basePathRewritePlugin 内部 rewrite 不改地址栏)。
|
|
37
39
|
html = (0, transform_1.injectPlatformTags)(html);
|
|
38
40
|
if (doMinify) {
|
|
39
41
|
// 用 HBS-保护版 minify:占位符 {{...}} 在 CSS / inline JS 里不会被压坏
|
package/lib/plugins/inject.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import type { Plugin } from 'vite';
|
|
|
2
2
|
export interface PlatformInjectPluginOptions {
|
|
3
3
|
/** appId override,透传给 view-context(dev 拼真值字面量)。 */
|
|
4
4
|
appId?: string;
|
|
5
|
+
/** vite `base`,透传给注入以在前缀下修相对链接(`<base href>`)。 */
|
|
6
|
+
base?: string;
|
|
5
7
|
}
|
|
6
8
|
/**
|
|
7
9
|
* dev 阶段的平台注入:走 vite 原生 `transformIndexHtml`,调用与 build 共享的
|
package/lib/plugins/inject.js
CHANGED
|
@@ -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;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.baseTag = baseTag;
|
|
4
|
+
/**
|
|
5
|
+
* `<base href>` 注入(dev + build 共用)。
|
|
6
|
+
*
|
|
7
|
+
* 沙箱网关把 `/app/<id>`(**无末尾斜杠**)透传给 devserver,`basePathRewritePlugin` 走
|
|
8
|
+
* 内部 rewrite(**非 301**)补上尾斜杠让 index.html 能出 —— 但内部 rewrite 不改浏览器
|
|
9
|
+
* 地址栏,地址栏仍停在无斜杠的 `/app/<id>`。此时 HTML 里的**相对链接**
|
|
10
|
+
* (`href="solutions.html"`)按"替换最后一段"规则解析成 `/app/solutions.html`(appId 被
|
|
11
|
+
* 吃掉)→ 请求不以 base `/app/<id>/` 开头 → vite baseMiddleware 警告 / 404。相对静态
|
|
12
|
+
* 资源(`src="assets/logo.svg"`)同理错位。
|
|
13
|
+
*
|
|
14
|
+
* 注入 `<base href="${base}">` 让相对链接 / 相对资源一律相对 base 解析,无视地址栏末尾
|
|
15
|
+
* 斜杠。head-prepend 放最前,先于用户 head 里的相对 `<link>` / `<script>`,页面加载时
|
|
16
|
+
* 同步发出的资源也能吃到正确 base。
|
|
17
|
+
*
|
|
18
|
+
* 不注入的情形:
|
|
19
|
+
* - `base` 为空或 `'/'`(本地裸 dev / 无前缀)—— 无前缀问题,避免 `<base href="/">`
|
|
20
|
+
* 平白改变页内锚点语义。
|
|
21
|
+
* - 源 HTML 已手写 `<base>` —— 尊重用户,不覆盖。
|
|
22
|
+
*
|
|
23
|
+
* 已知取舍:`<base>` 会让页内锚点 `#xxx` 也相对 base 解析(跳到 base 页 + hash 而非当前
|
|
24
|
+
* 页)。当前模板是纯 `.html` MPA 导航,影响可控。
|
|
25
|
+
*/
|
|
26
|
+
const BASE_TAG_RE = /<base[\s/>]/i;
|
|
27
|
+
function baseTag(base, html) {
|
|
28
|
+
if (!base || base === '/')
|
|
29
|
+
return [];
|
|
30
|
+
if (BASE_TAG_RE.test(html))
|
|
31
|
+
return [];
|
|
32
|
+
return [{ tag: 'base', attrs: { href: base }, injectTo: 'head-prepend' }];
|
|
33
|
+
}
|
package/lib/transform/index.d.ts
CHANGED
|
@@ -25,6 +25,11 @@ export interface InjectPlatformOptions {
|
|
|
25
25
|
viewCtx?: RequestRenderContext;
|
|
26
26
|
/** appId override(透传给 view-context)。 */
|
|
27
27
|
appId?: string;
|
|
28
|
+
/**
|
|
29
|
+
* vite `base`(`${CLIENT_BASE_PATH}/`);非空且非 `/` 时注入 `<base href>` 修相对链接。
|
|
30
|
+
* 空 / `/` 或源 HTML 已有 `<base>` 时不注入。
|
|
31
|
+
*/
|
|
32
|
+
base?: string;
|
|
28
33
|
slardar?: SlardarOptions;
|
|
29
34
|
ogMeta?: OgMetaOptions;
|
|
30
35
|
}
|
|
@@ -32,7 +37,8 @@ export interface InjectPlatformOptions {
|
|
|
32
37
|
* 对一份 HTML 注入全部平台能力(view-context + slardar + runtime + 额外清单 + og-meta)。
|
|
33
38
|
* dev 插件与 build 步骤共用,唯一真相。默认 build 模式(emit HBS 占位符)。
|
|
34
39
|
*
|
|
35
|
-
* head-prepend 顺序(injectTags 按数组序 =
|
|
36
|
-
*
|
|
40
|
+
* head-prepend 顺序(injectTags 按数组序 = 文档序):`<base>` 在最前(须先于任何相对
|
|
41
|
+
* 链接 / 资源),view-context 其后,slardar 再其后,runtime 随后——保证 runtime 读
|
|
42
|
+
* window.appId / collectEvent 时已就绪。预览桥仅 dev 注入。
|
|
37
43
|
*/
|
|
38
44
|
export declare function injectPlatformTags(html: string, opts?: InjectPlatformOptions): string;
|
package/lib/transform/index.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PLATFORM_TAGS = void 0;
|
|
4
4
|
exports.injectPlatformTags = injectPlatformTags;
|
|
5
5
|
const html_tags_1 = require("../html-tags");
|
|
6
|
+
const base_tag_1 = require("./base-tag");
|
|
6
7
|
const view_context_1 = require("./view-context");
|
|
7
8
|
const slardar_1 = require("./slardar");
|
|
8
9
|
const runtime_1 = require("./runtime");
|
|
@@ -28,12 +29,14 @@ exports.PLATFORM_TAGS = [];
|
|
|
28
29
|
* 对一份 HTML 注入全部平台能力(view-context + slardar + runtime + 额外清单 + og-meta)。
|
|
29
30
|
* dev 插件与 build 步骤共用,唯一真相。默认 build 模式(emit HBS 占位符)。
|
|
30
31
|
*
|
|
31
|
-
* head-prepend 顺序(injectTags 按数组序 =
|
|
32
|
-
*
|
|
32
|
+
* head-prepend 顺序(injectTags 按数组序 = 文档序):`<base>` 在最前(须先于任何相对
|
|
33
|
+
* 链接 / 资源),view-context 其后,slardar 再其后,runtime 随后——保证 runtime 读
|
|
34
|
+
* window.appId / collectEvent 时已就绪。预览桥仅 dev 注入。
|
|
33
35
|
*/
|
|
34
36
|
function injectPlatformTags(html, opts = {}) {
|
|
35
37
|
const mode = opts.mode ?? 'build';
|
|
36
38
|
const tags = [
|
|
39
|
+
...(0, base_tag_1.baseTag)(opts.base, html),
|
|
37
40
|
(0, view_context_1.viewContextTag)(mode, { appId: opts.appId }, opts.viewCtx),
|
|
38
41
|
...(0, slardar_1.slardarTags)(opts.slardar),
|
|
39
42
|
// miaoda-coding-sdk runtime:依赖 collectEvent(slardar) + appId(view-context),故排其后
|
package/lib/vite-config.d.ts
CHANGED
|
@@ -17,7 +17,8 @@ export interface HtmlViteOptions {
|
|
|
17
17
|
* 无 `CLIENT_BASE_PATH` 时 base 退化为 `/`(本地裸 dev 不受影响)。
|
|
18
18
|
* - HMR 走 vite 的 WS:`MIAODA_WS_HOST` 存在时切独立 WS 域名(对齐 vite-react 网关方案)。
|
|
19
19
|
* - `appType: 'mpa'`:纯静态多页,不做 SPA history fallback。
|
|
20
|
-
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
20
|
+
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
21
|
+
* + runtime-url-fix(撕掉 vite 给后端运行时绝对 URL 误加的 base 前缀)。
|
|
21
22
|
*
|
|
22
23
|
* 沙箱 WS 韧性(dev only,对齐 coding-preset-vite-react):
|
|
23
24
|
* - `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");
|
|
@@ -23,7 +24,8 @@ const normalize_base_path_1 = require("./normalize-base-path");
|
|
|
23
24
|
* 无 `CLIENT_BASE_PATH` 时 base 退化为 `/`(本地裸 dev 不受影响)。
|
|
24
25
|
* - HMR 走 vite 的 WS:`MIAODA_WS_HOST` 存在时切独立 WS 域名(对齐 vite-react 网关方案)。
|
|
25
26
|
* - `appType: 'mpa'`:纯静态多页,不做 SPA history fallback。
|
|
26
|
-
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
27
|
+
* - 内置 health + 平台注入 + WS 韧性插件(vite-client-patch / ws-watchdog)+ base-path rewrite
|
|
28
|
+
* + runtime-url-fix(撕掉 vite 给后端运行时绝对 URL 误加的 base 前缀)。
|
|
27
29
|
*
|
|
28
30
|
* 沙箱 WS 韧性(dev only,对齐 coding-preset-vite-react):
|
|
29
31
|
* - `viteClientPatchPlugin`:删 WS 重连后的 `location.reload()`(休眠唤醒白屏),
|
|
@@ -63,12 +65,14 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
63
65
|
plugins: [
|
|
64
66
|
(0, raw_jsx_1.rawJsxPlugin)({ root, clientBasePath }),
|
|
65
67
|
(0, health_1.healthPlugin)(),
|
|
66
|
-
(0, inject_1.platformInjectPlugin)({ appId }),
|
|
68
|
+
(0, inject_1.platformInjectPlugin)({ appId, base }),
|
|
67
69
|
// dev only:apply:'serve' 自带,build 不挂
|
|
68
70
|
(0, vite_client_patch_1.viteClientPatchPlugin)(),
|
|
69
71
|
(0, ws_watchdog_1.wsWatchdogPlugin)({ clientBasePath }),
|
|
70
72
|
// base 非 `/` 时,把无尾斜杠的 `/app/<id>` 内部 rewrite 成带尾斜杠(自守卫空 base)
|
|
71
73
|
(0, base_path_1.basePathRewritePlugin)(clientBasePath),
|
|
74
|
+
// post transform:撕掉 vite 给运行时/后端绝对 URL(含 runtime 段)误加的 base 前缀
|
|
75
|
+
(0, runtime_url_fix_1.runtimeUrlFixPlugin)(clientBasePath),
|
|
72
76
|
// 在 `${base}routes.json` 提供路由清单(与 build 产物一致)
|
|
73
77
|
(0, routes_json_1.routesJsonPlugin)({ root, clientBasePath }),
|
|
74
78
|
],
|
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.6-alpha.1",
|
|
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",
|