mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 21:57:50 +00:00
chore: move isLoader
to loader.ts
and change the logic
This commit is contained in:
parent
6efa9af556
commit
3fb00b30c8
@ -1,9 +1,10 @@
|
|||||||
import { getCustomIcon } from './custom';
|
import { getCustomIcon } from './custom';
|
||||||
import { isNode } from './utils';
|
|
||||||
import { searchForIcon } from './modern';
|
import { searchForIcon } from './modern';
|
||||||
import { warnOnce } from './install-pkg';
|
import { warnOnce } from './install-pkg';
|
||||||
import type { IconifyLoaderOptions } from './types';
|
import type { IconifyLoaderOptions } from './types';
|
||||||
|
|
||||||
|
export const isNode = typeof process < 'u' && typeof process.stdout < 'u'
|
||||||
|
|
||||||
export async function loadIcon(
|
export async function loadIcon(
|
||||||
collection: string,
|
collection: string,
|
||||||
icon: string,
|
icon: string,
|
||||||
@ -18,6 +19,10 @@ export async function loadIcon(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isNode) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
let svg = await loadNodeBuiltinIcon(collection, icon, options);
|
let svg = await loadNodeBuiltinIcon(collection, icon, options);
|
||||||
|
|
||||||
if (svg && options) {
|
if (svg && options) {
|
||||||
@ -54,25 +59,22 @@ async function loadNodeBuiltinIcon(
|
|||||||
options?: IconifyLoaderOptions,
|
options?: IconifyLoaderOptions,
|
||||||
warn = true,
|
warn = true,
|
||||||
): Promise<string | undefined> {
|
): Promise<string | undefined> {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
const { loadCollectionFromFS } = await importFsModule();
|
||||||
|
const iconSet = await loadCollectionFromFS(collection, options?.autoInstall);
|
||||||
|
if (iconSet) {
|
||||||
|
// possible icon names
|
||||||
|
const ids = [
|
||||||
|
icon,
|
||||||
|
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(),
|
||||||
|
icon.replace(/([a-z])(\d+)/g, '$1-$2'),
|
||||||
|
];
|
||||||
|
return await searchForIcon(iconSet, collection, ids, options);
|
||||||
|
}
|
||||||
|
|
||||||
if (isNode) {
|
if (warn) {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
warnOnce(`failed to load \`@iconify-json/${collection}\`, have you installed it?`);
|
||||||
// @ts-ignore
|
|
||||||
const { loadCollectionFromFS } = await importFsModule();
|
|
||||||
const iconSet = await loadCollectionFromFS(collection, options?.autoInstall);
|
|
||||||
if (iconSet) {
|
|
||||||
// possible icon names
|
|
||||||
const ids = [
|
|
||||||
icon,
|
|
||||||
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(),
|
|
||||||
icon.replace(/([a-z])(\d+)/g, '$1-$2'),
|
|
||||||
];
|
|
||||||
return await searchForIcon(iconSet, collection, ids, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (warn) {
|
|
||||||
warnOnce(`failed to load \`@iconify-json/${collection}\`, have you installed it?`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user