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 support secondary #10965

Merged
merged 5 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion examples/max/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ export default defineConfig({
jsStrategy: 'granularChunks',
},
icons: {
include: ['local:rice', 'ant-design:fire-twotone'],
include: ['local:rice', 'local:logo/umi', 'ant-design:fire-twotone'],
},
});
1 change: 1 addition & 0 deletions examples/max/cypress/e2e/smoke.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ describe('Basic Test', () => {
it('display included Icon components', () => {
cy.get('span.local\\:rice');
cy.get('span.ant-design\\:fire-twotone');
cy.get('span.local\\:logo\\/umi');
});
});
10 changes: 10 additions & 0 deletions examples/max/icons/logo/foo/smile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/max/icons/logo/umi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 9 additions & 4 deletions examples/max/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { Button, DatePicker, Input } from 'antd';
import styles from './index.less';
console.log(TestDecorator);

const icons = ['local:rice', 'ant-design:fire-twotone'];
const includedIcons = [
'local:rice',
'local:logo/umi',
'ant-design:fire-twotone',
];

export default function HomePage() {
const { initialState } = useModel('@@initialState');
Expand Down Expand Up @@ -45,9 +49,10 @@ export default function HomePage() {

<h2> Icons</h2>
<div>
{icons.map((i) => (
<Icon key={i} icon={i} className={i} />
))}
{includedIcons.map((i) => {
return <Icon icon={i} className={i} key={i} />;
})}
<Icon icon="local:logo/foo/smile" />
</div>
</div>
);
Expand Down
27 changes: 24 additions & 3 deletions packages/preset-umi/src/features/icons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
importLazy,
installWithNpmClient,
logger,
winPath,
} from '@umijs/utils';
import fs from 'fs';
import path from 'path';
Expand Down Expand Up @@ -135,10 +136,10 @@ export default (api: IApi) => {
}
const localIconDir = getLocalIconDir();
const localIcons: string[] = [];

if (fs.existsSync(localIconDir)) {
localIcons.push(
...fs
.readdirSync(localIconDir)
...readIconsFromDir(localIconDir)
.filter((file) => file.endsWith('.svg'))
.map((file) => file.replace(/\.svg$/, '')),
);
Expand Down Expand Up @@ -380,7 +381,7 @@ function normalizeRotate(rotate: number | string) {
}

function camelCase(str: string) {
return str.replace(/-([a-z]|[1-9])/g, (g) => g[1].toUpperCase());
return str.replace(/\\//g, '-').replace(/-([a-z]|[1-9])/g, (g) => g[1].toUpperCase());
}

function normalizeIconName(name: string) {
Expand Down Expand Up @@ -430,3 +431,23 @@ function normalizeIconName(name: string) {
return path.join(api.paths.absSrcPath, 'icons');
}
};

function readIconsFromDir(dir: string) {
const icons: string[] = [];
const prefix = winPath(path.join(dir, './'));

const collect = (p: string) => {
if (fs.statSync(p).isDirectory()) {
const files = fs.readdirSync(p);
files.forEach((name) => {
collect(path.join(p, name));
});
} else {
const prunePath = winPath(p).replace(prefix, '');
icons.push(prunePath);
}
};
collect(dir);

return icons;
}
4 changes: 3 additions & 1 deletion packages/preset-umi/src/features/icons/svgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type { IApi } from '../../types';
import { loadIcon } from './loadIcon';

function camelCase(str: string) {
return str.replace(/-([a-z]|[0-9])/g, (g) => g[1].toUpperCase());
return str
.replace(/\//g, '-')
.replace(/-([a-z]|[0-9])/g, (g) => g[1].toUpperCase());
}

export function generateIconName(opts: { collect: string; icon: string }) {
Expand Down