diff --git a/playground/app.vue b/playground/app.vue index 63d5146..3a629a5 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -4,7 +4,7 @@ Basic Button - + Layout @@ -23,6 +23,9 @@ + + + diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index c23ae5c..0fa31df 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -7,6 +7,7 @@ export default defineNuxtConfig({ // iconInclude: ['Edit1'] // prefix: 'tdesign' // plugins :['DialogPlugin'] + // iconComponents:['IconFont'] }, devtools: { enabled: true } }); diff --git a/src/config/icon-components.ts b/src/config/icon-components.ts new file mode 100644 index 0000000..96eff99 --- /dev/null +++ b/src/config/icon-components.ts @@ -0,0 +1,7 @@ +export const iconComponentList = ['Icon', 'IconFont'] as const; +export type TDesignIconComponent = (typeof iconComponentList)[number]; + +export const iconComponentMap = { + Icon: 'svg-sprite', + IconFont: 'iconfont' +}; diff --git a/src/config/plugins.ts b/src/config/plugins.ts index 2ba5f4d..4472b03 100644 --- a/src/config/plugins.ts +++ b/src/config/plugins.ts @@ -1,4 +1,3 @@ export const pluginList = ['DialogPlugin', 'MessagePlugin', 'NotifyPlugin', 'LoadingPlugin', 'PopupPlugin', 'DrawerPlugin'] as const; - -export type TdesignPlugin = typeof pluginList[number]; // popupPlugin and drawerPlugin is in progress +export type TdesignPlugin = (typeof pluginList)[number]; diff --git a/src/interface.ts b/src/interface.ts index ece555b..5305996 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,4 +1,5 @@ -import { TdesignPlugin } from "./config/plugins"; +import { TDesignIconComponent } from './config/icon-components'; +import { TdesignPlugin } from './config/plugins'; // Module options TypeScript interface definition export interface ModuleOptions { @@ -15,7 +16,7 @@ export interface ModuleOptions { * exclude component name, if match do not resolve the name */ exclude?: string | RegExp | (string | RegExp)[]; - /** + /** * included component, only resolve component which match include */ include?: string | RegExp | (string | RegExp)[]; @@ -37,6 +38,12 @@ export interface ModuleOptions { * included icons, only resolve icons which match iconInclude */ iconInclude?: string | RegExp | (string | RegExp)[]; + + /** + * self-defined import icon component from tdesign-icons-vue-next + */ + iconComponents?: TDesignIconComponent[]; + /** * self-defined import plugin from tdesign-vue-next */ diff --git a/src/module.ts b/src/module.ts index 0323cee..84a3d6d 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,5 +1,5 @@ import { defineNuxtModule, createResolver } from '@nuxt/kit'; -import { resolveTDesignComponents, resolveTDesignPlugins, resolveTDesignVariables, resolveTDesignIcons } from './resolvers'; +import { resolveTDesignComponents, resolveTDesignPlugins, resolveTDesignVariables, resolveTDesignIcons, resolveTDesignIconComponents } from './resolvers'; import type { ModuleOptions } from './interface'; @@ -17,7 +17,8 @@ export default defineNuxtModule({ iconPrefix: undefined, iconExclude: undefined, iconInclude: undefined, - plugins: undefined + plugins: undefined, + iconComponents: undefined }, setup(options: ModuleOptions, nuxt) { const resolver = createResolver(import.meta.url); @@ -31,5 +32,6 @@ export default defineNuxtModule({ resolveTDesignComponents(options); resolveTDesignPlugins(options); options.resolveIcons && resolveTDesignIcons(options); + resolveTDesignIconComponents(options); } }); diff --git a/src/resolvers.ts b/src/resolvers.ts index df0c532..02b11a8 100644 --- a/src/resolvers.ts +++ b/src/resolvers.ts @@ -6,6 +6,7 @@ import { map, kebabCase } from 'lodash-es'; import { isMatch } from './utils'; import type { ModuleOptions } from './interface'; +import { iconComponentList, iconComponentMap } from './config/icon-components'; /** * auto import components @@ -62,6 +63,20 @@ export const resolveTDesignIcons = (options: ModuleOptions) => { }); }; +/** + * auto import icon from tdesign-icons-vue-next + */ +export const resolveTDesignIconComponents = (options: ModuleOptions) => { + const components = options.iconComponents ?? iconComponentList; + components.forEach((component) => { + addComponent({ + name: component, + export: component, + filePath: `tdesign-icons-vue-next/esm/${iconComponentMap[component]}/index` + }); + }); +}; + /** * auto import global CSS variables */