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

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-06-11 12:21:12 +00:00
import { components, directives } from '@primevue/metadata';
import type { ComponentResolver } from 'unplugin-vue-components/types';
export interface PrimeVueResolverOptions {
components?: {
prefix?: string;
};
directives?: {
prefix?: string;
};
}
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 === cName);
if (cMeta) {
return {
from: cMeta.from
};
}
}
},
{
type: 'directive',
resolve: (name: string) => {
const { prefix } = options.directives || {};
const dName = getName(name, prefix);
const dMeta = directives.find((d) => d.name === dName);
if (dMeta) {
return {
as: dMeta.as,
from: dMeta.from
};
}
}
}
];
}