How can I add Font Awesome to my Aurelia project u

2019-01-07 17:53发布

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?

8条回答
唯我独甜
2楼-- · 2019-01-07 18:26

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.

查看更多
We Are One
3楼-- · 2019-01-07 18:33

I believe that bundles.dependencies section is for referencing JS libraries.

In your case, a bit of additional work will be needed. According to Aurelia CLI docs, you can create your own generators as well, which comes in handy for us.

Add some new paths to aurelia.json:

"paths": {
    ...
    "fa": "node_modules\\font-awesome",
    "faCssOutput": "src",
    "faFontsOutput": "fonts"
    ...
}

Create a task for css bundling... au generate task fa-css

Modified task file: aurelia_project\tasks\fa-css.js|ts

import * as gulp from 'gulp';
import * as changedInPlace from 'gulp-changed-in-place';
import * as project from '../aurelia.json';
import {build} from 'aurelia-cli';

export default function faCss() {
    return gulp.src(`${project.paths.fa}\\css\\*.min.css`)
        .pipe(changedInPlace({firstPass:true}))
        /* this ensures that our 'require-from' path  
           will be simply './font-awesome.min.css' */
        .pipe(gulp.dest(project.paths.faCssOutput))
        .pipe(gulp.src(`${project.paths.faCssOutput}\\font-awesome.min.css`))
        .pipe(build.bundle());
};

...and another for copying font files: au generate task fa-fonts

Modified task file: aurelia_project\tasks\fa-fonts.js|ts

import * as gulp from 'gulp';
import * as project from '../aurelia.json';

export default function faFonts() {
    return gulp.src(`${project.paths.fa}\\fonts\\*`)
        .pipe(gulp.dest(project.paths.faFontsOutput));
}

Add these new tasks above to the build process in aurelia_project\tasks\build.js|ts:

export default gulp.series(
    readProjectConfiguration,
    gulp.parallel(
        transpile,
        processMarkup,
        processCSS,
        // custom tasks
        faCss,
        faFonts
    ),
    writeBundles
);

After doing these steps, au build should embed font-awesome.min.css into scripts/app-bundle.js and copy necessary font files to ./fonts folder.

Last thing to do is to require font-awesome within our html.

<require from ="./font-awesome.min.css"></require>
查看更多
登录 后发表回答