hq1

hq1

How to include FontAwesome in Phoenix app?

Hi,

I am completely clueless when it comes to the whole static assets building pipeline. That’s probably the most overwhelming programming experience in my career thus far :joy:

Seriously though, can anyone please share a snippet of things I need to add to have FontAwesome icons available in a phoenix app?

I’m using Phoenix 1.4.0 so I guess that’s webpack then, plus I do npm install in assets directory to get the priv contents built. Everything is pretty much at mix phx.new defaults. Pls send halp.

First 10 of 16 Posts Switch mode

hauleth

hauleth

You enter assets directory and then follow NPM instructions. That is it. Phoenix assets pipeline is exactly the same way as any other webpack project with specific build directory (../priv/static) and that is all.

adrianrl

adrianrl

Hi, when it comes to icons, fonts or heavy dependencies it’s a good idea to serve those assets using their respective CDNs, so I’d rather add a simple link tag to include Font Awesome. You should only serve by yourself those dependencies that you want to customize.

hq1

hq1

You enter assets directory and then follow NPM instructions . That is it.

I wish. Nothing related to font awesome ends up in priv/static after doing just that.

“Same way as any other webpack project” means nothing to me unfortunately.

hq1

hq1

That makes sense, I’ll see if I can just put those pre-built assets on S3 due to privacy concerns.

hauleth

hauleth

Have you restarted your application? It need to be rebuilt after the fact.

MrDoops

MrDoops

You might need something like File Loader to manage the kinds of assets FontAwesome provides. This guide seems to go over using Font Awesome and File Loader.

kokolegorille

kokolegorille

I have this loader for fonts in my webpack.config.js, in the modules/rules section

      // Load fonts
      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?(\?.*$|$)/,
        use: 'url-loader?&limit=10000&name=/fonts/[name].[ext]',
      },
      {
        test: /\.(eot|ttf|otf)?(\?.*$|$)/,
        loader: 'file-loader?&limit=10000&name=/fonts/[name].[ext]',
      },

Then in app.js, I load app.scss, and in app.scss, I load fontawesome, something like this…

@import '../../node_modules/font-awesome/css/font-awesome.css';

But for this to work, I added following dev dependencies,

node-sass, sass-loader, file-loader, url-loader.

You might show your webpack.config.js, and your package.json.

eahanson

eahanson

Another option is to download individual font awesome icons as SVGs into assets/static/images and them emit them in your templates or views with Phoenix Inline SVG.

If you only want a few icons, this can reduce the size of your responses and doesn’t involve any webpack stuff. Caveats: some older browsers have issues with SVG, and every time you want a new icon, you have to download it and add it to your assets directory.

hq1

hq1

Thank you, that worked!

belgoros

belgoros

Navigate to your phoenix app assets directory:

cd your_app/assets

Install free edition of fontawesome as explained here:

npm install --save-dev @fortawesome/fontawesome-free

You will see package.json updated with a new entry:

..
"devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@fortawesome/fontawesome-free": "^5.9.0",
...

You will have to install file-loader package as well: npm install file-loader.

Then modify webpack.config.js to be able to copy awesome fonts and extract SCSS files:

module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.(css|sass|scss)$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        use: [{
          loader: 'file-loader',
          options: {
            name: '[name].[ext]',
            outputPath: '../fonts'
          }
        }]
      }
    ]
  },
...

Next, modify your app.scss file as follows (I also use Bootrstrap, so remove the related import if you don’t use it):

@import "custom";
@import "~bootstrap/scss/bootstrap";
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
@import "~@fortawesome/fontawesome-free/scss/regular";
@import "~@fortawesome/fontawesome-free/scss/solid";
@import "~@fortawesome/fontawesome-free/scss/brands";

And finally, my _custom.scss file (remove $primary containing line, it is for Bootrstap override):

/* Boostrap overrides */

$primary: #F92653;

/* Fontawesome 5 config */

$fa-font-path: "~@fortawesome/fontawesome-free/webfonts"

Hope, this helps.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement