diff --git a/README.md b/README.md index 23edfbf9..e867c760 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ monkey-ai-web/ | `npm test -- --test-concurrency=1` | 串行运行单测(扫描 `src/`、`tests/`、`test/unit/`;新增共享单测放 `test/unit/`) | | `npm run eslint` / `npm run eslint:fix` | ESLint 检查(零警告门禁)/ 自动修复 | | `npm run lint` / `npm run lint:fix` | Prettier 检查 / 全仓格式化 | -| `npm run check:ui` | UI 依赖、完整 lock 图与品牌图标资产 hash 审计 | +| `npm run check:ui` | 审计应用源码、脚本、单测和根目录 JS 配置中的旧 UI 库引用,以及依赖声明、锁文件和品牌图标资产 SHA-256 | | `npm run check` | 依次执行 `check:ui`、ESLint、单测与生产构建,任一步失败即停止 | | `npm run vendor:icons` | 重新生成品牌图标资产 | | `npm run test:e2e` / `test:e2e:headed` / `test:e2e:report` | Playwright 验收 / 有头运行 / 查看报告 | diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3d843e23..1565be17 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,6 @@ # 变更记录(CHANGELOG) -> 状态:有效(持续追加) · 更新:2026-11-19 · 版本:v3.6 +> 状态:有效(持续追加) · 更新:2026-11-20 · 版本:v3.7 对现状仍有影响的功能与修复记录,按日期倒序。改造、迁移、优化等一次性过程不在此记录。 @@ -10,6 +10,14 @@ ## 2026-11-20 +### 修复 · 旧 UI 库审计规则统一 + +- **范围**:ESLint、UI 依赖审计、回归单测与开发文档。 +- **变更**:通过共享规则拦截旧 UI 包的导入、重导出及静态可确定的动态加载;审计扩展到根目录 JS 配置、脚本和单测,并检查 npm 别名与嵌套传递依赖。 +- **影响**:不改变运行时依赖或 UI 行为;保留本地品牌图标的来源与 SHA-256 校验。 +- **回滚**:仅逆向恢复本条对应的规则、审计、测试和文档差异。 +- **关联**:`eslint.config.cjs`、`scripts/ui-dependency-policy.cjs`、`scripts/check-ui-dependencies.mjs`、`test/unit/ui-dependency-policy.test.js`。 + ### 修复 · 个人密钥管理查询交互对齐 - **范围**:用户中心密钥管理的查询、搜索、分页。 diff --git a/docs/standards/DEV_STANDARDS.md b/docs/standards/DEV_STANDARDS.md index 9b07214a..23b17e4e 100644 --- a/docs/standards/DEV_STANDARDS.md +++ b/docs/standards/DEV_STANDARDS.md @@ -1,6 +1,6 @@ # 统一开发与 Ant Design CRUD 规范 -> 状态:有效 · 适用:React 18.3、antd 6.7.0、Vite 5.4、Node ≥ 22.13 · 更新:2026-11-20 · 版本:v2.4 +> 状态:有效 · 适用:React 18.3、antd 6.7.0、Vite 5.4、Node ≥ 22.13 · 更新:2026-11-20 · 版本:v2.5 新增与重构 CRUD 的编码规范:目录与命名、组件选择、请求、React 组件与状态、表单与弹层、权限、i18n,以及可直接复制的页面模板。外观取值见 `UI_COMPONENT_STANDARDS.md`。 @@ -110,7 +110,7 @@ - 外观取值(颜色、间距、圆角、字号、各组件样式)以 `docs/standards/UI_COMPONENT_STANDARDS.md` 为准,本文只写编码约束。 - 按钮与操作图标:`import { Plus, Pencil, Trash2, RefreshCw, Search } from '@/components/common/icons/ActionIcons'`,按钮内传 `size={16}`;装饰图标用 `lucide-react`。 -- 禁止导入 `@lobehub/*`、Radix UI、Floating UI、Semi、MUI、Chakra、Headless UI、leva、antd-style、react-layout-kit(ESLint `no-restricted-imports` 与 `npm run check:ui` 拦截)。 +- 禁止导入 `@lobehub/*`、Radix UI、Floating UI、Semi、MUI(含旧 `@material-ui/*`)、Chakra、Headless UI、leva、antd-style、react-layout-kit。ESLint 的 `ui-dependencies/no-legacy-ui` 与 `npm run check:ui` 共用 `scripts/ui-dependency-policy.cjs`,不要在入口重复维护包名。 - JSX 不写十六进制颜色;不用全局 `!important`;新视图不用 Tailwind 颜色类(如 `text-gray-500`),改用 token 或页面变量。 ## 3. 请求封装与错误处理 diff --git a/docs/standards/UI_COMPONENT_STANDARDS.md b/docs/standards/UI_COMPONENT_STANDARDS.md index f6a6103b..b337f1c9 100644 --- a/docs/standards/UI_COMPONENT_STANDARDS.md +++ b/docs/standards/UI_COMPONENT_STANDARDS.md @@ -1,6 +1,6 @@ # UI 公共组件与样式规范 -> 状态:有效 · 适用:antd 6.7.0 · 更新:2026-11-18 · 版本:v2.5 +> 状态:有效 · 适用:antd 6.7.0 · 更新:2026-11-20 · 版本:v2.6 所有后台页面共用的主题与组件标准。页面规范只记录与本文不同的部分;编码约定见 `DEV_STANDARDS.md`,外壳与布局见 `GLOBAL_UI_BASELINE.md`。 @@ -246,8 +246,8 @@ html.dark .km-page { 组件分层(antd 原生、中心封装、契约封装、专用组件)及选用方式见 `DEV_STANDARDS.md` 2.1、2.2 节。`src/components/ui/*` 只做视图映射,不放请求、权限与业务状态。 -- 禁止导入:`@lobehub/*`、Radix UI、Floating UI、Semi、MUI、Chakra、Headless UI、leva、antd-style、react-layout-kit(`eslint.config.cjs` 的 `no-restricted-imports`)。 -- `npm run check:ui` 审计源码导入、完整 lock 图与品牌图标资产 hash,已包含在 `npm run check` 中。 +- 禁用范围见开发规范第 2.4 节;ESLint 与 `npm run check:ui` 共用一份规则,本地品牌图标不是运行时 UI 依赖。 +- `npm run check:ui` 审计根目录 JS 配置、应用源码、脚本和单测的包引用,并检查依赖声明、锁文件中的传递包和别名,以及品牌图标资产 SHA-256。命令已包含在 `npm run check` 中。 - `antd` 与 `@ant-design/icons` 精确锁定版本;`src/components/ui/antd-v5-compat.css` 统一标签间距、分页选择器宽度与必填标记字体,页面样式可以覆盖。 - 当前版本 API 约定:Card 用 `variant`,Drawer 用 `size`,弹层遮罩用 `mask` 配置,Alert 标题用 `title`;样式选择器使用 `.ant-modal-container`、`.ant-alert-title`、`.ant-collapse-body`。聊天消息列表使用 `List`(antd 已标记弃用),替换前需先建立该区域的视觉基线。 diff --git a/eslint.config.cjs b/eslint.config.cjs index cb77488e..8f6d2b13 100644 --- a/eslint.config.cjs +++ b/eslint.config.cjs @@ -2,6 +2,7 @@ const globals = require('globals'); const react = require('eslint-plugin-react'); const reactHooks = require('eslint-plugin-react-hooks'); const unusedImports = require('eslint-plugin-unused-imports'); +const { uiDependencyConfig } = require('./scripts/ui-dependency-policy.cjs'); module.exports = [ { @@ -17,6 +18,7 @@ module.exports = [ 'src/assets/brand-icons/*.js', ], }, + uiDependencyConfig, { files: ['**/*.{js,jsx}'], linterOptions: { reportUnusedDisableDirectives: 'off' }, @@ -33,33 +35,6 @@ module.exports = [ 'unused-imports': unusedImports, }, rules: { - 'no-restricted-imports': [ - 'error', - { - patterns: [ - { - group: [ - '@radix-ui/*', - '@floating-ui/*', - '@lobehub/ui', - '@lobehub/ui/*', - '@lobehub/icons', - '@lobehub/icons/*', - '@douyinfe/semi-*', - '@headlessui/*', - '@mui/*', - '@chakra-ui/*', - 'leva', - 'leva/*', - 'antd-style', - 'react-layout-kit', - ], - message: - 'Use antd public APIs or the audited local brand SVG assets. Preserve legacy components/ui callback contracts.', - }, - ], - }, - ], 'no-multiple-empty-lines': ['error', { max: 1 }], // ===== 防线:杜绝“用了未定义/未导入标识符”导致的运行时崩溃(本次线上事故根因)===== diff --git a/scripts/check-ui-dependencies.mjs b/scripts/check-ui-dependencies.mjs index 61e033f7..d9028e83 100644 --- a/scripts/check-ui-dependencies.mjs +++ b/scripts/check-ui-dependencies.mjs @@ -3,20 +3,28 @@ import { createHash } from 'node:crypto'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { Linter } from 'eslint'; +import policy from './ui-dependency-policy.cjs'; const root = fileURLToPath(new URL('../', import.meta.url)); -const forbidden = [ - /^@radix-ui\//, - /^@floating-ui\//, - /^@lobehub\/(?:ui|icons)(?:\/|$)/, - /^@douyinfe\/semi-(?:ui|icons|foundation)(?:\/|$)/, - /^@headlessui\//, - /^@mui\//, - /^@chakra-ui\//, - /^(?:leva|antd-style|react-layout-kit)(?:\/|$)/, -]; -export const isForbiddenUiDependency = (name) => - forbidden.some((pattern) => pattern.test(name)); +const { isForbiddenUiDependency, uiDependencyConfig } = policy; +const linter = new Linter({ cwd: root }); +const sourceConfig = [uiDependencyConfig]; +const sourceDirectories = new Set(['src', 'scripts', 'test', 'tests']); +const excludedDirectories = new Set([ + 'node_modules', + 'dist', + 'dist-ssr', + 'build', + 'coverage', + 'report', + 'logs', + 'test-results', + 'playwright-report', + 'blob-report', + 'kt_batches', + 'kt_out', +]); +export { isForbiddenUiDependency }; export function auditManifest(manifest, lock) { const issues = []; @@ -26,14 +34,22 @@ export function auditManifest(manifest, lock) { 'optionalDependencies', 'peerDependencies', ]) { - for (const name of Object.keys(manifest[section] || {})) { - if (isForbiddenUiDependency(name)) + for (const [name, specifier] of Object.entries(manifest[section] || {})) { + const alias = /^npm:((?:@[^/]+\/)?[^@/]+)(?:@|$)/.exec(specifier)?.[1]; + if (isForbiddenUiDependency(name)) { issues.push(`package.json ${section}: ${name}`); + } else if (isForbiddenUiDependency(alias)) { + issues.push(`package.json ${section}: ${name} -> ${alias}`); + } } } - for (const location of Object.keys(lock.packages || {})) { - if (isForbiddenUiDependency(location.split('node_modules/').at(-1))) { + for (const [location, entry] of Object.entries(lock.packages || {})) { + const names = location.split(/(?:^|\/)node_modules\//); + if (names.length === 1) continue; + if (isForbiddenUiDependency(names.at(-1))) { issues.push(`package-lock.json: ${location}`); + } else if (isForbiddenUiDependency(entry.name)) { + issues.push(`package-lock.json: ${location} -> ${entry.name}`); } } if (!lock.packages || lock.lockfileVersion < 2) @@ -42,58 +58,26 @@ export function auditManifest(manifest, lock) { } export function auditSource(source, filename) { - const linter = new Linter({ configType: 'eslintrc' }); - linter.defineRule('ui-dependencies', { - create(context) { - const inspect = (node) => { - if ( - node?.type === 'Literal' && - typeof node.value === 'string' && - isForbiddenUiDependency(node.value) - ) { - context.report({ - node, - message: `Use Ant Design or the audited local brand assets: ${node.value}`, - }); - } - }; - return { - ImportDeclaration: (node) => inspect(node.source), - ExportNamedDeclaration: (node) => inspect(node.source), - ExportAllDeclaration: (node) => inspect(node.source), - ImportExpression: (node) => inspect(node.source), - CallExpression: (node) => { - if ( - node.callee.type === 'Identifier' && - node.callee.name === 'require' - ) - inspect(node.arguments[0]); - }, - }; - }, - }); return linter - .verify( - source, - { - parserOptions: { - ecmaVersion: 2022, - sourceType: 'module', - ecmaFeatures: { jsx: true }, - }, - rules: { 'ui-dependencies': 'error' }, - }, - { filename, allowInlineConfig: false }, - ) + .verify(source, sourceConfig, { filename, allowInlineConfig: false }) .map((issue) => `${filename}:${issue.line}: ${issue.message}`); } -async function sourceFiles(dir) { +async function sourceFiles(dir, isRoot = true) { const files = []; for (const entry of await readdir(dir, { withFileTypes: true })) { const name = path.join(dir, entry.name); - if (entry.isDirectory()) files.push(...(await sourceFiles(name))); - else if (/\.[cm]?[jt]sx?$/.test(entry.name)) files.push(name); + if (entry.isDirectory()) { + if ( + isRoot + ? sourceDirectories.has(entry.name) + : !entry.name.startsWith('.') && !excludedDirectories.has(entry.name) + ) { + files.push(...(await sourceFiles(name, false))); + } + } else if (entry.isFile() && /\.(?:js|jsx|cjs|mjs)$/.test(entry.name)) { + files.push(name); + } } return files; } @@ -106,7 +90,7 @@ export async function checkUiDependencies(directory = root) { await readFile(path.join(directory, 'package-lock.json'), 'utf8'), ); const issues = auditManifest(manifest, lock); - const files = await sourceFiles(path.join(directory, 'src')); + const files = await sourceFiles(directory); for (const filename of files) { issues.push( ...auditSource( @@ -143,7 +127,7 @@ export async function checkUiDependencies(directory = root) { return { issues, files: files.length, - packages: Object.keys(lock.packages).length - 1, + packages: Math.max(0, Object.keys(lock.packages || {}).length - 1), }; } diff --git a/scripts/i18n_kt_overflow.cjs b/scripts/i18n_kt_overflow.cjs index 2707b23d..c877ec58 100644 --- a/scripts/i18n_kt_overflow.cjs +++ b/scripts/i18n_kt_overflow.cjs @@ -3,7 +3,7 @@ // 找出会溢出的具体位置。控制台是围绕中文两字标签设计的,泰语平均膨胀 3.16x, // 短标签最高 9x,密集表格首当其冲。 // -// 判据:Semi 的 Table column `width: N`(px)在 14px 字号下大致可容 +// 判据:表格列的 `width: N`(px)在 14px 字号下大致可容 // 中文 ≈ N/14 字,拉丁/泰文 ≈ N/7.5 字符(泰文含上下叠加符号,按拉丁估更保守)。 // 超出即报,按超出比例排序。这是静态估算,不替代真机目视走查。 const fs = require('fs'); @@ -20,7 +20,7 @@ const DICTS = LANGS.map((l) => [l, load(l)]); const PX_PER_CHAR = 7.5; // 拼音文字/韩/泰在 14px 下的保守平均字宽 // 日文假名与汉字、繁体中文都是全角,按字号整宽计 const PX_PER_CHAR_BY_LANG = { ja: 14, 'zh-TW': 14 }; -const PADDING = 24; // Semi 表头左右内边距 + 排序图标余量 +const PADDING = 24; // 表头左右内边距及排序图标的估算余量 function walk(dir, files = []) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { diff --git a/scripts/ui-dependency-policy.cjs b/scripts/ui-dependency-policy.cjs new file mode 100644 index 00000000..18912f3d --- /dev/null +++ b/scripts/ui-dependency-policy.cjs @@ -0,0 +1,64 @@ +const forbiddenPackages = [ + /^@(?:radix-ui|floating-ui|lobehub|headlessui|mui|material-ui|chakra-ui)\//, + /^@douyinfe\/semi-/, + /^(?:leva|antd-style|react-layout-kit)(?:[/?#]|$)/, +]; + +function isForbiddenUiDependency(specifier) { + return ( + typeof specifier === 'string' && + forbiddenPackages.some((pattern) => pattern.test(specifier)) + ); +} + +const rule = { + meta: { + schema: [], + messages: { + forbidden: + 'Use Ant Design or the audited local brand assets instead of {{specifier}}.', + }, + }, + create(context) { + const inspect = (node) => { + const specifier = + node?.type === 'Literal' + ? node.value + : node?.type === 'TemplateLiteral' && node.expressions.length === 0 + ? node.quasis[0].value.cooked + : null; + + if (isForbiddenUiDependency(specifier)) { + context.report({ node, messageId: 'forbidden', data: { specifier } }); + } + }; + + return { + ImportDeclaration: (node) => inspect(node.source), + ExportNamedDeclaration: (node) => inspect(node.source), + ExportAllDeclaration: (node) => inspect(node.source), + ImportExpression: (node) => inspect(node.source), + CallExpression: (node) => { + const { callee } = node; + const isRequire = + callee.type === 'Identifier' && callee.name === 'require'; + const isRequireResolve = + callee.type === 'MemberExpression' && + !callee.computed && + callee.object.type === 'Identifier' && + callee.object.name === 'require' && + callee.property.name === 'resolve'; + if (isRequire || isRequireResolve) inspect(node.arguments[0]); + }, + }; + }, +}; + +const uiDependencyConfig = { + files: ['**/*.{js,jsx,cjs,mjs}'], + languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } }, + plugins: { 'ui-dependencies': { rules: { 'no-legacy-ui': rule } } }, + rules: { 'ui-dependencies/no-legacy-ui': 'error' }, +}; + +module.exports = { isForbiddenUiDependency, uiDependencyConfig }; diff --git a/scripts/vite/legacy-css-layers.js b/scripts/vite/legacy-css-layers.js index 8ae4473c..4b83abd5 100644 --- a/scripts/vite/legacy-css-layers.js +++ b/scripts/vite/legacy-css-layers.js @@ -3,7 +3,6 @@ import postcss from 'postcss'; // 旧浏览器的级联层(@layer)兜底。 // // index.css 把 Tailwind 的 base / components / utilities 整体包在原生 @layer 里, -// Semi 的按需样式也由 vitePluginSemi({ cssLayer: true }) 包进 @layer semi。 // @layer 自 Chromium 99 / Firefox 97 / Safari 15.4 才支持;更老的内核(停更的 // Edge、Win10 LTSC 自带 Edge、各类国产壳浏览器)把它当未知 at-rule 连同块内 // 全部规则一起丢弃 —— Tailwind 整体失效,页面只剩无尺寸约束的原图 logo。 @@ -72,8 +71,7 @@ export function flattenCssLayers(sources) { roots.forEach(assertSupportedShape); // 层外已有逐字相同的规则时,层内那份不必收:选择器与声明都一样、层外的又加载 - // 在后,它在旧浏览器里必然被覆盖。Semi 的 @layer 副本整份属于这种情况 - // (index.jsx 另行全量引入了层外的 semi.css),剔掉后兜底表只剩约五分之一。 + // 在后,它在旧浏览器里必然被覆盖。 const unlayered = new Set(); for (const root of roots) { root.each((node) => { diff --git a/src/pages/ExperienceCenter/README.md b/src/pages/ExperienceCenter/README.md index 07cdc1ea..a000d275 100644 --- a/src/pages/ExperienceCenter/README.md +++ b/src/pages/ExperienceCenter/README.md @@ -74,7 +74,7 @@ web/src/pages/ExperienceCenter/ ## 技术栈 - React 18+ -- Semi Design UI 组件库 +- Ant Design(存量组件通过 `@/components/ui` 适配层使用) - React Router v6 - i18next(国际化) diff --git a/src/pages/ExperienceCenter/components/AICreation/ImageTaskList-README.md b/src/pages/ExperienceCenter/components/AICreation/ImageTaskList-README.md index 9e1635f3..2cc35a11 100644 --- a/src/pages/ExperienceCenter/components/AICreation/ImageTaskList-README.md +++ b/src/pages/ExperienceCenter/components/AICreation/ImageTaskList-README.md @@ -93,7 +93,7 @@ - 已完成:绿色 (green) - 失败:红色 (red) - **渐变背景**:顶部操作栏使用微妙的渐变 -- **统一风格**:遵循 Semi Design 设计规范 +- **统一风格**:遵循项目的 Ant Design 组件规范 #### 响应式设计 diff --git a/test/unit/ui-dependency-policy.test.js b/test/unit/ui-dependency-policy.test.js new file mode 100644 index 00000000..993a5794 --- /dev/null +++ b/test/unit/ui-dependency-policy.test.js @@ -0,0 +1,84 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { ESLint } from 'eslint'; +import { + auditManifest, + auditSource, + checkUiDependencies, +} from '../../scripts/check-ui-dependencies.mjs'; + +test('ESLint and the UI audit enforce the same rule in every JS file format', async () => { + const eslint = new ESLint(); + const cases = [ + ['src/fixture.jsx', "import { Button } from '@douyinfe/semi-ui';"], + ['src/fixture.js', "export { item } from '@lobehub/ui';"], + ['src/fixture.js', "export * from '@lobehub/other-package';"], + ['scripts/fixture.mjs', "import('@material-ui/core');"], + ['scripts/fixture.mjs', 'import(`antd-style/css?raw`);'], + ['scripts/fixture.cjs', "require('@radix-ui/react-dialog');"], + ['scripts/fixture.cjs', "require.resolve('react-layout-kit/theme');"], + ]; + for (const [filePath, source] of cases) { + const [result] = await eslint.lintText(source, { filePath }); + const diagnostics = result.messages.filter( + ({ ruleId }) => ruleId === 'ui-dependencies/no-legacy-ui', + ); + assert.equal(diagnostics.length, 1, `${filePath}: ${source}`); + assert.deepEqual(auditSource(source, filePath), [ + `${filePath}:${diagnostics[0].line}: ${diagnostics[0].message}`, + ]); + } +}); + +test('the UI audit allows approved imports, local assets and provenance text', () => { + const source = ` + import 'antd'; + import '@xyflow/react'; + import '@/components/ui'; + import './assets/brand-icons/lobehub.js'; + // import 'antd-style'; + const upstream = '@lobehub/icons'; + `; + assert.deepEqual(auditSource(source, 'src/fixture.jsx'), []); + assert.equal(auditSource('import {', 'scripts/fixture.mjs').length, 1); + assert.equal( + auditSource( + "/* eslint-disable ui-dependencies/no-legacy-ui */\nimport 'leva';", + 'scripts/fixture.mjs', + ).length, + 1, + ); +}); + +test('the UI audit checks direct, aliased and nested locked dependencies', () => { + const manifest = { + dependencies: { antd: '6.7.0', 'legacy-ui': 'npm:@mui/material@^7' }, + devDependencies: { '@douyinfe/semi-animation': '1.0.0' }, + optionalDependencies: { 'old-style': 'npm:antd-style' }, + peerDependencies: { '@headlessui/react': '^2' }, + }; + const lock = { + lockfileVersion: 3, + packages: { + '': {}, + 'node_modules/antd': {}, + 'node_modules/nested/node_modules/@floating-ui/dom': {}, + 'node_modules/nested/node_modules/other-name': { name: '@lobehub/ui' }, + }, + }; + assert.deepEqual(auditManifest(manifest, lock), [ + 'package.json dependencies: legacy-ui -> @mui/material', + 'package.json devDependencies: @douyinfe/semi-animation', + 'package.json optionalDependencies: old-style -> antd-style', + 'package.json peerDependencies: @headlessui/react', + 'package-lock.json: node_modules/nested/node_modules/@floating-ui/dom', + 'package-lock.json: node_modules/nested/node_modules/other-name -> @lobehub/ui', + ]); +}); + +test('the checkout passes the source, lock and brand icon audits', async () => { + const result = await checkUiDependencies(); + assert.deepEqual(result.issues, []); + assert.ok(result.files > 100); + assert.ok(result.packages > 100); +});