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)], [], '注册数组里留着没人用的组件(整包的问题不该在按需里重犯)') })