@lark-apaas/coding-html-devserver 0.1.5-beta.0 → 0.1.6-alpha.0

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 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 里不会被压坏
@@ -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 共享的
@@ -27,6 +27,7 @@ function platformInjectPlugin(options = {}) {
27
27
  mode: 'dev',
28
28
  viewCtx: view_context_1.requestContext.getStore(),
29
29
  appId: options.appId,
30
+ base: options.base,
30
31
  });
31
32
  },
32
33
  };
@@ -0,0 +1,2 @@
1
+ import type { HtmlTagDescriptor } from '../html-tags';
2
+ export declare function baseTag(base: string | undefined, html: string): HtmlTagDescriptor[];
@@ -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
+ }
@@ -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 按数组序 = 文档序):view-context 在最前,slardar 其后,
36
- * runtime 再其后——保证 runtime 读 window.appId / collectEvent 时已就绪。预览桥仅 dev 注入。
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;
@@ -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 按数组序 = 文档序):view-context 在最前,slardar 其后,
32
- * runtime 再其后——保证 runtime 读 window.appId / collectEvent 时已就绪。预览桥仅 dev 注入。
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),故排其后
@@ -63,7 +63,7 @@ function createHtmlViteConfig(opts = {}) {
63
63
  plugins: [
64
64
  (0, raw_jsx_1.rawJsxPlugin)({ root, clientBasePath }),
65
65
  (0, health_1.healthPlugin)(),
66
- (0, inject_1.platformInjectPlugin)({ appId }),
66
+ (0, inject_1.platformInjectPlugin)({ appId, base }),
67
67
  // dev only:apply:'serve' 自带,build 不挂
68
68
  (0, vite_client_patch_1.viteClientPatchPlugin)(),
69
69
  (0, ws_watchdog_1.wsWatchdogPlugin)({ clientBasePath }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/coding-html-devserver",
3
- "version": "0.1.5-beta.0",
3
+ "version": "0.1.6-alpha.0",
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",