Files
Astro-Component/src/utils/icon.ts
T
2025-08-16 12:01:55 +05:30

20 lines
663 B
TypeScript

export const getDynamicIcon = async (name: string): Promise<string> => {
let icon = "";
let [prefix, iconName] = name.split(":");
const url = new URL(`../data/iconify/${prefix}.json`, import.meta.url);
try {
// @vite-ignore prevents pre-analysis of the dynamic path
const module: any = await import(/* @vite-ignore */ url.href);
icon = module.icons[iconName].body;
} catch {
await import(`../data/iconify/${prefix}.json`).then(module => {
icon = module.icons[iconName].body;
}).catch(error => {
console.error('Failed to load icon:', error);
});
}
return icon
}