Importing icons

Iconify Tools can import icons from several sources.

Usage

All functions listed below are asynchronous. Most functions create IconSet instances.

Async

If you are not familiar with asynchronous functions in JavaScript, read up on Promise class, async and await keywords.

Simplest way to use asynchronous functions is to wrap all your code in anonymous asynchronous function, then await functions:

// Anonymous async function. Not needed if you are using Node 17 or newer.
(async () => {
   // Create new SVG instance.
   const svg = new SVG(reallyBadIcon);

   // Wait for cleanupSVG() to finish working by adding 'await' before function call.
   await cleanupSVG(svg);

   // Do other stuff
   console.log(svg.toMinifiedString());

   // Close function and immediately execute it.
})();

To catch errors, use try and catch:

// Anonymous async function. Not needed if you are using Node 17 or newer.
(async () => {
   // Create new SVG instance.
   const svg = new SVG(reallyBadIcon);

   // Wait for cleanupSVG() to finish working by adding 'await' before function call.
   try {
       await cleanupSVG(svg);
   } catch (err) {
       // Clean up failed: see error message
       console.error(err);
       return;
   }

   // Do other stuff
   console.log(svg.toMinifiedString());

   // Close function and immediately execute it.
})();

Check out various tutorials for async and await. There are many free good tutorials on YouTube.

Cleanup

After importing icons, make sure you validate them. See cleanupSVG() function.

Import functions

There are several functions for importing icons:

importDirectory()

Function importDirectory() imports SVG files from directory.

importFromFigma()

Function importFromFigma() imports icons from Figma document.

Importing single icon

Making a function for importing single file is redundant. All you have to do is read file and create SVG instance.

See importing SVG documentation.

Importing Iconify icon set

Making a function for importing IconifyJSON data is also redundant. All you have to do is read file and create IconSet instance.

See importing Iconify icon set documentation.