Skip to content

Commit

Permalink
💥feat: support load on demand (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 authored Apr 4, 2020
1 parent 3f1d496 commit 7662962
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const react = require('react');
const allIcons = require('@ant-design/icons/es/icons');
// @ts-ignore
import allIcons from '@@/plugin-antd-icon/icons';

export interface MenuDataItem {
children?: MenuDataItem[];
Expand All @@ -13,9 +14,8 @@ export interface MenuDataItem {
path?: string;
[key: string]: any;
}

function toHump(name: string) {
return name.replace(/\-(\w)/g, function(all, letter) {
return name.replace(/\-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
}
Expand Down
60 changes: 59 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
import { IApi } from 'umi';
import * as allIcons from '@ant-design/icons';

export default function(api: IApi) {
export interface MenuDataItem {
children?: MenuDataItem[];
routes?: MenuDataItem[];
hideChildrenInMenu?: boolean;
hideInMenu?: boolean;
icon?: string;
locale?: string;
name?: string;
key?: string;
path?: string;
[key: string]: any;
}

function toHump(name: string) {
return name.replace(/\-(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
}

function formatter(data: MenuDataItem[]): MenuDataItem[] {
let icons = [];
data.forEach((item = { path: '/' }) => {
if (item.icon) {
const { icon } = item;
const v4IconName = toHump(icon.replace(icon[0], icon[0].toUpperCase()));
if (allIcons[icon]) {
icons.push(icon);
}
if (allIcons[`${v4IconName}Outlined`]) {
icons.push(`${v4IconName}Outlined`);
}
}
if (item.routes || item.children) {
icons = icons.concat(formatter(item.routes || item.children));
}
});

return Array.from(new Set(icons));
}

export default function (api: IApi) {
api.onGenerateFiles(() => {
const { userConfig } = api;
const icons = formatter(userConfig.routes);
let iconsString = icons.map(
(iconName) => `import ${iconName} from '@ant-design/icons/${iconName}'`,
);
api.writeTmpFile({
path: './plugin-antd-icon/icons.ts',
content: `
${iconsString.join(';\n')}
export default {
${icons.join(',\n')}
}
`,
});
});
api.addRuntimePlugin(() => require.resolve('./app.js'));
}

0 comments on commit 7662962

Please sign in to comment.