Font Awesome provides thousands of icons. The “Library” is the way you can subset or reduce file sizes and reference icons easily.
Font Awesome SVG Core and icon content packages
The @fortawesome/fontawesome-svg-core package provides the functionality but not any of the icon content.
We’ll be using the library object which is part of the API. Install one of the icon content packages and let’s get going.
Adding icons to the library
The { library } import has an add() method which takes any number of arguments and registers icons for later use.
If we install another icon content package, this time the Free Brands:
We can add this to the library at the same time as the Free Solid package.
Using the library
With icons in the library we can use the findIconDefinition() method to locate icon objects using their style prefix and their icon name.
What’s going on here? The findIconDefinition() method is using the library to locate an icon that matches the prefix and iconName.
The glasses object contains all the information about the icon–size, prefix, name, and the SVG path data. This is known as the icon definition.
One of the most useful API methods to use icon definitions with is icon(). It can transform an icon definition into an SVG representation suitable for use in the DOM or elsewhere.
Subsetting
While it’s easy to add entire styles to the library this results in some big
files. A typical site, even one that uses a lot of icons, will only need a
small portion of the thousands of icons Font Awesome provides.
That’s where subsetting comes in.
Let’s take our previous example and only import the icons that we need.
You can reap the benefits of this explicit import through bundling tools that
eliminate “dead code”. Dead code is anything in your final bundle that will
never be used by your project (making it safe to be removed). This process is
known as tree shaking. Rollup and Webpack 2+ are a couple of tools that support tree shaking.