From 3fb00b30c8f6ab83ee8aced19ab55c160967ad19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez=20Jim=C3=A9nez?= Date: Sat, 26 Feb 2022 17:49:13 +0100 Subject: [PATCH] chore: move `isLoader` to `loader.ts` and change the logic --- packages/utils/src/loader/loader.ts | 40 +++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/utils/src/loader/loader.ts b/packages/utils/src/loader/loader.ts index 222ad0c..526bfb4 100644 --- a/packages/utils/src/loader/loader.ts +++ b/packages/utils/src/loader/loader.ts @@ -1,9 +1,10 @@ import { getCustomIcon } from './custom'; -import { isNode } from './utils'; import { searchForIcon } from './modern'; import { warnOnce } from './install-pkg'; import type { IconifyLoaderOptions } from './types'; +export const isNode = typeof process < 'u' && typeof process.stdout < 'u' + export async function loadIcon( collection: string, icon: string, @@ -18,6 +19,10 @@ export async function loadIcon( } } + if (!isNode) { + return undefined; + } + let svg = await loadNodeBuiltinIcon(collection, icon, options); if (svg && options) { @@ -54,25 +59,22 @@ async function loadNodeBuiltinIcon( options?: IconifyLoaderOptions, warn = true, ): Promise { + // 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) { - // 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 (warn) { - warnOnce(`failed to load \`@iconify-json/${collection}\`, have you installed it?`); - } + if (warn) { + warnOnce(`failed to load \`@iconify-json/${collection}\`, have you installed it?`); } }