Skip to content

将所有 Iconify 图标导出为 SVG

本示例展示了如何为 @iconify/json 包中的所有图标生成 SVG 文件。

首先,创建一个空白的 Node 项目,并将 @iconify/tools 添加为依赖项。

然后创建文件 export-files.mjs 并填入以下内容:

export-files.mjs
mjsimport { readFile } from 'node:fs/promises';
import { downloadNPMPackage, IconSet, exportToDirectory } from '@iconify/tools';

// Directories
const cacheDir = 'cache';
const outDir = 'svg';

// Download all icon sets
console.log('Downloading latest package');
const downloaded = await downloadNPMPackage({
   package: '@iconify/json',
   target: cacheDir,
});
console.log('Downloaded version', downloaded.version);

// Get a list of icon sets
const list = JSON.parse(
   await readFile(downloaded.contentsDir + '/collections.json', 'utf8')
);
const prefixes = Object.keys(list);
console.log('Got', prefixes.length, 'icon sets');

// Export each icon set
for (let i = 0; i < prefixes.length; i++) {
   const prefix = prefixes[i];

   // Read file
   const data = JSON.parse(
       await readFile(
           downloaded.contentsDir + '/json/' + prefix + '.json',
           'utf8'
       )
   );

   // Create IconSet
   const iconSet = new IconSet(data);

   // Export it
   console.log('Exporting', iconSet.info.name);
   await exportToDirectory(iconSet, {
       target: outDir + '/' + prefix,
   });
}

console.log('Done');

然后运行该文件:

node export-files.mjs