@cordis-tavern/ct-index-renderer
v0.1.0-rc.1
Published
cordis-tavern 的 index 注入插件:经 index-inject 事件收集 head/body 片段并向 index 插入
Readme
@cordis-tavern/ct-index-renderer
cordis-tavern 的 index 注入插件:在启动时段经事件 index-inject 收集各插件要插入 index 的片段,由本插件统一控制插入 —— 与 ct-client-found 的 index 职责解耦。
行为
| 步骤 | 说明 |
| --- | --- |
| 事件 | 固定名 index-inject(INDEX_INJECT_EVENT / event 导出同值) |
| 发布 | ctx.emit('index-inject', payload) |
| 收集 | indexInject(事件订阅 + 服务方法同一入口);校验失败 TypeError 不入表 |
| 插入 | apply(html):global → 文档最前;head → </head> 前;body → </body> 前(同 placement 保持发布序) |
| 服务 | indexRenderer(收集 / apply)+ distIndex(见下) |
distIndex 服务
调用后返回自身 index.html 的绝对磁盘路径字符串(发布物内为 <pkg>/dist/index.html)。
// 本插件 apply 同步段
ctx.provide('distIndex', () => distIndexPath())
// 消费方:export const inject = ['distIndex']
const absPath = ctx.distIndex() // string也可直接 import { distIndexPath } from '@cordis-tavern/ct-index-renderer'。包根 index.html 由构建拷入 dist/。
载荷
{ kind: 'script', placement: 'head', content: '…' }| 键 | 类型 | 规则 |
| --- | --- | --- |
| kind | string | 非空;类型提示(script / inline / global / style …),不强制枚举 |
| placement | 'head' \| 'body' \| 'global' | 仅这三值;global 无 head/body 位置,前缀到文档首(如 __CT_BOOT__) |
| content | string | 非空;浏览器 DOM 片段原文,原样插入不包标签 |
未知键与非法值 → TypeError 指名键名。缺 </head> / </body> 时不丢注入(前缀 / 追加兜底)。
发布序 ≠ 执行序:同一 placement 按收集(发布)顺序插入;浏览器执行顺序由 apply 后的文档位置决定(global 最先)。
配置
本轮无业务键:允许 undefined / null / {};任意未知键 → TypeError。
发布方示例
ctx.emit('index-inject', {
kind: 'script',
placement: 'head',
content: '<script>console.log("startup tool")</script>',
})装载
- 包名写进环境级
ct.profile:ct plugin add -p <环境名> @cordis-tavern/ct-index-renderer - 包目录须落在
$CT_HOME/profile/<env>/node_modules/或$CT_HOME/node_modules/ ct.bundle.path→cordis.patch.yml,insert[].id: indexRenderer
开发
pnpm build # 根仓库:esbuild → ct-index-renderer/dist/index.js
pnpm typecheck
pnpm test # 对 dist/ 产物