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;
|
|
|
|
};
|
2024-06-20 12:49:15 +00:00
|
|
|
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);
|
2024-06-30 20:33:35 +00:00
|
|
|
const cMeta = components.find((c) => c.name.toLocaleLowerCase() === cName?.toLocaleLowerCase());
|
2024-06-11 12:21:12 +00:00
|
|
|
|
|
|
|
if (cMeta) {
|
2024-06-20 12:49:15 +00:00
|
|
|
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);
|
2024-06-30 20:33:35 +00:00
|
|
|
const dMeta = directives.find((d) => d.name.toLocaleLowerCase() === dName?.toLocaleLowerCase());
|
2024-06-11 12:21:12 +00:00
|
|
|
|
|
|
|
if (dMeta) {
|
2024-06-20 12:49:15 +00:00
|
|
|
return (
|
|
|
|
options?.resolve(dMeta, 'directive') ?? {
|
|
|
|
as: dMeta.as,
|
|
|
|
from: dMeta.from
|
|
|
|
}
|
|
|
|
);
|
2024-06-11 12:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|