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: auto import icon components #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<t-divider>Basic</t-divider>
<t-space>
<t-button>Button</t-button>
<t-icon name="cry-and-laugh"></t-icon>
<t-icon name="cry-and-laugh" />
</t-space>

<t-divider>Layout</t-divider>
Expand All @@ -23,6 +23,9 @@
<t-space>
<edit-icon />
<edit-1-icon />
<icon-font name="sneer" />
<icon-font name="unhappy" />
<icon name="close" />
</t-space>
</div>
</template>
Expand Down
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineNuxtConfig({
// iconInclude: ['Edit1']
// prefix: 'tdesign'
// plugins :['DialogPlugin']
// iconComponents:['IconFont']
},
devtools: { enabled: true }
});
7 changes: 7 additions & 0 deletions src/config/icon-components.ts
Original file line number Diff line number Diff line change
@@ -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'
};
3 changes: 1 addition & 2 deletions src/config/plugins.ts
Original file line number Diff line number Diff line change
@@ -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];
11 changes: 9 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)[];
Expand All @@ -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[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉这里不用新增吧 有点容易误解 iconInclude和iconExclude 就可以满足了 resolveIcons 应该就是要不要resolve icon这个包的

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我理解是原来三个参数只处理图标导入,不处理其他

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolveIcons 本应就包括这两个组件了 是实现缺了 可以再改下描述 而且通过原来的API完全可以满足不导入这两个或者需要这两个的需求 再新增API又是继续增加维护成本

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意UY的观点,确实API太多太容易混乱了,除非有什么必须区分的理由


/**
* self-defined import plugin from tdesign-vue-next
*/
Expand Down
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -17,7 +17,8 @@ export default defineNuxtModule<ModuleOptions>({
iconPrefix: undefined,
iconExclude: undefined,
iconInclude: undefined,
plugins: undefined
plugins: undefined,
iconComponents: undefined
},
setup(options: ModuleOptions, nuxt) {
const resolver = createResolver(import.meta.url);
Expand All @@ -31,5 +32,6 @@ export default defineNuxtModule<ModuleOptions>({
resolveTDesignComponents(options);
resolveTDesignPlugins(options);
options.resolveIcons && resolveTDesignIcons(options);
resolveTDesignIconComponents(options);
}
});
15 changes: 15 additions & 0 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down