From 2dc13d027427bd9063bbdd01db1b5161211afa28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Mon, 5 Oct 2026 22:29:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8C=89=E9=9C=80=E6=B3=A8=E5=86=8C):=20?= =?UTF-8?q?=E3=80=8C=E7=BC=96=E8=BE=91=E7=94=A8=E6=88=B7=E2=86=92=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E3=80=8D=E6=B8=B2=E6=9F=93=E6=88=90=E4=B8=80=E8=A1=8C?= =?UTF-8?q?=E8=A3=B8=E6=96=87=E6=9C=AC=E2=80=94=E2=80=94radio=20=E5=AD=90?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=98=AF=20withNoopInstall=EF=BC=8C=E8=A6=81?= =?UTF-8?q?=20use=20=E7=88=B6=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因不在样式也不在文案,在注册表:src/plugins/element.ts 登记的是 ElRadioButton / ElRadioGroup,而 EP 里子组件由 withNoopInstall 造(component.install = NOOP), app.use 它们注册不出任何东西;只有父组件 ElRadio 的 withInstall(radio, {RadioButton, RadioGroup}) 才会把三个名字一起 app.component 出去。于是模板里的 / 解析失败,被当成未知自定义元素渲染成裸标签,两个按钮文案连成一行 「普通账号管理员」。全站 30 条注册项只注册出 33 个名字,缺的就是这两个; ElFormItem / ElTableColumn / ElOption 等同为 no-op 的子组件都因父组件在表里而正常注册。 取证四路:像素扫描(角色栏 y=174-184 只有字形反锯齿点,同页三个输入框在 y=104/204/404 都是 723px 宽的完整边框)→ DOM(.el-radio-group 为 null,innerHTML 是原样标签)→ 控制台(Failed to resolve component: el-radio-group / el-radio-button 各 2 条)→ 运行期 app._context.components 键集合(无 ElRadioGroup/ElRadioButton)。 护栏原本 7/7 全绿而页面是坏的:perf_ep_registry_covers_every_used_tag 只比对注册数组的 文本里有没有那个名字——"登记过"被当成了"生效过"。改成对每个条目真跑 install(假 app)、 拿运行期注册出来的名字集合比对;修前红且红的正好是这两条。同时给 perf_ep_registry_has_no_dead_entries 补父组件豁免,否则正确修法(加 ElRadio,它自己没有 标签)反会被那条判成死条目——旧尺子会把人往错方向推。 验证:node --test 全量 1738 例 0 红、vue-tsc --noEmit rc=0、浏览器实测角色已渲染成分段 按钮("普通账号"高亮)且无解析告警。未提交未发布,现网 admin-vue 仍是 20261005-182523。 --- admin-frontend-vue/src/plugins/element.ts | 11 ++-- .../tests/perf-element-on-demand.test.ts | 60 ++++++++++++++++--- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/admin-frontend-vue/src/plugins/element.ts b/admin-frontend-vue/src/plugins/element.ts index 274599b23..7207fbcc7 100644 --- a/admin-frontend-vue/src/plugins/element.ts +++ b/admin-frontend-vue/src/plugins/element.ts @@ -9,6 +9,11 @@ * 1. 新增/换用任何 `el-*` 标签,这里必须同时加「组件导入」和「按组件样式导入」。 * 少一条不会让构建变红,只会让那个页面渲染成裸标签 —— 护栏在 * tests/perf-element-on-demand.test.ts,它从源码推标签清单来比对这份文件。 + * 1b. 子组件(`el-radio-group` / `el-form-item` / `el-table-column` 这类)在 EP 里是 + * `withNoopInstall` 造的,`app.use` 它们注册不出任何东西;要引的是**父组件** + * (`ElRadio` / `ElForm` / `ElTable`),由父组件的 install 把子组件一起注册。 + * 10-05「编辑用户 → 角色」渲染成裸文本就是只登记了两个 radio 子组件漏的。 + * 护栏按运行期 install 出来的名字集合比对,不看这份清单的字符串。 * 2. 样式一律走 `element-plus/es/components/<短名>/style/css`,它会带上 base * 与依赖组件的样式(例如 message-box 自带 input/button/overlay); * 不要图省事改回 `theme-chalk/index.css` 或 `dist/index.css`。 @@ -39,8 +44,7 @@ import { ElMenuItem, ElOption, ElProgress, - ElRadioButton, - ElRadioGroup, + ElRadio, ElResult, ElScrollbar, ElSelect, @@ -113,8 +117,7 @@ const components = [ ElMenuItem, ElOption, ElProgress, - ElRadioButton, - ElRadioGroup, + ElRadio, ElResult, ElScrollbar, ElSelect, diff --git a/admin-frontend-vue/tests/perf-element-on-demand.test.ts b/admin-frontend-vue/tests/perf-element-on-demand.test.ts index edde6d21e..30517b8e1 100644 --- a/admin-frontend-vue/tests/perf-element-on-demand.test.ts +++ b/admin-frontend-vue/tests/perf-element-on-demand.test.ts @@ -45,6 +45,43 @@ function toComponentName(kebab: string): string { return 'El' + kebab.split('-').map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join('') } +/** + * 逐个对注册数组里的条目调 `install(假 app)`,收集真正注册出来的组件名。 + * + * 为什么不能只比对数组文本:Element Plus 里子组件是 `withNoopInstall` 造的 + * (`component.install = NOOP`),`app.use(ElRadioGroup)` 等于什么都没注册;只有父组件 + * 的 `withInstall(main, extra)` 会把 main + extra 一起 `app.component(comp.name, comp)` 注册。 + * 所以「数组里写着 ElRadioGroup / ElRadioButton」和「模板能解析出 el-radio-group」是两件事 —— + * 前者当时全绿,后者在 10-05 的「编辑用户 → 角色」上渲染成了裸标签。 + * 尺子必须是运行期注册出来的名字集合,不是清单字符串。 + */ +async function installedComponentNames(names: string[]): Promise> { + const EP = (await import('element-plus')) as unknown as Record< + string, + { install?: (app: unknown) => void } | undefined + > + const out = new Map() + for (const name of names) { + const comp = EP[name] + assert.ok(comp, `element-plus 没有导出 ${name}`) + assert.equal(typeof comp?.install, 'function', `${name} 没有 install(),app.use 它是空转`) + const registered: string[] = [] + const app: Record = { + component(n: string) { + registered.push(n) + return app + }, + directive() { + return app + }, + config: { globalProperties: {} }, + } + comp!.install!(app) + out.set(name, registered) + } + return out +} + // 命令式 API 与指令:没有对应模板标签,所以不算"注册表里的死条目" const EXTRA_REGISTERED = ['ElConfigProvider', 'ElMessage', 'ElMessageBox', 'ElLoading', 'ElLoadingDirective'] @@ -121,11 +158,12 @@ function importedNames(registry: string): string[] { .flatMap((line) => [...line.matchAll(/\b(El[A-Z][A-Za-z0-9]*)\b/g)].map((m) => m[1])) } -test('perf_ep_registry_covers_every_used_tag', () => { +test('perf_ep_registry_covers_every_used_tag', async () => { const registry = readSource('src/plugins/element.ts') - const registered = new Set(registeredComponents(registry)) - const missing = usedTags().map(toComponentName).filter((name) => !registered.has(name)) - assert.deepEqual(missing, [], '有模板在用的 el-* 组件没进注册数组(页面会渲染成裸标签)') + const closure = await installedComponentNames(registeredComponents(registry)) + const effective = new Set([...closure.values()].flat()) + const missing = usedTags().map(toComponentName).filter((name) => !effective.has(name)) + assert.deepEqual(missing, [], '有模板在用的 el-* 组件在 app.use 之后并未注册出来(页面会渲染成裸标签)') }) test('perf_ep_every_used_tag_imports_its_style', () => { @@ -139,14 +177,20 @@ test('perf_ep_every_used_tag_imports_its_style', () => { assert.deepEqual(missing, [], '有在用的 el-* 组件没引按组件样式(构建照样绿,页面会没样式)') }) -test('perf_ep_registry_has_no_dead_entries', () => { +test('perf_ep_registry_has_no_dead_entries', async () => { const registry = readSource('src/plugins/element.ts') - const used = new Set(usedTags()) + const tags = usedTags() + const used = new Set(tags) + const usedComponentNames = new Set(tags.map(toComponentName)) + const closure = await installedComponentNames(registeredComponents(registry)) const dead: string[] = [] - for (const name of registeredComponents(registry)) { + for (const name of closure.keys()) { if (EXTRA_REGISTERED.includes(name)) continue const kebab = name.slice(2).replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase() - if (!used.has(kebab)) dead.push(name) + if (used.has(kebab)) continue + // 父组件例外:它自己不出现在模板里,但只有 app.use(它) 才把在用的子组件注册出来。 + if ((closure.get(name) ?? []).some((n) => usedComponentNames.has(n))) continue + dead.push(name) } assert.deepEqual([...new Set(dead)], [], '注册数组里留着没人用的组件(整包的问题不该在按需里重犯)') })