primevue-mirror/packages/auto-import-resolver/index.ts

61 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-06-20 15:20:59 +00:00
import { components, directives, MetaType } from '@primevue/metadata';
import type { ComponentResolver, ComponentResolveResult } from 'unplugin-vue-components/types';
2024-06-11 12:21:12 +00:00
export interface PrimeVueResolverOptions {
components?: {
prefix?: string;
};
directives?: {
prefix?: string;
};
resolve?: (meta: MetaType, type: string) => ComponentResolveResult;
2024-06-11 12:21:12 +00:00
}
export function PrimeVueResolver(options: PrimeVueResolverOptions = {}): ComponentResolver[] {
const getName = (name: string, prefix?: string) => {
if (prefix) {
if (!name.startsWith(prefix)) return;
name = name.substring(prefix.length);
}
return name;
};
return [
{
type: 'component',
resolve: (name: string) => {
const { prefix } = options.components || {};
const cName = getName(name, prefix);
const cMeta = components.find((c) => c.name.toLocaleLowerCase() === cName?.toLocaleLowerCase());
2024-06-11 12:21:12 +00:00
if (cMeta) {
return (
options?.resolve?.(cMeta, 'component') ?? {
from: cMeta.from
}
);
2024-06-11 12:21:12 +00:00
}
}
},
{
type: 'directive',
resolve: (name: string) => {
const { prefix } = options.directives || {};
const dName = getName(name, prefix);
const dMeta = directives.find((d) => d.name.toLocaleLowerCase() === dName?.toLocaleLowerCase());
2024-06-11 12:21:12 +00:00
if (dMeta) {
return (
options?.resolve?.(dMeta, 'directive') ?? {
as: dMeta.as,
from: dMeta.from
}
);
2024-06-11 12:21:12 +00:00
}
}
}
];
}