Skip to content

导入 SVG

本教程是 Iconify Tools导入函数文档 的一部分。

由于导入单个图标属于多余操作,因此没有提供专门的函数。您只需读取内容并创建一个新的 SVG 实例即可:

示例

example.ts
tsimport { promises as fs } from 'fs';
import {
   SVG,
   blankIconSet,
   cleanupSVG,
   runSVGO,
   parseColors,
   isEmptyColor,
} from '@iconify/tools';

(async () => {
   // Create an empty icon set
   const iconSet = blankIconSet('test');

   // Read icon, create SVG instance
   const content = await fs.readFile('files/home.svg', 'utf8');
   const svg = new SVG(content);

   // Clean up icon code
   cleanupSVG(svg);

   // Assume icon is monotone: replace color with currentColor, add if missing
   // If icons are not monotone, remove this code
   parseColors(svg, {
       defaultColor: 'currentColor',
       callback: (attr, colorStr, color) => {
           return !color || isEmptyColor(color) ? colorStr : 'currentColor';
       },
   });

   // Optimise
   runSVGO(svg);

   // Add icon to icon set
   iconSet.fromSVG('home', svg);
})();