feat(auto-import): 添加 naive-ui 组件自动导入支持
All checks were successful
CI/CD Pipeline / playwright (push) Successful in 4m8s
CI/CD Pipeline / build-and-deploy (push) Successful in 2m35s

This commit is contained in:
严浩
2025-10-25 00:47:07 +08:00
parent 699b68990e
commit d0f1ad0702
4 changed files with 351 additions and 11 deletions

View File

@@ -22,7 +22,38 @@ import IconsResolver from 'unplugin-icons/resolver';
import { VantResolver } from '@vant/auto-import-resolver';
// <<<<<
export async function loadPlugin(_configEnv: ConfigEnv): Promise<PluginOption> {
import fs from 'node:fs';
import path from 'node:path';
function _getNaiveUiComponentNames() {
// 方法1: 从 web-types.json 读取(推荐)
const webTypesPath = path.resolve('node_modules/naive-ui/web-types.json');
if (fs.existsSync(webTypesPath)) {
const webTypes = JSON.parse(fs.readFileSync(webTypesPath, 'utf-8'));
const components = webTypes.contributions.html['vue-components'];
const componentNames = components.map((component: { name: string }) => component.name);
console.log('naive-ui components count (from web-types.json):', componentNames.length);
return componentNames;
}
// 方法2: 从 volar.d.ts 读取(备选)
const volarPath = path.resolve('node_modules/naive-ui/volar.d.ts');
if (fs.existsSync(volarPath)) {
const volarContent = fs.readFileSync(volarPath, 'utf-8');
// 匹配类似 "NAffix: (typeof import('naive-ui'))['NAffix']" 的行
const regex = /^\s+(N\w+):/gm;
const matches = [...volarContent.matchAll(regex)];
const componentNames = matches.map((match) => match[1]);
console.log('naive-ui components count (from volar.d.ts):', componentNames.length);
return componentNames;
}
console.warn('Could not find naive-ui component metadata files');
return [];
}
export function loadPlugin(_configEnv: ConfigEnv): PluginOption {
return [
// https://github.com/antfu/unplugin-auto-import
AutoImport({
@@ -43,7 +74,14 @@ export async function loadPlugin(_configEnv: ConfigEnv): Promise<PluginOption> {
{
'consola/browser': ['consola'],
'vue-router/auto': ['useLink'],
'naive-ui': ['useModal', 'useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
'naive-ui': [
'useModal',
'useDialog',
'useMessage',
'useNotification',
'useLoadingBar',
..._getNaiveUiComponentNames(),
],
},
],
vueTemplate: true,
@@ -53,7 +91,7 @@ export async function loadPlugin(_configEnv: ConfigEnv): Promise<PluginOption> {
// `__`开头的
excludeNames: [/^__/],
// allow auto load markdown components under `./src/components/`
extensions: ['vue', 'md'],
extensions: ['vue', 'md', 'tsx'],
// allow auto import and register components used in markdown
include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
resolvers: [