Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add Font Awesome to my Aurelia project using npm?

I have been following the Contact Manager tutorial and would like to add Font Awesome to the project. Here's what I have done so far:

  • npm install Font-Awesome --save
  • Added the following to aurelia.jsonunder the dependencies array of the vendor-bundle.js:


...
{
    "name": "font-awesome",
    "path": "../node_modules/font-awesome",
    "resources": [
      "css/font-awesome.min.css"
    ]
},
...

But when running au run --watch I get the error:

error C:\Users\node_modules\font-awesome.js

Why is it looking for the .js file?

like image 219
MaYaN Avatar asked Sep 01 '16 12:09

MaYaN


4 Answers

Don't add font-awesome resources to aurelia.json - you'd need font files too, which Aurelia don't process. Instead, take the following steps.

First, if you added anything for font-awesome already to your aurelia.json file, remove it again.

Add new file prepare-font-awesome.js in folder \aurelia_project\tasks and insert the below code. It copies font-awesome resource files to output folder (as configured aurelia.json config file; e.g. scripts):

import gulp from 'gulp';
import merge from 'merge-stream';
import changedInPlace from 'gulp-changed-in-place';
import project from '../aurelia.json';

export default function prepareFontAwesome() {
  const source = 'node_modules/font-awesome';

  const taskCss = gulp.src(`${source}/css/font-awesome.min.css`)
    .pipe(changedInPlace({ firstPass: true }))
    .pipe(gulp.dest(`${project.platform.output}/css`));

  const taskFonts = gulp.src(`${source}/fonts/*`)
    .pipe(changedInPlace({ firstPass: true }))
    .pipe(gulp.dest(`${project.platform.output}/fonts`));

  return merge(taskCss, taskFonts);
}

Open the build.js file in the \aurelia_project\tasks folder and insert the following two lines; this will import the new function and execute it:

import prepareFontAwesome from './prepare-font-awesome'; // Our custom task

export default gulp.series(
  readProjectConfiguration,
  gulp.parallel(
    transpile,
    processMarkup,
    processCSS,
    prepareFontAwesome // Our custom task
  ),
  writeBundles
);

Finally, in the <head> section of your index.html file, just add the following line:

<link rel="stylesheet" href="scripts/css/font-awesome.min.css">

That's all; now you can use font-awesome icons in any Aurelia view modules (html files).

Note that this works for any complex third party library which requires resources which you have to manually include.

like image 106
JayDi Avatar answered Nov 06 '22 01:11

JayDi


Simple default settings method

Here are the 4 simple steps I use to bring in Font-Awesome to an Aurelia project that uses the CLI.

1) npm install font-awesome --save

2) add copyFiles to build of aurelia.json

"build": {
    "copyFiles": {
      "node_modules/font-awesome/fonts/*": "/fonts/"
    },

3) add bundling to dependencies array of aurelia.json

"dependencies": [
{
    "name": "font-awesome",
    "path": "../node_modules/font-awesome/css",
    "main": "font-awesome.css"
},

4) include the import for the css file (mine lives in the app.html)

<require from="font-awesome.css"></require>

=========================================================================

Alternative

Specifying a custom font location

As I was serving my files from a different location I needed to be able to tweek the font location configured. As such, below are the steps involved if you need to do the same and specify where the fonts are stored. I am using .less

1, 2) As above.

3) Instead of adding to the bundling, you need to reference the font-awesome less file within your own less file (mine is called site.less) and then set the @fa-font-path to your custom location.

@import "../node_modules/font-awesome/less/font-awesome.less";
@fa-font-path:   "fonts";

4) There is no 4, with this method as long as you have your own compiled equivalent site.css file referenced already (with the import) you don't need to add anything else.

like image 27
4imble Avatar answered Nov 06 '22 00:11

4imble


Funny I was trying to get the same thing working this morning. This is all I had to do in my aurelia.json dependencies for it to work:

      {
        "name": "font-awesome",
        "path": "../node_modules/font-awesome/",
        "main": "",
        "resources": [
          "css/font-awesome.min.css"
        ]
      },

Then in my html I had:

<require from="font-awesome/css/font-awesome.min.css"></require>
like image 8
Grace W. Avatar answered Nov 05 '22 23:11

Grace W.


Not actually answering to your question of how to integrate Font Awesome in your application using NPM, but there is an alternative, clean way to get it in your application: using the CDN.

As mentioned in other answers, Aurlia currently doesn't support bundling resources other than js, css and html out-of-the-box using the CLI. There's a lot of discussion about this subject, and there are several, mostly hacky, workarounds, like some suggested here.

Rob Eisenberg says he's planning on getting it properly integrated in the Aurelia CLI, but he considers it low priority because there's a simple workaround. To quote him:

Of course there is interest in addressing this. However, it's lower priority than other things on the list for the CLI, in part because a simple link tag will fix the problem and is much easier than the work we would have to do to solve this inside the CLI.

Source: https://github.com/aurelia/cli/issues/248#issuecomment-254254995

  1. Get your unique CDN link mailed here: http://fontawesome.io/get-started/
  2. Include this link in the head of your index html file
  3. Don't forget to remove everything you might have already added to try to get it working: the npm package (and its reference in your package.json), the reference in your aurelia.json file, any custom tasks you might have created, any <require> tags,...
like image 8
Vincent Sels Avatar answered Nov 06 '22 01:11

Vincent Sels