From 6b49e93823d3b47eaadab86d0a2163e9513e94e8 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Fri, 23 Apr 2021 15:59:26 +0300 Subject: [PATCH] Do not attempt to require cross-fetch in core module --- packages/core/src/api/modules/fetch.ts | 29 +++++++------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/packages/core/src/api/modules/fetch.ts b/packages/core/src/api/modules/fetch.ts index aadd97f..5e04c24 100644 --- a/packages/core/src/api/modules/fetch.ts +++ b/packages/core/src/api/modules/fetch.ts @@ -20,29 +20,16 @@ const maxLengthCache: Record = Object.create(null); const pathCache: Record = Object.create(null); /** - * Get fetch module + * Fetch function + * + * Use this to set 'cross-fetch' in node.js environment if you are retrieving icons on server side. + * Not needed when using stuff like Next.js or SvelteKit because components use API only on client side. */ -export function getFetch(): typeof fetch | undefined { - let fetchModule: typeof fetch | undefined = - typeof fetch === 'function' ? fetch : void 0; - try { - if ( - typeof document === 'undefined' && - typeof module.exports === 'object' && - typeof require === 'function' - ) { - // Attempt to load cross-fetch. - // Should be added as dependency when using Iconify with Node.js - fetchModule = require('cross-fetch'); - } - } catch (err) { - // Do nothing - } - return fetchModule; -} +let fetchModule = fetch; -// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -const fetchModule = getFetch()!; +export function setFetch(fetch: typeof fetchModule): void { + fetchModule = fetch; +} /** * Return API module