refactor(ui-deps): 统一组件依赖审计与回归规则
This commit is contained in:
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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 })) {
|
||||
|
||||
@@ -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 };
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user