-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥feat: support load on demand (#10)
- Loading branch information
1 parent
3f1d496
commit 7662962
Showing
2 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} |