0

It seems tedious (and like a footgun) to have to re-type all my imported icons into the library.add function. Is there a way to get around this?

// font-awesome-library.js

import { library } from '@fortawesome/fontawesome-svg-core'
import {
  faChartBar, faFileAlt, faBalanceScale, faUser, faFutbol, faBuilding,
  faAddressBook, faChartLine, faDatabase, faServer, faLink, faCloudUpload,
  faClipboard, faSlidersH, faChartBar, faUsers
} from '@fortawesome/pro-solid-svg-icons'
import { faFortAwesome} from '@fortawesome/free-brands-svg-icons';

// Re-listing these below seem tedious..
library.add(
  faChartBar, faFileAlt, faBalanceScale, faUser, faFutbol, faBuilding,
  faAddressBook, faChartLine, faDatabase, faServer, faLink, faCloudUpload,
  faClipboard, faSlidersH, faChartBar, faUsers, faFortAwesome, 
);

Also, if I typo the import (or use the wrong name, eg faBarChart instead of faChartBar) I get an unhelpful error in react-fontawesome: TypeError: Cannot read property 'prefix' of undefined :(

craigmichaelmartin
  • 6,091
  • 1
  • 21
  • 25

1 Answers1

0

I refactored font-awesome-library.js to:

// font-awesome-library.js

import { library } from '@fortawesome/fontawesome-svg-core'
import * as icons from './font-awesome-icons';

Object.keys(icons).forEach(key => {
  try {
    library.add(icons[key]);
  } catch (err) {
    console.log(key);
    debugger; // eslint-disable-line
    throw err;
  }
});

And only have to enumerate the icons once in font-awesome-icons.js:

// font-awesome-icons.js

export {
  faChartBar, faFileAlt, faBalanceScale, faUser, faFutbol, faBuilding,
  faAddressBook, faChartLine, faDatabase, faServer, faLink, faCloudUpload,
  faClipboard, faSlidersH, faChartPie, faUsers
} from '@fortawesome/pro-solid-svg-icons'
export { faFortAwesome} from '@fortawesome/free-brands-svg-icons';

Also, by importing them as an object ({faChartBar, faFileAlt, /*...*/}) I am able to know exactly which icon (of the ones I tried to import) was wrongly named, solving my other problem.

craigmichaelmartin
  • 6,091
  • 1
  • 21
  • 25