2

I have been struggling with this for way too long and have no clue where I am going wrong.

I am trying to use fontawesome 5 in my webpack project. I have been able to use icons in HTML but if I try a pseudo selector it just returns the empty square. Read loads of articles and docs but nothing seems to work. Please help thanks.

npm install @fortawesome/fontawesome-free

index.js

    import 'bootstrap'
    import '@fortawesome/fontawesome-free/js/all.js'
    import '../scss/main.scss'

    window.FontAwesomeConfig = {
      searchPseudoElements: true
    }

main.scss

    /// Bootstrap ///
    // -- Required
    @import "~bootstrap/scss/functions";
    @import "~bootstrap/scss/variables";
    @import "~bootstrap/scss/mixins";

    // -- Optional
    @import "~bootstrap/scss/buttons";
    @import "~bootstrap/scss/code";
    @import "~bootstrap/scss/forms";
    @import "~bootstrap/scss/grid";
    @import "~bootstrap/scss/images";
    @import "~bootstrap/scss/modal";
    @import "~bootstrap/scss/reboot";
    @import "~bootstrap/scss/type";
    @import "~bootstrap/scss/transitions";
    @import "~bootstrap/scss/mixins/breakpoints";

    /// Modules ///
    @import 'modules/colors';
    @import 'modules/sizes';
    @import 'modules/fonts';
    @import 'modules/transitions';    

    /// Partial Files ///
    @import 'partials/jumbotron';

    /// Vendor Files ///
    @import 'vendor/reset';

SCSS File _jumbotron.scss

    .products {
        background: $colour-red;

        .products-item {
          .products-item-header {
            .products-item-title {
              a {
                padding: $size-base;
                display: block;
                color: $colour-white;
                text-align: center;

                &:before {
                  font-family: "Font Awesome 5 Brands", "Font Awesome 5 Free";
                  display: block;
                  content: "\f095";
                  font-weight: 900;
                  font-size: 20px;
                  color: $colour-white;
            }
          }
        }
      }

HTML

    <div class="products">
        <div class="row">
            <div class="col-sm-3 products-item">
                <div id="post-37">
                  <div class="products-item-header entry-header">
                    <h3 class="products-item-title entry-title">
                       <a href="http://localhost/wplocal/products/agribusiness">
                          Agribusiness
                        </a>
                    </h3>
                 </div>
              </div>
           </div>
        </div>
     </div>

enter image description here

Webpack Config

    const path = require('path')
    const webpack = require('webpack')
    const HtmlWebPackPlugin = require('html-webpack-plugin')
    const MiniCssExtractPlugin = require('mini-css-extract-plugin')
    const CopyWebpackPlugin = require('copy-webpack-plugin')
    // const devMode = process.env.NODE_ENV !== 'production'

    module.exports = {
        entry: { main: './src/js/index.js' },
        output: {
            path: path.resolve(__dirname, 'dist'),
            filename: 'js/[name].js'
        },
        module: {
            rules: [
                {
                  use: {
                    loader:'babel-loader',
                    options: { presets: ['es2015'] }
                  },
                  test: /\.js$/,
                  exclude: /node_modules/
                },
                {
                    test: /\.(sa|sc|c)ss$/,
                    use: [
                        'style-loader',
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'sass-loader'
                    ]
                },
                {
                  test: /\.(gif|png|jp(e*)g|svg)$/,
                  use: [{
                    loader: 'url-loader',
                    options: {
                      limit: 1000000, //Convert images < 1mb to base 64 strings
                      name: 'images/[name].[ext]'
                    }
                  }] 
                },
                {
                  test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                  use: [{
                    loader: 'file-loader',
                    options: {
                      name: 'fonts/[name].[ext]',
                      publicPath: '../'
                    }
                  }]
                }
            ]
        },
        devServer: {
            contentBase: "./dist"
        },
        plugins: [
            new HtmlWebPackPlugin({
                inject: false,
                hash: true,
                template: './src/index.html',
                filename: 'index.html'
            }),
            new MiniCssExtractPlugin({
                filename: 'css/style.css',
                chunkFilename: 'css/style.css'
            }),
            new CopyWebpackPlugin ([
              {
                from: './src/images',
                to: 'images'
              }
            ]),
            new webpack.ProvidePlugin({
              $: 'jquery',
              jQuery: 'jquery',
              'window.jQuery': 'jquery',
              Popper: ['popper.js', 'default']
            })
        ]
    }
Syntle
  • 5,168
  • 3
  • 13
  • 34
bilcker
  • 1,120
  • 1
  • 15
  • 43
  • Possible duplicate of [FontAwesome - Choosing the correct font-family in CSS pseudo-elements](https://stackoverflow.com/questions/50680160/fontawesome-choosing-the-correct-font-family-in-css-pseudo-elements) – Temani Afif Aug 24 '18 at 01:01
  • possible duplicate of : https://stackoverflow.com/questions/49754892/font-awesome-shows-square-instead-of-icon-when-used-directly-in-css/49755090#49755090 – Temani Afif Aug 24 '18 at 01:01
  • I figured so too but I tried everything in the above threads to no success – bilcker Aug 24 '18 at 01:35
  • If your using the js version do you still need to include css file? – bilcker Aug 24 '18 at 01:36
  • no need the CSS with JS, can you share a full working code so we can debug? – Temani Afif Aug 24 '18 at 07:54

3 Answers3

3

Try

  font-family: 'Font Awesome\ 5 Free';
Priya
  • 39
  • 6
0

Set searchPseudoElements to true in the fontawesome-svg-core config object. And verify if the font-family and content properties have the correct values.

Taken from the fontawesome docs:

https://fontawesome.com/how-to-use/javascript-api/setup/configuration

By enabling searchPseudoElements to true (it’s false by default) you can have Font Awesome search your DOM for any elements that have:

A Font Awesome matching font-family,

A content property

Valid font-family values are: Font Awesome 5 Solid, Font Awesome 5 Regular, Font Awesome > 5 Light, and Font Awesome 5 Brands.

Example


import {library, dom, config} from "@fortawesome/fontawesome-svg-core";
import {faCheck} from "@fortawesome/free-solid-svg-icons/faCheck";

config.searchPseudoElements = true; //<----- config

export default () => {

    library.add(faCheck);
    dom.watch();

}

Example:

<head>
  <script src="https://use.fontawesome.com/releases/v5.13.1/js/all.js" data-search-pseudo-elements></script>
  <style>
  .glasses::before {
    display: none;
    font-family: 'Font Awesome 5 Solid';
    content: "\f530";
  }
  </style>
</head>
<body>
  <span class="glasses"></span>
</body>
nooberson
  • 71
  • 1
  • 4
-1
<style>
.element:before {
content: "\f000";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #000;
font-size: 18px;
padding-right: 0.5em;
position: absolute;
top: 10px;
left: 0;
}

  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – 31piy Aug 24 '18 at 03:25