Search code examples
npmwebpackfont-awesomefont-awesome-5

Font Awesome 5 Bundle via NPM


I'm trying to bundle only required Font Awesome 5 icons via webpack, but the icons are not replaced in the DOM.

  1. I've added all required packages from the documentation:

    yarn add -D @fortawesome/fontawesome
    yarn add -D @fortawesome/fontawesome-pro-solid
    yarn add -D @fortawesome/fontawesome-pro-regular
    yarn add -D @fortawesome/fontawesome-free-brands
    
  2. The following custom JS is included:

    "use strict";
    
    import fontawesome from '@fortawesome/fontawesome';
    import faCheck from '@fortawesome/fontawesome-pro-regular/faCheck';
    
    fontawesome.icon(faCheck);
    
    function iconsDoneRendering () {
        console.log('Icons have rendered'); // No output in console
    }
    
    fontawesome.dom.i2svg({ 
        callback: iconsDoneRendering,
    })
    
  3. The HTML template:

    <head>
        <link rel="stylesheet" href="/css/app.css?v2.1.4"> <!-- contains fa-svg-with-js.css -->
    </head>
    <body>
        <ul class="fa-ul">
            <li><span class="fa-li"><i class="far fa-phone"></i></span>List item 1</li>
            <li><span class="fa-li"><i class="far fa-check"></i></span>List item 2</li>
        </ul>
        <script src="/js/app.js?v2.1.4"></script>
    </body>
    

The svg path is inside the bundled JS file, but I can't figure out which method needs to be called.


The following JS code solves the problem (since v5.0.2):

"use strict";

import fontawesome from '@fortawesome/fontawesome';
import faCheck from '@fortawesome/fontawesome-pro-regular/faCheck';
import faPhone from '@fortawesome/fontawesome-pro-regular/faPhone';

fontawesome.library.add(faCheck,faPhone);

Solution

  • We just released version 5.0.2 and updated the @fortawesome NPM packages to fix a few bugs related to this. Make sure you upgrade before you try anything else.

    The missing step of the above example is to add the icon to the library:

    fontawesome.library.add(faCheck)