From 2b0ca8f6850745d4b3203c22058bfd2692277f98 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Tue, 7 Feb 2023 18:04:20 +0200 Subject: [PATCH] fix: combine files and iconSets options in Tailwind plugin --- plugins/tailwind/src/loader.ts | 45 +++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/plugins/tailwind/src/loader.ts b/plugins/tailwind/src/loader.ts index 7852856..2c0856e 100644 --- a/plugins/tailwind/src/loader.ts +++ b/plugins/tailwind/src/loader.ts @@ -10,12 +10,9 @@ type IconifyJSONLoaderCallback = () => IconifyJSON; * Options for icon set loaders */ export interface IconifyPluginLoaderOptions { - // Location of icon set files. Key is icon set prefix - files?: Record; - // Custom icon sets // Value can be loaded icon set or callback that loads icon set - iconSets?: Record; + iconSets?: Record; } /** @@ -25,15 +22,7 @@ interface LocatedIconSet { main: string; info?: string; } -export function locateIconSet( - prefix: string, - options: IconifyPluginLoaderOptions -): LocatedIconSet | undefined { - if (options.files?.[prefix]) { - return { - main: options.files?.[prefix], - }; - } +export function locateIconSet(prefix: string): LocatedIconSet | undefined { try { const main = require.resolve(`@iconify-json/${prefix}/icons.json`); const info = require.resolve(`@iconify-json/${prefix}/info.json`); @@ -67,19 +56,35 @@ export function loadIconSet( prefix: string, options: IconifyPluginLoaderOptions ): IconifyJSON | undefined { + let filename: LocatedIconSet; + // Check for custom icon set const customIconSet = options.iconSets?.[prefix]; if (customIconSet) { - if (typeof customIconSet === 'function') { - // Callback. Store result in options to avoid loading it again - const result = customIconSet(); - options.iconSets[prefix] = result; - return result; + switch (typeof customIconSet) { + case 'function': { + // Callback. Store result in options to avoid loading it again + const result = customIconSet(); + options.iconSets[prefix] = result; + return result; + } + + case 'string': { + // Filename to load it from + filename = { + main: customIconSet, + }; + break; + } + + default: + return customIconSet; } - return customIconSet; + } else { + // Find icon set + filename = locateIconSet(prefix); } - const filename = options.files?.[prefix] || locateIconSet(prefix, options); if (!filename) { return; }