diff --git a/packages/gitbook/scripts/generate-icon-symbols.js b/packages/gitbook/scripts/generate-icon-symbols.js index fb5127d95..ed1c7b250 100644 --- a/packages/gitbook/scripts/generate-icon-symbols.js +++ b/packages/gitbook/scripts/generate-icon-symbols.js @@ -5,26 +5,29 @@ const path = require('node:path'); const packageJSONPath = require.resolve('@gitbook/fontawesome-pro/package.json'); const packageRoot = path.dirname(packageJSONPath); +const supportedStyles = require('../src/lib/icons/supported-styles.json'); const outputDirectory = path.resolve(__dirname, '../.generated/icon-symbols'); +const loadersModulePath = path.resolve(__dirname, '../.generated/icon-symbol-loaders.ts'); const metadataPath = path.join(packageRoot, 'icons', 'metadata', 'icons.json'); -const supportedStyles = [ - 'brands', - 'custom-icons', - 'duotone', - 'light', - 'regular', - 'sharp-duotone-solid', - 'sharp-light', - 'sharp-regular', - 'sharp-solid', - 'sharp-thin', - 'solid', - 'thin', -]; - const symbolPattern = /([\s\S]*?)<\/symbol>/g; +function generateLoadersModule(styles) { + const entries = styles + .map((style) => { + return ` ${JSON.stringify(style)}: () => + import('./icon-symbols/${style}.json', { with: { type: 'json' } }),`; + }) + .join('\n'); + + return `import type { StyleIconSymbolManifest } from '../src/lib/icons/types'; + +export const loaders = { +${entries} +} satisfies Record Promise<{ default: StyleIconSymbolManifest }>>; +`; +} + async function main() { await fs.rm(outputDirectory, { recursive: true, force: true }); await fs.mkdir(outputDirectory, { recursive: true }); @@ -63,6 +66,8 @@ async function main() { }) ); + await fs.writeFile(loadersModulePath, generateLoadersModule(supportedStyles), 'utf8'); + // biome-ignore lint/suspicious/noConsole: CLI output is useful when regenerating manifests. console.log(`Generated ${supportedStyles.length} icon symbol manifests in ${outputDirectory}`); } diff --git a/packages/gitbook/src/lib/icons/supported-styles.json b/packages/gitbook/src/lib/icons/supported-styles.json new file mode 100644 index 000000000..38879ea4f --- /dev/null +++ b/packages/gitbook/src/lib/icons/supported-styles.json @@ -0,0 +1,14 @@ +[ + "brands", + "custom-icons", + "duotone", + "light", + "regular", + "sharp-duotone-solid", + "sharp-light", + "sharp-regular", + "sharp-solid", + "sharp-thin", + "solid", + "thin" +] diff --git a/packages/gitbook/src/lib/icons/symbols.ts b/packages/gitbook/src/lib/icons/symbols.ts index 92c5aaae3..d1bb1e04d 100644 --- a/packages/gitbook/src/lib/icons/symbols.ts +++ b/packages/gitbook/src/lib/icons/symbols.ts @@ -1,37 +1,8 @@ import 'server-only'; +import { loaders } from '../../../.generated/icon-symbol-loaders'; import type { StyleIconSymbolManifest } from './types'; -const loaders = { - brands: () => - import('../../../.generated/icon-symbols/brands.json', { with: { type: 'json' } }), - 'custom-icons': () => - import('../../../.generated/icon-symbols/custom-icons.json', { - with: { type: 'json' }, - }), - duotone: () => - import('../../../.generated/icon-symbols/duotone.json', { with: { type: 'json' } }), - light: () => import('../../../.generated/icon-symbols/light.json', { with: { type: 'json' } }), - regular: () => - import('../../../.generated/icon-symbols/regular.json', { with: { type: 'json' } }), - 'sharp-duotone-solid': () => - import('../../../.generated/icon-symbols/sharp-duotone-solid.json', { - with: { type: 'json' }, - }), - 'sharp-light': () => - import('../../../.generated/icon-symbols/sharp-light.json', { with: { type: 'json' } }), - 'sharp-regular': () => - import('../../../.generated/icon-symbols/sharp-regular.json', { - with: { type: 'json' }, - }), - 'sharp-solid': () => - import('../../../.generated/icon-symbols/sharp-solid.json', { with: { type: 'json' } }), - 'sharp-thin': () => - import('../../../.generated/icon-symbols/sharp-thin.json', { with: { type: 'json' } }), - solid: () => import('../../../.generated/icon-symbols/solid.json', { with: { type: 'json' } }), - thin: () => import('../../../.generated/icon-symbols/thin.json', { with: { type: 'json' } }), -} satisfies Record Promise<{ default: StyleIconSymbolManifest }>>; - type SupportedSymbolStyle = keyof typeof loaders; const manifestPromises = new Map>(); @@ -56,7 +27,7 @@ export async function getIconStyleManifest(style: string): Promise = load().then( - (module) => module.default + (module) => module.default as StyleIconSymbolManifest ); manifestPromises.set(style, manifestPromise);