Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: icons插件增加ant-design/icons类型提示 #11891

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/preset-umi/src/features/icons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export default (api: IApi) => {
const localIconDir = getLocalIconDir();
const localIcons: string[] = [];

const antIconsComponents = getAntIconsComponents();

if (fs.existsSync(localIconDir)) {
localIcons.push(
...readIconsFromDir(localIconDir)
Expand All @@ -155,10 +157,11 @@ const alias = ${JSON.stringify(api.config.icons.alias || {})};
type AliasKeys = keyof typeof alias;
const localIcons = ${JSON.stringify(localIcons)} as const;
type LocalIconsKeys = typeof localIcons[number];
const antIcons = ${JSON.stringify(antIconsComponents)} as const;
type AntIconsKeys = typeof antIcons[number];
fz6m marked this conversation as resolved.
Show resolved Hide resolved

type IconCollections = 'academicons' |
'akar-icons' |
'ant-design' |
'arcticons' |
'basil' |
'bi' |
Expand Down Expand Up @@ -306,7 +309,7 @@ type IconCollections = 'academicons' |
type Icon = \`\${IconCollections}:\${string}\`;

interface IUmiIconProps extends React.SVGAttributes<SVGElement> {
icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\`;
icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\` | \`ant-design:\${AntIconsKeys}\`;
hover?: AliasKeys | string;
className?: string;
viewBox?: string;
Expand Down Expand Up @@ -451,3 +454,15 @@ function readIconsFromDir(dir: string) {

return icons;
}

function getAntIconsComponents() {
const antIconsFilePath = require.resolve('@ant-design/icons/es/icons/index.js');
fz6m marked this conversation as resolved.
Show resolved Hide resolved
const contents = fs.readFileSync(antIconsFilePath, 'utf-8');
const matches = contents.match(/default\sas\s([^\s]+)\s}\sfrom\s\'\.\/([^\']+)/g);
const antIconsComponents = matches!.map((match) => {
const [_, componentName] = match.match(/default\sas\s([^\s]+)\s}\sfrom\s\'\.\/([^\']+)/)!;
// 驼峰转横杠:ManOutlined --> man-outlined
return componentName.replace(/\B([A-Z])/g, '-$1').toLowerCase();
});
return antIconsComponents;
}