Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use Font Awesome SCSS in a Yeoman's Angular project

I created an Angular project using Yeoman's generator, and now I am trying to add the Font Awesome to the project. I installed it using Bower using

bower install fontawesome --save

then it automatically added to my app/index.html the following code:

<!-- build:css(.) styles/vendor.css -->
<!-- bower:css -->
<link rel="stylesheet" href="bower_components/fontawesome/css/font-awesome.css" />
<!-- endbower -->
<!-- endbuild -->

But I didn't want to use it as CSS, but as a SCSS import (to be able to change the URL of font files). So I deleted the above code from the HTML page and added the proper code into app/styles/main.scss:

$icon-font-path: "../bower_components/bootstrap-sass-official/assets/fonts/bootstrap/";
$fa-font-path: "../bower_components/fontawesome/fonts/"; // <==== here

// bower:scss
@import "bootstrap-sass-official/assets/stylesheets/_bootstrap.scss";
@import "fontawesome/scss/font-awesome.scss"; // <==== and here
// endbower

Then I run grunt build, but something (Grunt?) edited my files back to the original ones. The index.html got the <link> again and mine main.scss was kept only with the Bootstrap import.

Ok, we are almost there.

So I looked at the Bootstrap's bower.json and compared to the Font Awesome's bower.json and I saw the following difference:

// Bootstrap
"main": [
  "assets/stylesheets/_bootstrap.scss", // it's SCSS
  ...
],

// Font Awesome
"main": [
  "./css/font-awesome.css", // it's CSS
  ...
],

Then I found a way to properly (not sure) override the Font Awesome's bower configuration using my app's bower.json, and added the following code into it

"overrides": {
  "fontawesome": {
    "main": [
      "./scss/font-awesome.scss", // now it's SCSS
      "./fonts/*"
    ]
  }
}

Question: Is this the correct way to use Font Awesome as a SCSS import and avoid Grunt of changing my files when building the project? By overriding its default "main" property?

like image 875
Rodrigo Saling Avatar asked Sep 29 '22 13:09

Rodrigo Saling


1 Answers

Judging by your question I think you may be new to the web frontend build process. Unfortunately it can be a headache at times.

First off, note that SCSS != CSS and browsers do not know how to use SCSS. Including <link rel="stylesheet" href="main.scss"> in your HTML <head> tag will not work. We can enjoy the various perks of SCSS (e.g. variables, imports, etc.) during development, but to use these styles in the browser we must transpile SCSS into CSS (which the browser recognises).

To make developer's lives easier, it is common for seed projects (such as the one you are using) to include some kind of automated build process which includes transpiling SCSS into CSS. The build process is typically executed via a task runner, such as Grunt or Gulp. These seed projects/templates also typically include a task to inject your project's dependencies (as declared in your bower.json file) into your index.html automatically. A tool called wiredep is used to read through your bower.json files to determine these dependencies, in particularly, it will look for the 'main' attribute to determine which files a particular package requires. It will then inject the appropriate <script/> and <link> tags.

To explain this further, when you execute bower -install font-aweseome --save a few things happen:

  1. The package is downloaded and added to your project (usually to bower_components/).
  2. An entry is added to your bower.json file adding the package to your project's dependencies.

Each package includes its own bower.json file which describes its various properties, the most important of which (after name) is main. You can see the font-awesome bower.json file here. As you can see, it's main attribute references several files. These are the files that wiredep uses to determine which <script/> and <link> tags to inject into your index.html.

By specifying an override in your project's bower.json like this:

"overrides": {
  "fontawesome": {
    "main": [
      "./scss/font-awesome.scss", // now it's SCSS
      "./fonts/*"
    ]
  }
}

You are specifying to wiredep that ./scss/font-awesome.scss and ./fonts/* are the files required by your project, and to ignore those listed in the font-awesome bower.json file.

Regarding SCSS: take a look at the Sass guide, in particular the section on imports. The Sass @import lets you split your styles across several smaller SCSS files (partials) that, upon transpilation, are combined and included into a single (optionally minified) CSS file. This includes any third party SCSS files that you import into your main.scss. For example, if you imported font-awesome.scss into your main.scss file, then transpiled it into CSS, the resulting CSS file would include all styles contained within font-awesome.scss.

To include and customize a third party SCSS file, what I do is:

  1. Specify an import in my own custom main.scss file.
  2. Make any customizations to variables I want.
  3. Transpile main.scss into CSS.
  4. Include a <link> tag to the transpiled CSS file in my index.html file.
like image 95
Blake Mumford Avatar answered Oct 05 '22 07:10

Blake Mumford