From ab1a843d5b1841fb1d57bdc9f918f4deeb6ff801 Mon Sep 17 00:00:00 2001 From: wy187819 Date: Wed, 22 Nov 2023 18:07:56 +0800 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20icons=E6=8F=92=E4=BB=B6=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0ant-design/icons=E7=B1=BB=E5=9E=8B=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/preset-umi/src/features/icons/icons.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/preset-umi/src/features/icons/icons.ts b/packages/preset-umi/src/features/icons/icons.ts index 35d597cd2f57..deaea85fae42 100644 --- a/packages/preset-umi/src/features/icons/icons.ts +++ b/packages/preset-umi/src/features/icons/icons.ts @@ -137,6 +137,15 @@ export default (api: IApi) => { const localIconDir = getLocalIconDir(); const localIcons: string[] = []; + const antIconsFilePath = require.resolve('@ant-design/icons/es/icons/index.js'); + 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(); + }); + if (fs.existsSync(localIconDir)) { localIcons.push( ...readIconsFromDir(localIconDir) @@ -155,10 +164,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]; type IconCollections = 'academicons' | 'akar-icons' | - 'ant-design' | 'arcticons' | 'basil' | 'bi' | @@ -306,7 +316,7 @@ type IconCollections = 'academicons' | type Icon = \`\${IconCollections}:\${string}\`; interface IUmiIconProps extends React.SVGAttributes { - icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\`; + icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\` | \`ant-design:\${AntIconsKeys}\`; hover?: AliasKeys | string; className?: string; viewBox?: string; From b0d8d5e701a8df8f115838055423e125cc595636 Mon Sep 17 00:00:00 2001 From: wy187819 Date: Thu, 23 Nov 2023 09:44:12 +0800 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=E6=8F=90=E5=8F=96getAntIconsCompone?= =?UTF-8?q?nts=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../preset-umi/src/features/icons/icons.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/preset-umi/src/features/icons/icons.ts b/packages/preset-umi/src/features/icons/icons.ts index deaea85fae42..cbefc9bba7c6 100644 --- a/packages/preset-umi/src/features/icons/icons.ts +++ b/packages/preset-umi/src/features/icons/icons.ts @@ -137,14 +137,7 @@ export default (api: IApi) => { const localIconDir = getLocalIconDir(); const localIcons: string[] = []; - const antIconsFilePath = require.resolve('@ant-design/icons/es/icons/index.js'); - 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(); - }); + const antIconsComponents = getAntIconsComponents(); if (fs.existsSync(localIconDir)) { localIcons.push( @@ -461,3 +454,15 @@ function readIconsFromDir(dir: string) { return icons; } + +function getAntIconsComponents() { + const antIconsFilePath = require.resolve('@ant-design/icons/es/icons/index.js'); + 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; +} From 3e57e6d04d5b1b61367450e2becf25528ac23ab5 Mon Sep 17 00:00:00 2001 From: wy187819 Date: Thu, 23 Nov 2023 09:52:19 +0800 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=E8=8E=B7=E5=8F=96antIcons=E6=94=BE?= =?UTF-8?q?=E5=88=B0onGenerateFiles=E5=A4=96=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/preset-umi/src/features/icons/icons.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/preset-umi/src/features/icons/icons.ts b/packages/preset-umi/src/features/icons/icons.ts index cbefc9bba7c6..4a36f2298d22 100644 --- a/packages/preset-umi/src/features/icons/icons.ts +++ b/packages/preset-umi/src/features/icons/icons.ts @@ -126,6 +126,8 @@ export default (api: IApi) => { }); }); + const antIconsComponents = getAntIconsComponents(); + api.onGenerateFiles(({ isFirstTime }) => { // ensure first time file exist for esbuild resolve if (isFirstTime) { @@ -137,8 +139,6 @@ export default (api: IApi) => { const localIconDir = getLocalIconDir(); const localIcons: string[] = []; - const antIconsComponents = getAntIconsComponents(); - if (fs.existsSync(localIconDir)) { localIcons.push( ...readIconsFromDir(localIconDir) From 4f6933ea970736f6ee84bbb2b365d4976b1762a4 Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Thu, 23 Nov 2023 20:50:48 +0800 Subject: [PATCH 4/7] refactor: resolve project dep func --- packages/plugins/src/antd.ts | 8 +++-- packages/plugins/src/layout.ts | 9 ++++-- packages/plugins/src/react-query.ts | 3 +- .../features/configPlugins/configPlugins.ts | 15 +--------- packages/preset-vue/src/features/default.ts | 3 +- .../preset-vue/src/utils/resolveProjectDep.ts | 30 ------------------- .../preset-vue/src/utils/resolveVuePath.ts | 13 ++++++++ packages/utils/src/index.ts | 1 + .../utils => utils/src}/resolveProjectDep.ts | 2 +- 9 files changed, 32 insertions(+), 52 deletions(-) delete mode 100644 packages/preset-vue/src/utils/resolveProjectDep.ts create mode 100644 packages/preset-vue/src/utils/resolveVuePath.ts rename packages/{plugins/src/utils => utils/src}/resolveProjectDep.ts (88%) diff --git a/packages/plugins/src/antd.ts b/packages/plugins/src/antd.ts index e7cf1ceece29..0e29a20cbc86 100644 --- a/packages/plugins/src/antd.ts +++ b/packages/plugins/src/antd.ts @@ -3,9 +3,13 @@ import AntdMomentWebpackPlugin from '@ant-design/moment-webpack-plugin'; import assert from 'assert'; import { dirname, join } from 'path'; import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi'; -import { deepmerge, semver, winPath } from 'umi/plugin-utils'; +import { + deepmerge, + resolveProjectDep, + semver, + winPath, +} from 'umi/plugin-utils'; import { TEMPLATES_DIR } from './constants'; -import { resolveProjectDep } from './utils/resolveProjectDep'; import { withTmpPath } from './utils/withTmpPath'; const ANTD_TEMPLATES_DIR = join(TEMPLATES_DIR, 'antd'); diff --git a/packages/plugins/src/layout.ts b/packages/plugins/src/layout.ts index 796dd20b8dd0..674d4ee07351 100644 --- a/packages/plugins/src/layout.ts +++ b/packages/plugins/src/layout.ts @@ -1,8 +1,13 @@ import { existsSync, readFileSync } from 'fs'; import { dirname, join } from 'path'; import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi'; -import { lodash, Mustache, NpmClientEnum, winPath } from 'umi/plugin-utils'; -import { resolveProjectDep } from './utils/resolveProjectDep'; +import { + lodash, + Mustache, + NpmClientEnum, + resolveProjectDep, + winPath, +} from 'umi/plugin-utils'; import { withTmpPath } from './utils/withTmpPath'; // 获取所有 icons diff --git a/packages/plugins/src/react-query.ts b/packages/plugins/src/react-query.ts index ae07f5cb3151..3e624d370927 100644 --- a/packages/plugins/src/react-query.ts +++ b/packages/plugins/src/react-query.ts @@ -1,7 +1,6 @@ -import { winPath } from '@umijs/utils'; import { dirname } from 'path'; import { IApi } from 'umi'; -import { resolveProjectDep } from './utils/resolveProjectDep'; +import { resolveProjectDep, winPath } from 'umi/plugin-utils'; import { withTmpPath } from './utils/withTmpPath'; export default (api: IApi) => { diff --git a/packages/preset-umi/src/features/configPlugins/configPlugins.ts b/packages/preset-umi/src/features/configPlugins/configPlugins.ts index 2034004d5bef..98835113ba43 100644 --- a/packages/preset-umi/src/features/configPlugins/configPlugins.ts +++ b/packages/preset-umi/src/features/configPlugins/configPlugins.ts @@ -1,23 +1,10 @@ import { getSchemas as getViteSchemas } from '@umijs/bundler-vite/dist/schema'; import { getSchemas as getWebpackSchemas } from '@umijs/bundler-webpack/dist/schema'; -import { resolve } from '@umijs/utils'; +import { resolveProjectDep } from '@umijs/utils'; import { dirname, join } from 'path'; import type { IApi } from '../../types'; import { getSchemas as getExtraSchemas } from './schema'; -function resolveProjectDep(opts: { pkg: any; cwd: string; dep: string }) { - if ( - opts.pkg.dependencies?.[opts.dep] || - opts.pkg.devDependencies?.[opts.dep] - ) { - return dirname( - resolve.sync(`${opts.dep}/package.json`, { - basedir: opts.cwd, - }), - ); - } -} - export default (api: IApi) => { const { userConfig } = api; const reactDOMPath = diff --git a/packages/preset-vue/src/features/default.ts b/packages/preset-vue/src/features/default.ts index c858afe537d2..62e9a0f683f3 100644 --- a/packages/preset-vue/src/features/default.ts +++ b/packages/preset-vue/src/features/default.ts @@ -1,6 +1,7 @@ +import { resolveProjectDep } from '@umijs/utils'; import { dirname } from 'path'; import type { IApi } from 'umi'; -import { resolveProjectDep, resolveVuePath } from '../utils/resolveProjectDep'; +import { resolveVuePath } from '../utils/resolveVuePath'; export default (api: IApi) => { api.describe({ diff --git a/packages/preset-vue/src/utils/resolveProjectDep.ts b/packages/preset-vue/src/utils/resolveProjectDep.ts deleted file mode 100644 index 0ab48775ba1b..000000000000 --- a/packages/preset-vue/src/utils/resolveProjectDep.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { dirname, join } from 'path'; -import { resolve } from 'umi/plugin-utils'; - -export function resolveProjectDep(opts: { - pkg: any; - cwd: string; - dep: string; -}) { - if ( - opts.pkg.dependencies?.[opts.dep] || - opts.pkg.devDependencies?.[opts.dep] - ) { - return dirname( - resolve.sync(`${opts.dep}/package.json`, { - basedir: opts.cwd, - }), - ); - } -} - -export function resolveVuePath(opts: { pkg: any; cwd: string; path: string }) { - const vuePkgPath = - resolveProjectDep({ - pkg: opts.pkg, - cwd: opts.cwd, - dep: 'vue', - }) || dirname(require.resolve('vue/package.json')); - - return join(vuePkgPath, opts.path); -} diff --git a/packages/preset-vue/src/utils/resolveVuePath.ts b/packages/preset-vue/src/utils/resolveVuePath.ts new file mode 100644 index 000000000000..2765e56723e4 --- /dev/null +++ b/packages/preset-vue/src/utils/resolveVuePath.ts @@ -0,0 +1,13 @@ +import { resolveProjectDep } from '@umijs/utils'; +import { dirname, join } from 'path'; + +export function resolveVuePath(opts: { pkg: any; cwd: string; path: string }) { + const vuePkgPath = + resolveProjectDep({ + pkg: opts.pkg, + cwd: opts.cwd, + dep: 'vue', + }) || dirname(require.resolve('vue/package.json')); + + return join(vuePkgPath, opts.path); +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 574ac11cebe9..8b7b4e24ca09 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -50,6 +50,7 @@ export * from './npmClient'; export * from './randomColor/randomColor'; export * from './readDirFiles'; export * as register from './register'; +export * from './resolveProjectDep'; export * from './setNoDeprecation'; export * from './tryPaths'; export * from './winPath'; diff --git a/packages/plugins/src/utils/resolveProjectDep.ts b/packages/utils/src/resolveProjectDep.ts similarity index 88% rename from packages/plugins/src/utils/resolveProjectDep.ts rename to packages/utils/src/resolveProjectDep.ts index ab487064faa9..7b1406712e9b 100644 --- a/packages/plugins/src/utils/resolveProjectDep.ts +++ b/packages/utils/src/resolveProjectDep.ts @@ -1,5 +1,5 @@ import { dirname } from 'path'; -import { resolve } from 'umi/plugin-utils'; +import resolve from '../compiled/resolve'; export function resolveProjectDep(opts: { pkg: any; From 09f9550c827a599020e62ad1f5e95f1b5350baf9 Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Thu, 23 Nov 2023 22:14:28 +0800 Subject: [PATCH 5/7] refactor: ... --- .../preset-umi/src/features/icons/icons.ts | 82 +++++++++++++++---- 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/packages/preset-umi/src/features/icons/icons.ts b/packages/preset-umi/src/features/icons/icons.ts index 4a36f2298d22..8d31f74021fd 100644 --- a/packages/preset-umi/src/features/icons/icons.ts +++ b/packages/preset-umi/src/features/icons/icons.ts @@ -3,6 +3,7 @@ import { importLazy, installWithNpmClient, logger, + resolve, winPath, } from '@umijs/utils'; import fs from 'fs'; @@ -11,6 +12,18 @@ import path from 'path'; import type { IApi } from '../../types'; import { addDeps } from '../depsOnDemand/depsOnDemand'; +interface IIconsType { + iconsType: string; +} + +interface IIconsJson { + prefix?: string; + icons?: Record; + aliases?: Record; +} + +const ICON_TYPES_WHITE_LIST = ['ant-design']; + export default (api: IApi) => { const iconPlugin: typeof import('./esbuildIconPlugin') = importLazy( require.resolve('./esbuildIconPlugin'), @@ -126,7 +139,57 @@ export default (api: IApi) => { }); }); - const antIconsComponents = getAntIconsComponents(); + function getIconsType() { + const pkg = api.pkg; + const prefix = `@iconify-json`; + const iconifyDeps = Object.keys({ + ...pkg?.dependencies, + ...pkg?.devDependencies, + }).filter((name) => name.startsWith(`${prefix}/`)); + const icons: IIconsType[] = []; + iconifyDeps.forEach((name) => { + try { + const pkgPath = resolve.sync(`${name}/icons.json`, { + basedir: api.cwd, + }); + const map = require(pkgPath) as IIconsJson; + if (!map?.prefix?.length) { + return; + } + const isWhite = ICON_TYPES_WHITE_LIST.some( + (prefix) => prefix === map.prefix, + ); + if (!isWhite) { + return; + } + const iconKeys = Object.keys({ + ...map?.icons, + ...map?.aliases, + }); + const iconsType = iconKeys + .map((name) => { + return `\`${map.prefix}:${name}\``; + }) + .join(' | '); + icons.push({ + iconsType, + }); + } catch {} + }); + const finalIconifyDeps: IIconsType = icons.reduce( + (memo, curr) => { + return { + iconsType: `${memo.iconsType} | ${curr.iconsType}`, + }; + }, + { + iconsType: '', + }, + ); + return finalIconifyDeps; + } + + const iconsType = getIconsType(); api.onGenerateFiles(({ isFirstTime }) => { // ensure first time file exist for esbuild resolve @@ -157,8 +220,7 @@ 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]; +${iconsType.iconsMapType} type IconCollections = 'academicons' | 'akar-icons' | @@ -309,7 +371,7 @@ type IconCollections = 'academicons' | type Icon = \`\${IconCollections}:\${string}\`; interface IUmiIconProps extends React.SVGAttributes { - icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\` | \`ant-design:\${AntIconsKeys}\`; + icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\` ${iconsType.iconsType}; hover?: AliasKeys | string; className?: string; viewBox?: string; @@ -454,15 +516,3 @@ function readIconsFromDir(dir: string) { return icons; } - -function getAntIconsComponents() { - const antIconsFilePath = require.resolve('@ant-design/icons/es/icons/index.js'); - 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; -} From c291e4083e4de53f33087042358609b18aa6f22e Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Thu, 23 Nov 2023 22:14:41 +0800 Subject: [PATCH 6/7] Revert "refactor: resolve project dep func" This reverts commit 4f6933ea970736f6ee84bbb2b365d4976b1762a4. --- packages/plugins/src/antd.ts | 8 ++--- packages/plugins/src/layout.ts | 9 ++---- packages/plugins/src/react-query.ts | 3 +- .../src/utils}/resolveProjectDep.ts | 2 +- .../features/configPlugins/configPlugins.ts | 15 +++++++++- packages/preset-vue/src/features/default.ts | 3 +- .../preset-vue/src/utils/resolveProjectDep.ts | 30 +++++++++++++++++++ .../preset-vue/src/utils/resolveVuePath.ts | 13 -------- packages/utils/src/index.ts | 1 - 9 files changed, 52 insertions(+), 32 deletions(-) rename packages/{utils/src => plugins/src/utils}/resolveProjectDep.ts (88%) create mode 100644 packages/preset-vue/src/utils/resolveProjectDep.ts delete mode 100644 packages/preset-vue/src/utils/resolveVuePath.ts diff --git a/packages/plugins/src/antd.ts b/packages/plugins/src/antd.ts index 0e29a20cbc86..e7cf1ceece29 100644 --- a/packages/plugins/src/antd.ts +++ b/packages/plugins/src/antd.ts @@ -3,13 +3,9 @@ import AntdMomentWebpackPlugin from '@ant-design/moment-webpack-plugin'; import assert from 'assert'; import { dirname, join } from 'path'; import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi'; -import { - deepmerge, - resolveProjectDep, - semver, - winPath, -} from 'umi/plugin-utils'; +import { deepmerge, semver, winPath } from 'umi/plugin-utils'; import { TEMPLATES_DIR } from './constants'; +import { resolveProjectDep } from './utils/resolveProjectDep'; import { withTmpPath } from './utils/withTmpPath'; const ANTD_TEMPLATES_DIR = join(TEMPLATES_DIR, 'antd'); diff --git a/packages/plugins/src/layout.ts b/packages/plugins/src/layout.ts index 674d4ee07351..796dd20b8dd0 100644 --- a/packages/plugins/src/layout.ts +++ b/packages/plugins/src/layout.ts @@ -1,13 +1,8 @@ import { existsSync, readFileSync } from 'fs'; import { dirname, join } from 'path'; import { IApi, RUNTIME_TYPE_FILE_NAME } from 'umi'; -import { - lodash, - Mustache, - NpmClientEnum, - resolveProjectDep, - winPath, -} from 'umi/plugin-utils'; +import { lodash, Mustache, NpmClientEnum, winPath } from 'umi/plugin-utils'; +import { resolveProjectDep } from './utils/resolveProjectDep'; import { withTmpPath } from './utils/withTmpPath'; // 获取所有 icons diff --git a/packages/plugins/src/react-query.ts b/packages/plugins/src/react-query.ts index 3e624d370927..ae07f5cb3151 100644 --- a/packages/plugins/src/react-query.ts +++ b/packages/plugins/src/react-query.ts @@ -1,6 +1,7 @@ +import { winPath } from '@umijs/utils'; import { dirname } from 'path'; import { IApi } from 'umi'; -import { resolveProjectDep, winPath } from 'umi/plugin-utils'; +import { resolveProjectDep } from './utils/resolveProjectDep'; import { withTmpPath } from './utils/withTmpPath'; export default (api: IApi) => { diff --git a/packages/utils/src/resolveProjectDep.ts b/packages/plugins/src/utils/resolveProjectDep.ts similarity index 88% rename from packages/utils/src/resolveProjectDep.ts rename to packages/plugins/src/utils/resolveProjectDep.ts index 7b1406712e9b..ab487064faa9 100644 --- a/packages/utils/src/resolveProjectDep.ts +++ b/packages/plugins/src/utils/resolveProjectDep.ts @@ -1,5 +1,5 @@ import { dirname } from 'path'; -import resolve from '../compiled/resolve'; +import { resolve } from 'umi/plugin-utils'; export function resolveProjectDep(opts: { pkg: any; diff --git a/packages/preset-umi/src/features/configPlugins/configPlugins.ts b/packages/preset-umi/src/features/configPlugins/configPlugins.ts index 98835113ba43..2034004d5bef 100644 --- a/packages/preset-umi/src/features/configPlugins/configPlugins.ts +++ b/packages/preset-umi/src/features/configPlugins/configPlugins.ts @@ -1,10 +1,23 @@ import { getSchemas as getViteSchemas } from '@umijs/bundler-vite/dist/schema'; import { getSchemas as getWebpackSchemas } from '@umijs/bundler-webpack/dist/schema'; -import { resolveProjectDep } from '@umijs/utils'; +import { resolve } from '@umijs/utils'; import { dirname, join } from 'path'; import type { IApi } from '../../types'; import { getSchemas as getExtraSchemas } from './schema'; +function resolveProjectDep(opts: { pkg: any; cwd: string; dep: string }) { + if ( + opts.pkg.dependencies?.[opts.dep] || + opts.pkg.devDependencies?.[opts.dep] + ) { + return dirname( + resolve.sync(`${opts.dep}/package.json`, { + basedir: opts.cwd, + }), + ); + } +} + export default (api: IApi) => { const { userConfig } = api; const reactDOMPath = diff --git a/packages/preset-vue/src/features/default.ts b/packages/preset-vue/src/features/default.ts index 62e9a0f683f3..c858afe537d2 100644 --- a/packages/preset-vue/src/features/default.ts +++ b/packages/preset-vue/src/features/default.ts @@ -1,7 +1,6 @@ -import { resolveProjectDep } from '@umijs/utils'; import { dirname } from 'path'; import type { IApi } from 'umi'; -import { resolveVuePath } from '../utils/resolveVuePath'; +import { resolveProjectDep, resolveVuePath } from '../utils/resolveProjectDep'; export default (api: IApi) => { api.describe({ diff --git a/packages/preset-vue/src/utils/resolveProjectDep.ts b/packages/preset-vue/src/utils/resolveProjectDep.ts new file mode 100644 index 000000000000..0ab48775ba1b --- /dev/null +++ b/packages/preset-vue/src/utils/resolveProjectDep.ts @@ -0,0 +1,30 @@ +import { dirname, join } from 'path'; +import { resolve } from 'umi/plugin-utils'; + +export function resolveProjectDep(opts: { + pkg: any; + cwd: string; + dep: string; +}) { + if ( + opts.pkg.dependencies?.[opts.dep] || + opts.pkg.devDependencies?.[opts.dep] + ) { + return dirname( + resolve.sync(`${opts.dep}/package.json`, { + basedir: opts.cwd, + }), + ); + } +} + +export function resolveVuePath(opts: { pkg: any; cwd: string; path: string }) { + const vuePkgPath = + resolveProjectDep({ + pkg: opts.pkg, + cwd: opts.cwd, + dep: 'vue', + }) || dirname(require.resolve('vue/package.json')); + + return join(vuePkgPath, opts.path); +} diff --git a/packages/preset-vue/src/utils/resolveVuePath.ts b/packages/preset-vue/src/utils/resolveVuePath.ts deleted file mode 100644 index 2765e56723e4..000000000000 --- a/packages/preset-vue/src/utils/resolveVuePath.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { resolveProjectDep } from '@umijs/utils'; -import { dirname, join } from 'path'; - -export function resolveVuePath(opts: { pkg: any; cwd: string; path: string }) { - const vuePkgPath = - resolveProjectDep({ - pkg: opts.pkg, - cwd: opts.cwd, - dep: 'vue', - }) || dirname(require.resolve('vue/package.json')); - - return join(vuePkgPath, opts.path); -} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 8b7b4e24ca09..574ac11cebe9 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -50,7 +50,6 @@ export * from './npmClient'; export * from './randomColor/randomColor'; export * from './readDirFiles'; export * as register from './register'; -export * from './resolveProjectDep'; export * from './setNoDeprecation'; export * from './tryPaths'; export * from './winPath'; From 0f780c66ab2e361262bc50645ef652278e9a41e4 Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Thu, 23 Nov 2023 22:36:38 +0800 Subject: [PATCH 7/7] fix: collections type --- .../preset-umi/src/features/icons/icons.ts | 330 +++++++++--------- 1 file changed, 169 insertions(+), 161 deletions(-) diff --git a/packages/preset-umi/src/features/icons/icons.ts b/packages/preset-umi/src/features/icons/icons.ts index 8d31f74021fd..5e702a2bfab8 100644 --- a/packages/preset-umi/src/features/icons/icons.ts +++ b/packages/preset-umi/src/features/icons/icons.ts @@ -13,7 +13,8 @@ import type { IApi } from '../../types'; import { addDeps } from '../depsOnDemand/depsOnDemand'; interface IIconsType { - iconsType: string; + type: string; + collections?: string; } interface IIconsJson { @@ -22,8 +23,6 @@ interface IIconsJson { aliases?: Record; } -const ICON_TYPES_WHITE_LIST = ['ant-design']; - export default (api: IApi) => { const iconPlugin: typeof import('./esbuildIconPlugin') = importLazy( require.resolve('./esbuildIconPlugin'), @@ -146,6 +145,7 @@ export default (api: IApi) => { ...pkg?.dependencies, ...pkg?.devDependencies, }).filter((name) => name.startsWith(`${prefix}/`)); + const collectionsExcludes: string[] = []; const icons: IIconsType[] = []; iconifyDeps.forEach((name) => { try { @@ -156,36 +156,34 @@ export default (api: IApi) => { if (!map?.prefix?.length) { return; } - const isWhite = ICON_TYPES_WHITE_LIST.some( - (prefix) => prefix === map.prefix, - ); - if (!isWhite) { - return; - } + collectionsExcludes.push(map.prefix); const iconKeys = Object.keys({ ...map?.icons, ...map?.aliases, }); - const iconsType = iconKeys + const type = iconKeys .map((name) => { return `\`${map.prefix}:${name}\``; }) .join(' | '); icons.push({ - iconsType, + type, }); } catch {} }); const finalIconifyDeps: IIconsType = icons.reduce( (memo, curr) => { return { - iconsType: `${memo.iconsType} | ${curr.iconsType}`, + type: `${memo.type} | ${curr.type}`, }; }, { - iconsType: '', + type: '', }, ); + finalIconifyDeps.collections = getCollectionsType({ + excludes: collectionsExcludes, + }); return finalIconifyDeps; } @@ -220,158 +218,12 @@ 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]; -${iconsType.iconsMapType} -type IconCollections = 'academicons' | - 'akar-icons' | - 'arcticons' | - 'basil' | - 'bi' | - 'bpmn' | - 'brandico' | - 'bx' | - 'bxl' | - 'bxs' | - 'bytesize' | - 'carbon' | - 'charm' | - 'ci' | - 'cib' | - 'cif' | - 'cil' | - 'circle-flags' | - 'circum' | - 'clarity' | - 'codicon' | - 'cryptocurrency-color' | - 'cryptocurrency' | - 'dashicons' | - 'ei' | - 'el' | - 'emblemicons' | - 'emojione-monotone' | - 'emojione-v1' | - 'emojione' | - 'entypo-social' | - 'entypo' | - 'eos-icons' | - 'ep' | - 'et' | - 'eva' | - 'fa-brands' | - 'fa-regular' | - 'fa-solid' | - 'fa' | - 'fa6-brands' | - 'fa6-regular' | - 'fa6-solid' | - 'fad' | - 'fe' | - 'feather' | - 'file-icons' | - 'flag' | - 'flagpack' | - 'flat-color-icons' | - 'flat-ui' | - 'fluent-emoji-flat' | - 'fluent-emoji-high-contrast' | - 'fluent-emoji' | - 'fluent-mdl2' | - 'fluent' | - 'fontelico' | - 'fontisto' | - 'foundation' | - 'fxemoji' | - 'gala' | - 'game-icons' | - 'geo' | - 'gg' | - 'gis' | - 'gridicons' | - 'grommet-icons' | - 'healthicons' | - 'heroicons-outline' | - 'heroicons-solid' | - 'heroicons' | - 'humbleicons' | - 'ic' | - 'icomoon-free' | - 'icon-park-outline' | - 'icon-park-solid' | - 'icon-park-twotone' | - 'icon-park' | - 'iconoir' | - 'icons8' | - 'il' | - 'ion' | - 'iwwa' | - 'jam' | - 'la' | - 'line-md' | - 'logos' | - 'ls' | - 'lucide' | - 'majesticons' | - 'maki' | - 'map' | - 'material-symbols' | - 'mdi-light' | - 'mdi' | - 'medical-icon' | - 'memory' | - 'mi' | - 'mingcute' | - 'mono-icons' | - 'nimbus' | - 'nonicons' | - 'noto-v1' | - 'noto' | - 'octicon' | - 'oi' | - 'ooui' | - 'openmoji' | - 'pajamas' | - 'pepicons-pop' | - 'pepicons-print' | - 'pepicons' | - 'ph' | - 'pixelarticons' | - 'prime' | - 'ps' | - 'quill' | - 'radix-icons' | - 'raphael' | - 'ri' | - 'si-glyph' | - 'simple-icons' | - 'simple-line-icons' | - 'skill-icons' | - 'subway' | - 'svg-spinners' | - 'system-uicons' | - 'tabler' | - 'teenyicons' | - 'topcoat' | - 'twemoji' | - 'typcn' | - 'uil' | - 'uim' | - 'uis' | - 'uit' | - 'uiw' | - 'vaadin' | - 'vs' | - 'vscode-icons' | - 'websymbol' | - 'whh' | - 'wi' | - 'wpf' | - 'zmdi' | - 'zondicons'; +type IconCollections = ${iconsType.collections}; type Icon = \`\${IconCollections}:\${string}\`; interface IUmiIconProps extends React.SVGAttributes { - icon: AliasKeys | Icon | \`local:\${LocalIconsKeys}\` ${iconsType.iconsType}; + icon: AliasKeys ${iconsType.type} | Icon | \`local:\${LocalIconsKeys}\`; hover?: AliasKeys | string; className?: string; viewBox?: string; @@ -516,3 +368,159 @@ function readIconsFromDir(dir: string) { return icons; } + +function getCollectionsType(opts: { excludes: string[] }) { + const { excludes = [] } = opts; + const collections = [ + 'academicons', + 'akar-icons', + 'arcticons', + 'basil', + 'bi', + 'bpmn', + 'brandico', + 'bx', + 'bxl', + 'bxs', + 'bytesize', + 'carbon', + 'charm', + 'ci', + 'cib', + 'cif', + 'cil', + 'circle-flags', + 'circum', + 'clarity', + 'codicon', + 'cryptocurrency-color', + 'cryptocurrency', + 'dashicons', + 'ei', + 'el', + 'emblemicons', + 'emojione-monotone', + 'emojione-v1', + 'emojione', + 'entypo-social', + 'entypo', + 'eos-icons', + 'ep', + 'et', + 'eva', + 'fa-brands', + 'fa-regular', + 'fa-solid', + 'fa', + 'fa6-brands', + 'fa6-regular', + 'fa6-solid', + 'fad', + 'fe', + 'feather', + 'file-icons', + 'flag', + 'flagpack', + 'flat-color-icons', + 'flat-ui', + 'fluent-emoji-flat', + 'fluent-emoji-high-contrast', + 'fluent-emoji', + 'fluent-mdl2', + 'fluent', + 'fontelico', + 'fontisto', + 'foundation', + 'fxemoji', + 'gala', + 'game-icons', + 'geo', + 'gg', + 'gis', + 'gridicons', + 'grommet-icons', + 'healthicons', + 'heroicons-outline', + 'heroicons-solid', + 'heroicons', + 'humbleicons', + 'ic', + 'icomoon-free', + 'icon-park-outline', + 'icon-park-solid', + 'icon-park-twotone', + 'icon-park', + 'iconoir', + 'icons8', + 'il', + 'ion', + 'iwwa', + 'jam', + 'la', + 'line-md', + 'logos', + 'ls', + 'lucide', + 'majesticons', + 'maki', + 'map', + 'material-symbols', + 'mdi-light', + 'mdi', + 'medical-icon', + 'memory', + 'mi', + 'mingcute', + 'mono-icons', + 'nimbus', + 'nonicons', + 'noto-v1', + 'noto', + 'octicon', + 'oi', + 'ooui', + 'openmoji', + 'pajamas', + 'pepicons-pop', + 'pepicons-print', + 'pepicons', + 'ph', + 'pixelarticons', + 'prime', + 'ps', + 'quill', + 'radix-icons', + 'raphael', + 'ri', + 'si-glyph', + 'simple-icons', + 'simple-line-icons', + 'skill-icons', + 'subway', + 'svg-spinners', + 'system-uicons', + 'tabler', + 'teenyicons', + 'topcoat', + 'twemoji', + 'typcn', + 'uil', + 'uim', + 'uis', + 'uit', + 'uiw', + 'vaadin', + 'vs', + 'vscode-icons', + 'websymbol', + 'whh', + 'wi', + 'wpf', + 'zmdi', + 'zondicons', + ] + .filter((name) => !excludes.includes(name)) + .map((name) => `'${name}'`) + .join(' | '); + return `(${collections})`; +}