Importing SVG from directory

This function is part of import functions in Iconify Tools.

Function importDirectory() finds and imports all SVG files from a directory.

It creates IconSet instance, which can be exported to various formats.


Function has the following parameters:

  • dir, string. Directory to import from.
  • options, object. Options (optional).

Function returns IconSet instance.

Function is asynchronous. That means you need to handle it as Promise instance, usually by adding await before function call.


After importing icons, they need to be:

See example below.


import {
} from '@iconify/tools';

(async () => {
   // Import icons
   const iconSet = await importDirectory('files/svg', {
       prefix: 'test',

   // Validate, clean up, fix palette and optimise
   await iconSet.forEach(async (name, type) => {
       if (type !== 'icon') {

       const svg = iconSet.toSVG(name);
       if (!svg) {
           // Invalid icon

       // Clean up and optimise icons
       try {
           // Clean up icon code
           await cleanupSVG(svg);

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

           // Optimise
           await runSVGO(svg);
       } catch (err) {
           // Invalid icon
           console.error(`Error parsing ${name}:`, err);

       // Update icon
       iconSet.fromSVG(name, svg);

   // Export